From b1d1c72b2fc199891c7dd47227ebc2f4ae14ae30 Mon Sep 17 00:00:00 2001 From: Li Jie Date: Wed, 30 Oct 2024 09:42:49 +0800 Subject: [PATCH] clean func signature and debug --- README.md | 8 +++++-- _demo/gradio/gradio.go | 8 ------- bool.go | 2 +- bytes.go | 2 +- complex.go | 2 +- dict.go | 4 ++-- float.go | 2 +- function.go | 51 +++++------------------------------------- list.go | 2 +- long.go | 2 +- module.go | 2 +- object.go | 6 ++--- python.go | 1 + tuple.go | 2 +- unicode.go | 2 +- 15 files changed, 26 insertions(+), 70 deletions(-) diff --git a/README.md b/README.md index 3417de8..bcaf959 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,12 @@ +# go-python: a CPython wrapper for Go + +Make Go and Python code inter-operable. + ## Goal - Provide automatically DecRef for Python objects. - Wrap generic PyObject(s) to typed Python objects. -- Provide a way to define Python objects in LLGo. +- Provide a way to define Python objects in Go. ## Python types wrapper design @@ -13,7 +17,7 @@ type pyObject struct { obj *C.PyObject } -func newObject(obj *C.PyObject) *pyObject { +func newObject(obj *PyObject) *pyObject { o := &pyObject{obj} runtime.SetFinalizer(o, func(o *pyObject) { o.obj.DecRef() diff --git a/_demo/gradio/gradio.go b/_demo/gradio/gradio.go index eac5b5f..e236bee 100644 --- a/_demo/gradio/gradio.go +++ b/_demo/gradio/gradio.go @@ -10,7 +10,6 @@ import "C" import ( "os" - "unsafe" "github.com/cpunion/go-python" ) @@ -48,13 +47,6 @@ func UpdateExamples(country string) python.Object { } } -//export UpdateExamples2 -func UpdateExamples2(self, args *C.PyObject) *C.PyObject { - argsTuple := python.FromPy((*python.PyObject)(unsafe.Pointer(args))).AsTuple() - country := argsTuple.Get(0).String() - return (*C.PyObject)(unsafe.Pointer(UpdateExamples(country).Obj())) -} - func main() { if len(os.Args) > 2 { // avoid gradio start subprocesses diff --git a/bool.go b/bool.go index 2a4ff03..fc55f34 100644 --- a/bool.go +++ b/bool.go @@ -10,7 +10,7 @@ type Bool struct { Object } -func newBool(obj *C.PyObject) Bool { +func newBool(obj *PyObject) Bool { return Bool{newObject(obj)} } diff --git a/bytes.go b/bytes.go index 2dae9d9..ef12697 100644 --- a/bytes.go +++ b/bytes.go @@ -13,7 +13,7 @@ type Bytes struct { Object } -func newBytes(obj *C.PyObject) Bytes { +func newBytes(obj *PyObject) Bytes { return Bytes{newObject(obj)} } diff --git a/complex.go b/complex.go index abcf98a..41dc4d5 100644 --- a/complex.go +++ b/complex.go @@ -10,7 +10,7 @@ type Complex struct { Object } -func newComplex(obj *C.PyObject) Complex { +func newComplex(obj *PyObject) Complex { return Complex{newObject(obj)} } diff --git a/dict.go b/dict.go index e981964..7d0495c 100644 --- a/dict.go +++ b/dict.go @@ -10,11 +10,11 @@ type Dict struct { Object } -func newDict(obj *C.PyObject) Dict { +func newDict(obj *PyObject) Dict { return Dict{newObject(obj)} } -func NewDict(obj *C.PyObject) Dict { +func NewDict(obj *PyObject) Dict { return newDict(obj) } diff --git a/float.go b/float.go index a1946ff..d09edf8 100644 --- a/float.go +++ b/float.go @@ -10,7 +10,7 @@ type Float struct { Object } -func newFloat(obj *C.PyObject) Float { +func newFloat(obj *PyObject) Float { return Float{newObject(obj)} } diff --git a/function.go b/function.go index 68fb9ac..22e2e41 100644 --- a/function.go +++ b/function.go @@ -24,7 +24,7 @@ type Func struct { Object } -func newFunc(obj *C.PyObject) Func { +func newFunc(obj *PyObject) Func { return Func{newObject(obj)} } @@ -69,30 +69,23 @@ func (f Func) Call(args ...any) Object { type wrapperContext struct { v any + t reflect.Type } //export wrapperFunc -func wrapperFunc(self, args *C.PyObject) *C.PyObject { +func wrapperFunc(self, args *PyObject) *PyObject { wCtx := (*wrapperContext)(C.PyCapsule_GetPointer(self, AllocCStr("wrapperContext"))) - fmt.Printf("wrapperContext: %p\n", wCtx) - // 恢复上下文 v := reflect.ValueOf(wCtx.v) t := v.Type() - fmt.Printf("wrapperFunc type: %v\n", t) - // 构建参数 + goArgs := make([]reflect.Value, t.NumIn()) - argsTuple := FromPy(args).AsTuple() - fmt.Printf("args: %v\n", argsTuple) for i := range goArgs { goArgs[i] = reflect.New(t.In(i)).Elem() ToValue(FromPy(C.PyTuple_GetItem(args, C.Py_ssize_t(i))), goArgs[i]) - fmt.Printf("goArgs[%d]: %T\n", i, goArgs[i].Interface()) } - // 调用原始函数 results := v.Call(goArgs) - // 处理返回值 if len(results) == 0 { return None().Obj() } @@ -117,8 +110,6 @@ func FuncOf1(name string, fn unsafe.Pointer, doc string) Func { return newFunc(pyFn) } -var ctxs = make(map[unsafe.Pointer]*wrapperContext) - func FuncOf(name string, fn any, doc string) Func { m := MainModule() v := reflect.ValueOf(fn) @@ -127,13 +118,8 @@ func FuncOf(name string, fn any, doc string) Func { fmt.Printf("type: %T, kind: %d\n", fn, t.Kind()) panic("AddFunction: fn must be a function") } - println("FuncOf name:", name) - fmt.Printf("FuncOf type: %v\n", t) - ctx := new(wrapperContext) - ctx.v = fn + ctx := &wrapperContext{v: fn, t: t} obj := C.PyCapsule_New(unsafe.Pointer(ctx), AllocCStr("wrapperContext"), nil) - fmt.Printf("FuncOf ctx: %p\n", ctx) - ctxs[unsafe.Pointer(ctx)] = ctx def := &C.PyMethodDef{ ml_name: AllocCStr(name), ml_meth: C.PyCFunction(C.wrapperFunc), @@ -146,30 +132,3 @@ func FuncOf(name string, fn any, doc string) Func { } return newFunc(pyFn) } - -func buildFormatString(t reflect.Type) *C.char { - format := "" - for i := 0; i < t.NumIn(); i++ { - switch t.In(i).Kind() { - case reflect.Int, reflect.Int64: - format += "i" - case reflect.Float64: - format += "d" - case reflect.String: - format += "s" - // Add more types as needed - default: - panic(fmt.Sprintf("Unsupported argument type: %v", t.In(i))) - } - } - return AllocCStr(format) -} - -func buildArgPointers(args []reflect.Value) []interface{} { - pointers := make([]interface{}, len(args)) - for i := range args { - args[i] = reflect.New(args[i].Type()).Elem() - pointers[i] = args[i].Addr().Interface() - } - return pointers -} diff --git a/list.go b/list.go index e61815a..60e656a 100644 --- a/list.go +++ b/list.go @@ -10,7 +10,7 @@ type List struct { Object } -func newList(obj *C.PyObject) List { +func newList(obj *PyObject) List { return List{newObject(obj)} } diff --git a/long.go b/long.go index 0b2f805..283cfe4 100644 --- a/long.go +++ b/long.go @@ -10,7 +10,7 @@ type Long struct { Object } -func newLong(obj *C.PyObject) Long { +func newLong(obj *PyObject) Long { return Long{newObject(obj)} } diff --git a/module.go b/module.go index 01af318..8689d83 100644 --- a/module.go +++ b/module.go @@ -14,7 +14,7 @@ type Module struct { Object } -func newModule(obj *C.PyObject) Module { +func newModule(obj *PyObject) Module { return Module{newObject(obj)} } diff --git a/object.go b/object.go index 17ba423..08ce21e 100644 --- a/object.go +++ b/object.go @@ -18,7 +18,7 @@ type pyObject struct { obj *C.PyObject } -func (obj *pyObject) Obj() *C.PyObject { +func (obj *pyObject) Obj() *PyObject { if obj == nil { return nil } @@ -50,7 +50,7 @@ func (obj Object) object() Object { return obj } -func newObject(obj *C.PyObject) Object { +func newObject(obj *PyObject) Object { if obj == nil { C.PyErr_Print() panic("nil Python object") @@ -173,7 +173,7 @@ func (obj Object) String() string { return newStr(C.PyObject_Str(obj.obj)).String() } -func (obj Object) Obj() *C.PyObject { +func (obj Object) Obj() *PyObject { if obj.Nil() { return nil } diff --git a/python.go b/python.go index 9ea94a7..144363f 100644 --- a/python.go +++ b/python.go @@ -8,6 +8,7 @@ import "C" import "unsafe" type PyObject = C.PyObject +type PyCFunction = C.PyCFunction func Initialize() { C.Py_Initialize() diff --git a/tuple.go b/tuple.go index a46731e..96f6388 100644 --- a/tuple.go +++ b/tuple.go @@ -10,7 +10,7 @@ type Tuple struct { Object } -func newTuple(obj *C.PyObject) Tuple { +func newTuple(obj *PyObject) Tuple { return Tuple{newObject(obj)} } diff --git a/unicode.go b/unicode.go index 4c09daf..c20bfae 100644 --- a/unicode.go +++ b/unicode.go @@ -14,7 +14,7 @@ type Str struct { Object } -func newStr(obj *C.PyObject) Str { +func newStr(obj *PyObject) Str { return Str{newObject(obj)} }