github.com/goplus/llgo@v0.8.3/py/_pyg/module.c (about)

     1  #include <stdlib.h>
     2  #include <stdarg.h>
     3  #include <stdio.h>
     4  
     5  // example:
     6  // llgoLoadPyModSyms(mod, "name1", &func1, "name2", &func2, NULL)
     7  
     8  typedef struct PyObject PyObject;
     9  
    10  PyObject* PyObject_GetAttrString(PyObject* mod, const char* attrName);
    11  
    12  void llgoLoadPyModSyms(PyObject* mod, ...) {
    13      va_list ap;
    14      va_start(ap, mod);
    15      for (;;) {
    16          const char* name = va_arg(ap, const char*);
    17          if (name == NULL) {
    18              break;
    19          }
    20          PyObject** pfunc = va_arg(ap, PyObject**);
    21          if (*pfunc == NULL) {
    22              *pfunc = PyObject_GetAttrString(mod, name);
    23          }
    24      }
    25      va_end(ap);
    26  }
    27  
    28  /*
    29  wchar_t* toWcs(const char* str) {
    30      size_t len = mbstowcs(NULL, str, 0);
    31      wchar_t* wstr = (wchar_t*)malloc((len + 1) * sizeof(wchar_t));
    32      mbstowcs(wstr, str, len + 1);
    33      return wstr;
    34  }
    35  
    36  char* toMbs(const wchar_t* str) {
    37      size_t len = wcstombs(NULL, str, 0);
    38      char* mstr = (char*)malloc(len + 1);
    39      wcstombs(mstr, str, len + 1);
    40      return mstr;
    41  }
    42  
    43  wchar_t *Py_GetPath();
    44  
    45  void Py_SetPath(const wchar_t* path);
    46  void Py_Initialize();
    47  
    48  void llgoPyInitialize() {
    49      setenv("PYTHONPATH", "/opt/homebrew/lib/python3.12/site-packages", 1);
    50      Py_Initialize();
    51      printf("sys.path = %s\n", toMbs(Py_GetPath()));
    52  }
    53  */