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

     1  #include <stdlib.h>
     2  #include <stdio.h>
     3  
     4  typedef struct PyObject PyObject;
     5  
     6  void Py_Initialize();
     7  
     8  PyObject* PyImport_ImportModule(const char* modName);
     9  PyObject* PyModule_GetDict(PyObject* mod);
    10  PyObject* PyObject_Str(PyObject* obj);
    11  PyObject* PyDict_Keys(PyObject* dict);
    12  PyObject* PyList_GetItem(PyObject* list, size_t index);
    13  PyObject* PyTuple_GetItem(PyObject* tuple, size_t index);
    14  PyObject* PyObject_GetAttr(PyObject* mod, PyObject* attrName);
    15  PyObject* PyObject_GetAttrString(PyObject* mod, const char* attrName);
    16  PyObject* PyObject_CallOneArg(PyObject* fn, PyObject* arg);
    17  
    18  const char* PyUnicode_AsUTF8(PyObject* str);
    19  
    20  size_t PyList_Size(PyObject* list);
    21  
    22  int PyCallable_Check(PyObject*);
    23  
    24  int main() {
    25      Py_Initialize();
    26      PyObject* inspect = PyImport_ImportModule("inspect");
    27      PyObject* signature = PyObject_GetAttrString(inspect, "signature");
    28  	PyObject* mod = PyImport_ImportModule("numpy");
    29  	PyObject* dict = PyModule_GetDict(mod);
    30  	PyObject* keys = PyDict_Keys(dict);
    31      size_t i, n;
    32      n = PyList_Size(keys);
    33  	for (i = 0; i < n; i++) {
    34  		PyObject* key = PyList_GetItem(keys, i);
    35          PyObject* val = PyObject_GetAttr(mod, key);
    36  		if (PyCallable_Check(val) != 0) {
    37  			PyObject* doc = PyObject_GetAttrString(val, "__doc__");
    38  			PyObject* sig = PyObject_CallOneArg(signature, val);
    39  			printf("-----------------------------------\n");
    40  			printf("sig: %p\n", sig);
    41  			printf("%s: %s\n", PyUnicode_AsUTF8(key), PyUnicode_AsUTF8(PyObject_Str(sig)));
    42  			printf("%s\n", PyUnicode_AsUTF8(key));
    43  			if (doc != NULL) {
    44  				printf("%s\n", PyUnicode_AsUTF8(doc));
    45  			}
    46  		}
    47  	}
    48      return 0;
    49  }