Skip to content

Commit

Permalink
Merge pull request #23 from cpunion/reduce-types
Browse files Browse the repository at this point in the history
reduce exported types
  • Loading branch information
cpunion authored Nov 7, 2024
2 parents c4ea32c + bf2bf59 commit 397cf15
Show file tree
Hide file tree
Showing 20 changed files with 61 additions and 62 deletions.
2 changes: 1 addition & 1 deletion bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type Bool struct {
Object
}

func newBool(obj *PyObject) Bool {
func newBool(obj *cPyObject) Bool {
return Bool{newObject(obj)}
}

Expand Down
2 changes: 1 addition & 1 deletion bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type Bytes struct {
Object
}

func newBytes(obj *PyObject) Bytes {
func newBytes(obj *cPyObject) Bytes {
return Bytes{newObject(obj)}
}

Expand Down
2 changes: 1 addition & 1 deletion complex.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type Complex struct {
Object
}

func newComplex(obj *PyObject) Complex {
func newComplex(obj *cPyObject) Complex {
return Complex{newObject(obj)}
}

Expand Down
6 changes: 3 additions & 3 deletions convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
}
Expand Down
12 changes: 6 additions & 6 deletions convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
}

Expand Down
12 changes: 6 additions & 6 deletions dict.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Dict struct {
Object
}

func newDict(obj *PyObject) Dict {
func newDict(obj *cPyObject) Dict {
return Dict{newObject(obj)}
}

Expand Down Expand Up @@ -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))
Expand All @@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func CreateFunc(name string, fn any, doc string) Func {
}

type wrapperType struct {
PyObject
cPyObject
goObj any
holder *objectHolder
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions extension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand All @@ -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")
}
}
Expand Down
2 changes: 1 addition & 1 deletion float.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type Float struct {
Object
}

func newFloat(obj *PyObject) Float {
func newFloat(obj *cPyObject) Float {
return Float{newObject(obj)}
}

Expand Down
6 changes: 3 additions & 3 deletions function.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package gp
import "C"

type Objecter interface {
Obj() *PyObject
cpyObj() *cPyObject
object() Object
Ensure()
}
Expand All @@ -15,7 +15,7 @@ type Func struct {
Object
}

func newFunc(obj *PyObject) Func {
func newFunc(obj *cPyObject) Func {
return Func{newObject(obj)}
}

Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions list.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type List struct {
Object
}

func newList(obj *PyObject) List {
func newList(obj *cPyObject) List {
return List{newObject(obj)}
}

Expand All @@ -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))
Expand All @@ -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")
}
4 changes: 2 additions & 2 deletions long.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type Long struct {
Object
}

func newLong(obj *PyObject) Long {
func newLong(obj *cPyObject) Long {
return Long{newObject(obj)}
}

Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion math/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
2 changes: 1 addition & 1 deletion module.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type Module struct {
Object
}

func newModule(obj *PyObject) Module {
func newModule(obj *cPyObject) Module {
return Module{newObject(obj)}
}

Expand Down
14 changes: 7 additions & 7 deletions object.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type pyObject struct {
g *globalData
}

func (o *pyObject) Obj() *PyObject {
func (o *pyObject) cpyObj() *cPyObject {
if o == nil {
return nil
}
Expand All @@ -42,20 +42,20 @@ type Object struct {
*pyObject
}

func FromPy(obj *PyObject) Object {
func FromPy(obj *cPyObject) Object {
return newObject(obj)
}

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")
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
20 changes: 10 additions & 10 deletions object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}()

Expand Down Expand Up @@ -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")
}
}()

Expand Down
Loading

0 comments on commit 397cf15

Please sign in to comment.