From e9570f24007ea51017e3ec4a85538506e191dc26 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Tue, 14 May 2024 22:33:36 +0800 Subject: [PATCH] llpyg: prompt import mod fail; py: fix typo --- chore/llpyg/llpyg.go | 11 ++- py/_pyg/module.c | 28 ++++++ py/llgo_autogen.lla | Bin 1710 -> 1635 bytes py/object.go | 6 +- py/sys/gen.go | 220 +++++++++++++++++++++++++++++++++++++++++++ py/type.go | 19 ++-- 6 files changed, 267 insertions(+), 17 deletions(-) create mode 100644 py/sys/gen.go diff --git a/chore/llpyg/llpyg.go b/chore/llpyg/llpyg.go index b20b6f83..20630b11 100644 --- a/chore/llpyg/llpyg.go +++ b/chore/llpyg/llpyg.go @@ -59,6 +59,10 @@ func main() { json.Unmarshal(out.Bytes(), &mod) modName := mod.Name + if modName == "" { + log.Printf("import module %s failed\n", pyLib) + os.Exit(1) + } pkg := gogen.NewPackage("", modName, nil) pkg.Import("unsafe").MarkForceUsed(pkg) // import _ "unsafe" py := pkg.Import("github.com/goplus/llgo/py") // import "github.com/goplus/llgo/py" @@ -79,10 +83,11 @@ func main() { switch sym.Type { case "builtin_function_or_method", "function": ctx.genFunc(pkg, sym) - case "str", "float", "bool", "type", "dict", "list", "module", "int", "set": // skip + case "str", "float", "bool", "type", "dict", "tuple", "list", + "module", "int", "set", "frozenset", "flags": // skip default: t := sym.Type - if len(t) > 0 && (t[0] >= 'a' && t[0] <= 'z') { + if len(t) > 0 && (t[0] >= 'a' && t[0] <= 'z') && !strings.HasSuffix(t, "_info") { log.Panicln("unsupport type:", sym.Type) } } @@ -167,7 +172,7 @@ func genName(name string, idxDontTitle int) string { } name = strings.Join(parts, "") switch name { - case "default", "": + case "default", "func", "": name += "_" } return name diff --git a/py/_pyg/module.c b/py/_pyg/module.c index 7ce91821..571116d0 100644 --- a/py/_pyg/module.c +++ b/py/_pyg/module.c @@ -1,5 +1,6 @@ #include #include +#include // example: // llgoLoadPyModSyms(mod, "name1", &func1, "name2", &func2, NULL) @@ -23,3 +24,30 @@ void llgoLoadPyModSyms(PyObject* mod, ...) { } va_end(ap); } + +/* +wchar_t* toWcs(const char* str) { + size_t len = mbstowcs(NULL, str, 0); + wchar_t* wstr = (wchar_t*)malloc((len + 1) * sizeof(wchar_t)); + mbstowcs(wstr, str, len + 1); + return wstr; +} + +char* toMbs(const wchar_t* str) { + size_t len = wcstombs(NULL, str, 0); + char* mstr = (char*)malloc(len + 1); + wcstombs(mstr, str, len + 1); + return mstr; +} + +wchar_t *Py_GetPath(); + +void Py_SetPath(const wchar_t* path); +void Py_Initialize(); + +void llgoPyInitialize() { + setenv("PYTHONPATH", "/opt/homebrew/lib/python3.12/site-packages", 1); + Py_Initialize(); + printf("sys.path = %s\n", toMbs(Py_GetPath())); +} +*/ diff --git a/py/llgo_autogen.lla b/py/llgo_autogen.lla index 2d02e0b3e0de3b12b24620cf79da93d6b6b8691c..f8013ee2bf060fe194811d51d9864b8a62d6fe01 100644 GIT binary patch delta 415 zcmZ3-`2X zBv*d_{75kZgccgcy zm>1Xl(%H~&kix!oPe2y0%p@19v}H41ioUJBzxw_gQTy1(y_ri&eo5Xq%!ml0Yn#?h zu4I&F-(2jRwzb%K^L)lc7CzK)o6N_i!E_;KvIU#0F;IB~m%%kfR~80_P(B6*P6i2J m*fKBxgN$KGqau*X%}*)KNd<>2D+4po*vTDi3T!J`L8<`P=%P6Q delta 472 zcmaFNvyQhuz?+#xgn@y9gCTd)x`=lVckEDR1oBuI82A}v7;b@(h6<{MwYKY)nEgbPP*%T*g)jy{mZ!^}}(2Z9A!`^TRsKG9 zUmMEAvovGjhY3-?_uFbQrkJ{SlrcR$SngQ$G>L8Tc^Rv-(vM@mO}WD@wc3P#*0uJM z&bRyP>i5u-Hw&Y2Ers&gGShcG6x@S#S- zstr conversions. +// +//go:linkname GetIntMaxStrDigits py.get_int_max_str_digits +func GetIntMaxStrDigits() *py.Object + +// Set the maximum string digits limit for non-binary int<->str conversions. +// +//go:linkname SetIntMaxStrDigits py.set_int_max_str_digits +func SetIntMaxStrDigits(maxdigits *py.Object) *py.Object diff --git a/py/type.go b/py/type.go index a78cb1c6..30b1fe54 100644 --- a/py/type.go +++ b/py/type.go @@ -22,20 +22,17 @@ import ( // https://docs.python.org/3/c-api/type.html -// TypeObject represents the Python type object. -type TypeObject = Object - // Return the type’s name. Equivalent to getting the type’s __name__ attribute. // -// llgo:link (*TypeObject).Name C.PyType_GetName -func (t *TypeObject) Name() *Object { return nil } +// llgo:link (*Object).TypeName C.PyType_GetName +func (t *Object) TypeName() *Object { return nil } // Return the tp_flags member of type. This function is primarily meant for use // with Py_LIMITED_API; the individual flag bits are guaranteed to be stable across // Python releases, but access to tp_flags itself is not part of the limited API. // -// llgo:link (*TypeObject).Flags C.PyType_GetFlags -func (t *TypeObject) Flags() uint32 { return 0 } +// llgo:link (*Object).TypeFlags C.PyType_GetFlags +func (t *Object) TypeFlags() uint32 { return 0 } // Return the module object associated with the given type when the type was created // using PyType_FromModuleAndSpec(). @@ -49,8 +46,8 @@ func (t *TypeObject) Flags() uint32 { return 0 } // the class that defines the method. See ModuleByDef() for cases when PyCMethod // cannot be used. // -// llgo:link (*TypeObject).Module C.PyType_GetModule -func (t *TypeObject) Module() *Object { return nil } +// llgo:link (*Object).TypeModule C.PyType_GetModule +func (t *Object) TypeModule() *Object { return nil } -// llgo:link (*TypeObject).ModuleByDef C.PyType_GetModuleByDef -// func (t *TypeObject) ModuleByDef(def *ModuleDef) *Object { return nil } +// llgo:link (*Object).TypeModuleByDef C.PyType_GetModuleByDef +// func (t *Object) TypeModuleByDef(def *ModuleDef) *Object { return nil }