From bf2bf59499cfb8680fb9e571d6132515b2a08323 Mon Sep 17 00:00:00 2001 From: Li Jie Date: Thu, 7 Nov 2024 20:40:57 +0800 Subject: [PATCH] reduce exported types --- bool.go | 2 +- bytes.go | 2 +- complex.go | 2 +- convert.go | 6 +++--- convert_test.go | 12 ++++++------ dict.go | 12 ++++++------ extension.go | 10 +++++----- extension_test.go | 4 ++-- float.go | 2 +- function.go | 6 +++--- list.go | 6 +++--- long.go | 4 ++-- math/math.go | 2 +- module.go | 2 +- object.go | 14 +++++++------- object_test.go | 20 ++++++++++---------- python.go | 5 ++--- tuple.go | 8 ++++---- tuple_test.go | 2 +- unicode.go | 2 +- 20 files changed, 61 insertions(+), 62 deletions(-) diff --git a/bool.go b/bool.go index 8de7c78..b1f20b7 100644 --- a/bool.go +++ b/bool.go @@ -9,7 +9,7 @@ type Bool struct { Object } -func newBool(obj *PyObject) Bool { +func newBool(obj *cPyObject) Bool { return Bool{newObject(obj)} } diff --git a/bytes.go b/bytes.go index bd4856f..86556be 100644 --- a/bytes.go +++ b/bytes.go @@ -12,7 +12,7 @@ type Bytes struct { Object } -func newBytes(obj *PyObject) Bytes { +func newBytes(obj *cPyObject) Bytes { return Bytes{newObject(obj)} } diff --git a/complex.go b/complex.go index 9ae2e36..aa2636f 100644 --- a/complex.go +++ b/complex.go @@ -9,7 +9,7 @@ type Complex struct { Object } -func newComplex(obj *PyObject) Complex { +func newComplex(obj *cPyObject) Complex { return Complex{newObject(obj)} } diff --git a/convert.go b/convert.go index 2afcd50..4b5b0b0 100644 --- a/convert.go +++ b/convert.go @@ -14,7 +14,7 @@ import ( func From(from any) Object { switch v := from.(type) { case Objecter: - return newObject(v.Obj()) + return newObject(v.cpyObj()) case int8: return newObject(C.PyLong_FromLong(C.long(v))) case int16: @@ -181,11 +181,11 @@ func ToValue(from Object, to reflect.Value) bool { } } else { maps := getGlobalData() - tyMeta := maps.typeMetas[from.Type().Obj()] + tyMeta := maps.typeMetas[from.Type().cpyObj()] if tyMeta == nil { return false } - wrapper := (*wrapperType)(unsafe.Pointer(from.Obj())) + wrapper := (*wrapperType)(unsafe.Pointer(from.cpyObj())) to.Set(reflect.ValueOf(wrapper.goObj).Elem()) return true } diff --git a/convert_test.go b/convert_test.go index 3886a8f..434365c 100644 --- a/convert_test.go +++ b/convert_test.go @@ -180,15 +180,15 @@ func TestFromSpecialCases(t *testing.T) { }() func() { - // Test From with Object.Obj() + // Test From with Object.cpyObj() original := From(42) - obj := From(original.Obj()) + obj := From(original.cpyObj()) if !obj.IsLong() { - t.Error("From(Object.Obj()) did not create Long object") + t.Error("From(Object.cpyObj()) did not create Long object") } if got := obj.AsLong().Int64(); got != 42 { - t.Errorf("From(Object.Obj()) = %d, want 42", got) + t.Errorf("From(Object.cpyObj()) = %d, want 42", got) } // Test that the new object is independent @@ -281,7 +281,7 @@ func TestFromWithCustomType(t *testing.T) { obj := From(p) // Verify the type - if obj.Type().Obj() != pointClass.Obj() { + if obj.Type().cpyObj() != pointClass.cpyObj() { t.Error("From(Point) created object with wrong type") } // Verify the values @@ -311,7 +311,7 @@ func TestFromWithCustomType(t *testing.T) { obj := From(p) // Verify the type - if obj.Type().Obj() != pointClass.Obj() { + if obj.Type().cpyObj() != pointClass.cpyObj() { t.Error("From(*Point) created object with wrong type") } diff --git a/dict.go b/dict.go index 0944e9d..d1d214e 100644 --- a/dict.go +++ b/dict.go @@ -13,7 +13,7 @@ type Dict struct { Object } -func newDict(obj *PyObject) Dict { +func newDict(obj *cPyObject) Dict { return Dict{newObject(obj)} } @@ -44,19 +44,19 @@ func (d Dict) HasKey(key any) bool { } func (d Dict) Get(key Objecter) Object { - v := C.PyDict_GetItem(d.obj, key.Obj()) + v := C.PyDict_GetItem(d.obj, key.cpyObj()) C.Py_IncRef(v) return newObject(v) } func (d Dict) Set(key, value Objecter) { - keyObj := key.Obj() - valueObj := value.Obj() + keyObj := key.cpyObj() + valueObj := value.cpyObj() C.PyDict_SetItem(d.obj, keyObj, valueObj) } func (d Dict) SetString(key string, value Objecter) { - valueObj := value.Obj() + valueObj := value.cpyObj() ckey := AllocCStr(key) r := C.PyDict_SetItemString(d.obj, ckey, valueObj) C.free(unsafe.Pointer(ckey)) @@ -72,7 +72,7 @@ func (d Dict) GetString(key string) Object { } func (d Dict) Del(key Objecter) { - C.PyDict_DelItem(d.obj, key.Obj()) + C.PyDict_DelItem(d.obj, key.cpyObj()) } func (d Dict) Iter() *DictIter { diff --git a/extension.go b/extension.go index 05c746a..1158649 100644 --- a/extension.go +++ b/extension.go @@ -31,7 +31,7 @@ func CreateFunc(name string, fn any, doc string) Func { } type wrapperType struct { - PyObject + cPyObject goObj any holder *objectHolder } @@ -139,7 +139,7 @@ func getterMethod(self *C.PyObject, _closure unsafe.Pointer, methodId C.int) *C. return (*C.PyObject)(unsafe.Pointer(newWrapper)) } } - return From(field.Interface()).Obj() + return From(field.Interface()).cpyObj() } //export setterMethod @@ -307,17 +307,17 @@ func wrapperMethod_(typeMeta *typeMeta, methodMeta *slotMeta, self, args *C.PyOb } if len(results) == 0 { - return None().Obj() + return None().cpyObj() } if len(results) == 1 { - return From(results[0].Interface()).Obj() + return From(results[0].Interface()).cpyObj() } tuple := MakeTupleWithLen(len(results)) for i := range results { tuple.Set(i, From(results[i].Interface())) } - return tuple.Obj() + return tuple.cpyObj() } func goNameToPythonName(name string) string { diff --git a/extension_test.go b/extension_test.go index 6f97151..ea4ad8b 100644 --- a/extension_test.go +++ b/extension_test.go @@ -700,7 +700,7 @@ func TestAddTypeDuplicate(t *testing.T) { t.Fatal("Failed to get type on second registration") } - if typ1.Obj() != typ2.Obj() { + if typ1.cpyObj() != typ2.cpyObj() { t.Fatal("Expected same type object on second registration") } @@ -721,7 +721,7 @@ assert obj1.value == 42 t.Fatal("Failed to get type on registration with pointer") } - if typ1.Obj() != typ3.Obj() { + if typ1.cpyObj() != typ3.cpyObj() { t.Fatal("Expected same type object on second registration") } } diff --git a/float.go b/float.go index 00e5eab..b180e51 100644 --- a/float.go +++ b/float.go @@ -11,7 +11,7 @@ type Float struct { Object } -func newFloat(obj *PyObject) Float { +func newFloat(obj *cPyObject) Float { return Float{newObject(obj)} } diff --git a/function.go b/function.go index 0c4ea11..6cb6cb8 100644 --- a/function.go +++ b/function.go @@ -6,7 +6,7 @@ package gp import "C" type Objecter interface { - Obj() *PyObject + cpyObj() *cPyObject object() Object Ensure() } @@ -15,7 +15,7 @@ type Func struct { Object } -func newFunc(obj *PyObject) Func { +func newFunc(obj *cPyObject) Func { return Func{newObject(obj)} } @@ -32,7 +32,7 @@ func (f Func) callNoArgs() Object { } func (f Func) callOneArg(arg Objecter) Object { - return newObject(C.PyObject_CallOneArg(f.obj, arg.Obj())) + return newObject(C.PyObject_CallOneArg(f.obj, arg.cpyObj())) } func (f Func) CallObject(args Tuple) Object { diff --git a/list.go b/list.go index b5fa003..0d38f97 100644 --- a/list.go +++ b/list.go @@ -10,7 +10,7 @@ type List struct { Object } -func newList(obj *PyObject) List { +func newList(obj *cPyObject) List { return List{newObject(obj)} } @@ -30,7 +30,7 @@ func (l List) GetItem(index int) Object { } func (l List) SetItem(index int, item Objecter) { - itemObj := item.Obj() + itemObj := item.cpyObj() C.Py_IncRef(itemObj) r := C.PyList_SetItem(l.obj, C.Py_ssize_t(index), itemObj) check(r == 0, fmt.Sprintf("failed to set item %d in list", index)) @@ -41,6 +41,6 @@ func (l List) Len() int { } func (l List) Append(obj Objecter) { - r := C.PyList_Append(l.obj, obj.Obj()) + r := C.PyList_Append(l.obj, obj.cpyObj()) check(r == 0, "failed to append item to list") } diff --git a/long.go b/long.go index 414b90a..a65119b 100644 --- a/long.go +++ b/long.go @@ -10,7 +10,7 @@ type Long struct { Object } -func newLong(obj *PyObject) Long { +func newLong(obj *cPyObject) Long { return Long{newObject(obj)} } @@ -30,7 +30,7 @@ func LongFromString(s string, base int) Long { } func LongFromUnicode(u Object, base int) Long { - return newLong(C.PyLong_FromUnicodeObject(u.Obj(), C.int(base))) + return newLong(C.PyLong_FromUnicodeObject(u.cpyObj(), C.int(base))) } func (l Long) Int() int { diff --git a/math/math.go b/math/math.go index f65dc71..98a170d 100644 --- a/math/math.go +++ b/math/math.go @@ -14,5 +14,5 @@ func math() gp.Module { } func Sqrt(x gp.Float) gp.Float { - return math().Call("sqrt", x.Obj()).AsFloat() + return math().Call("sqrt", x).AsFloat() } diff --git a/module.go b/module.go index 5527e81..6ffed9b 100644 --- a/module.go +++ b/module.go @@ -10,7 +10,7 @@ type Module struct { Object } -func newModule(obj *PyObject) Module { +func newModule(obj *cPyObject) Module { return Module{newObject(obj)} } diff --git a/object.go b/object.go index 19a1409..e07f75e 100644 --- a/object.go +++ b/object.go @@ -18,7 +18,7 @@ type pyObject struct { g *globalData } -func (o *pyObject) Obj() *PyObject { +func (o *pyObject) cpyObj() *cPyObject { if o == nil { return nil } @@ -42,7 +42,7 @@ type Object struct { *pyObject } -func FromPy(obj *PyObject) Object { +func FromPy(obj *cPyObject) Object { return newObject(obj) } @@ -50,12 +50,12 @@ func (o Object) object() Object { return o } -func newObjectRef(obj *PyObject) Object { +func newObjectRef(obj *cPyObject) Object { C.Py_IncRef(obj) return newObject(obj) } -func newObject(obj *PyObject) Object { +func newObject(obj *cPyObject) Object { if obj == nil { C.PyErr_Print() panic("nil Python object") @@ -73,7 +73,7 @@ func (o Object) Dir() List { } func (o Object) Equals(other Objecter) bool { - return C.PyObject_RichCompareBool(o.obj, other.Obj(), C.Py_EQ) != 0 + return C.PyObject_RichCompareBool(o.obj, other.cpyObj(), C.Py_EQ) != 0 } func (o Object) Attr(name string) Object { @@ -222,14 +222,14 @@ func (o Object) Repr() string { } func (o Object) Type() Object { - return newObject(C.PyObject_Type(o.Obj())) + return newObject(C.PyObject_Type(o.cpyObj())) } func (o Object) String() string { return newStr(C.PyObject_Str(o.obj)).String() } -func (o Object) Obj() *PyObject { +func (o Object) cpyObj() *cPyObject { if o.Nil() { return nil } diff --git a/object_test.go b/object_test.go index b15948e..0f344bf 100644 --- a/object_test.go +++ b/object_test.go @@ -185,16 +185,16 @@ func TestObjectString(t *testing.T) { func TestPyObjectMethods(t *testing.T) { setupTest(t) - // Test pyObject.Obj() + // Test pyObject.cpyObj() obj := From(42) - if obj.pyObject.Obj() == nil { - t.Error("pyObject.Obj() returned nil for valid object") + if obj.pyObject.cpyObj() == nil { + t.Error("pyObject.cpyObj() returned nil for valid object") } func() { var nilObj *pyObject - if nilObj.Obj() != nil { - t.Error("pyObject.Obj() should return nil for nil object") + if nilObj.cpyObj() != nil { + t.Error("pyObject.cpyObj() should return nil for nil object") } }() @@ -365,17 +365,17 @@ def make_tuple(): }() func() { - // Test Object.Obj() + // Test Object.cpyObj() obj := From(42) - if obj.Obj() == nil { - t.Error("Object.Obj() returned nil for valid object") + if obj.cpyObj() == nil { + t.Error("Object.cpyObj() returned nil for valid object") } }() func() { var nilObj Object - if nilObj.Obj() != nil { - t.Error("Object.Obj() should return nil for nil object") + if nilObj.cpyObj() != nil { + t.Error("Object.cpyObj() should return nil for nil object") } }() diff --git a/python.go b/python.go index fedfaee..3e55b3f 100644 --- a/python.go +++ b/python.go @@ -11,8 +11,7 @@ import ( "unsafe" ) -type PyObject = C.PyObject -type PyCFunction = C.PyCFunction +type cPyObject = C.PyObject func Initialize() { runtime.LockOSThread() @@ -55,7 +54,7 @@ func CompileString(code, filename string, start InputType) (Object, error) { } func EvalCode(code Object, globals, locals Dict) Object { - return newObject(C.PyEval_EvalCode(code.Obj(), globals.Obj(), locals.Obj())) + return newObject(C.PyEval_EvalCode(code.cpyObj(), globals.cpyObj(), locals.cpyObj())) } // ---------------------------------------------------------------------------- diff --git a/tuple.go b/tuple.go index 8fce03f..bdc8b55 100644 --- a/tuple.go +++ b/tuple.go @@ -10,7 +10,7 @@ type Tuple struct { Object } -func newTuple(obj *PyObject) Tuple { +func newTuple(obj *cPyObject) Tuple { return Tuple{newObject(obj)} } @@ -34,7 +34,7 @@ func (t Tuple) Get(index int) Object { } func (t Tuple) Set(index int, obj Objecter) { - objObj := obj.Obj() + objObj := obj.cpyObj() C.Py_IncRef(objObj) r := C.PyTuple_SetItem(t.obj, C.Py_ssize_t(index), objObj) check(r == 0, fmt.Sprintf("failed to set item %d in tuple", index)) @@ -101,8 +101,8 @@ func (t Tuple) ParseArgs(addrs ...any) bool { case *bool: *v = obj.AsBool().Bool() - case **PyObject: - *v = obj.Obj() + case **cPyObject: + *v = obj.cpyObj() // Python object case *Object: diff --git a/tuple_test.go b/tuple_test.go index 29aa47f..ee41879 100644 --- a/tuple_test.go +++ b/tuple_test.go @@ -150,7 +150,7 @@ func TestTupleParseArgsTypes(t *testing.T) { var strVal string var bytesVal []byte var objVal Object - var pyObj *PyObject + var pyObj *cPyObject success = strTuple.ParseArgs(&strVal) if !success || strVal != "hello" { diff --git a/unicode.go b/unicode.go index 5ecdfd1..0e7e4b7 100644 --- a/unicode.go +++ b/unicode.go @@ -10,7 +10,7 @@ type Str struct { Object } -func newStr(obj *PyObject) Str { +func newStr(obj *cPyObject) Str { return Str{newObject(obj)} }