diff --git a/chore/_xtool/llpyg/llpyg.go b/chore/_xtool/llpyg/llpyg.go index 8a098c60..86792e95 100644 --- a/chore/_xtool/llpyg/llpyg.go +++ b/chore/_xtool/llpyg/llpyg.go @@ -20,7 +20,6 @@ import ( "github.com/goplus/llgo/c" "github.com/goplus/llgo/py" "github.com/goplus/llgo/py/inspect" - // "github.com/goplus/llgo/py/builtins" ) func main() { @@ -38,20 +37,18 @@ func main() { py.Initialize() mod := py.ImportModule(pyLib) - dict := mod.ModuleGetDict() - items := dict.DictItems() - for i, n := uintptr(0), items.ListLen(); i < n; i++ { - item := items.ListItem(i) - key := item.TupleItem(0) - val := item.TupleItem(1) + keys := mod.ModuleGetDict().DictKeys() + for i, n := uintptr(0), keys.ListLen(); i < n; i++ { + key := keys.ListItem(i) + val := mod.GetAttr(key) + doc := val.GetAttrString(c.Str("__doc__")) + c.Fprintf(c.Stderr, c.Str("-----------------------------------\n")) if val.Callable() != 0 { - doc := val.GetAttrString(c.Str("__doc__")) sig := inspect.Signature(val) - c.Fprintf(c.Stderr, c.Str("-----------------------------------\n")) c.Fprintf(c.Stderr, c.Str("%s: %s\n"), key.CStr(), sig.Str().CStr()) - c.Fprintf(c.Stderr, c.Str("%s\n"), doc.CStr()) - // c.Fprintf(c.Stderr, c.Str("-----------------------------------\n")) - // builtins.Help(val) + } else { + c.Fprintf(c.Stderr, c.Str("var: %s\n"), key.CStr()) } + c.Fprintf(c.Stderr, c.Str("%s\n"), doc.CStr()) } } diff --git a/py/_pyg/llpyg/llpyg.c b/py/_pyg/llpyg/llpyg.c new file mode 100644 index 00000000..2bca6a3a --- /dev/null +++ b/py/_pyg/llpyg/llpyg.c @@ -0,0 +1,46 @@ +#include +#include + +typedef struct PyObject PyObject; + +void Py_Initialize(); + +PyObject* PyImport_ImportModule(const char* modName); +PyObject* PyModule_GetDict(PyObject* mod); +PyObject* PyObject_Str(PyObject* obj); +PyObject* PyDict_Keys(PyObject* dict); +PyObject* PyList_GetItem(PyObject* list, size_t index); +PyObject* PyTuple_GetItem(PyObject* tuple, size_t index); +PyObject* PyObject_GetAttr(PyObject* mod, PyObject* attrName); +PyObject* PyObject_GetAttrString(PyObject* mod, const char* attrName); +PyObject* PyObject_CallOneArg(PyObject* fn, PyObject* arg); + +const char* PyUnicode_AsUTF8(PyObject* str); + +size_t PyList_Size(PyObject* list); + +int PyCallable_Check(PyObject*); + +int main() { + Py_Initialize(); + PyObject* inspect = PyImport_ImportModule("inspect"); + PyObject* signature = PyObject_GetAttrString(inspect, "signature"); + PyObject* mod = PyImport_ImportModule("math"); + PyObject* dict = PyModule_GetDict(mod); + PyObject* keys = PyDict_Keys(dict); + size_t i, n; + n = PyList_Size(keys); + for (i = 0; i < n; i++) { + PyObject* key = PyList_GetItem(keys, i); + PyObject* val = PyObject_GetAttr(mod, key); + if (PyCallable_Check(val) != 0) { + PyObject* doc = PyObject_GetAttrString(val, "__doc__"); + PyObject* sig = PyObject_CallOneArg(signature, val); + printf("-----------------------------------\n"); + printf("sig: %p\n", sig); + printf("%s: %s\n", PyUnicode_AsUTF8(key), PyUnicode_AsUTF8(PyObject_Str(sig))); + printf("%s\n", PyUnicode_AsUTF8(doc)); + } + } + return 0; +}