Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions base.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,11 @@ type BaseObject struct {
o *C.PyObject
}

var baseObjMap = make(map[*C.PyObject]*BaseObject)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get why are you removing this? and similar ones.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it causes memory leaks. there is no code which in charge to remove an item from the map

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But doesn't this enable us to reuse an object created before and not create it again?

Copy link
Copy Markdown
Author

@halturin halturin Dec 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think its broken by design, i mean the way to save few bytes and couple milliseconds :) and caused a lot of memory leaks with random data. There are two ways to fix it:

  1. remove the reason (pull request)
  2. implement some kind of TTL (time to live) for every single item stored this way

its your repo, your choice :)

Anyway, i've forked this repo and using it without leaks

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, so I will check if there is any problem with other repositories while using these changes. Thanks


// BaseType is the Type object that represents the BaseObject type.
var BaseType = newType((*C.PyObject)(unsafe.Pointer(C.getBasePyType(C.GoPyBaseObject_Type))))

func newBaseObject(obj *C.PyObject) *BaseObject {
if bo, ok := baseObjMap[obj]; ok {
return bo
}
bo := &BaseObject{o: obj}
baseObjMap[obj] = bo
return bo
}

Expand Down Expand Up @@ -200,8 +194,11 @@ func (obj *BaseObject) CallObject(args *Tuple) (Object, error) {
var a *C.PyObject = nil
if args != nil {
a = c(args)
defer args.Decref()
}

ret := C.PyObject_CallObject(c(obj), a)

return obj2ObjErr(ret)
}

Expand Down
6 changes: 0 additions & 6 deletions complex.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ type Complex struct {
o *C.PyComplexObject
}

var complexObjMap = make(map[*C.PyObject]*Complex)

// ComplexType is the Type object that represents the Complex type.
var ComplexType = newType((*C.PyObject)(unsafe.Pointer(C.getBasePyType(C.GoPyComplex_Type))))

Expand All @@ -29,11 +27,7 @@ func complexCheck(obj Object) bool {
}

func newComplex(obj *C.PyObject) *Complex {
if c, ok := complexObjMap[obj]; ok {
return c
}
c := &Complex{o: (*C.PyComplexObject)(unsafe.Pointer(obj))}
complexObjMap[obj] = c
return c
}

Expand Down
6 changes: 0 additions & 6 deletions dict.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ type Dict struct {
o *C.PyDictObject
}

var dictObjMap = make(map[*C.PyObject]*Dict)

// DictType is the Type object that represents the Dict type.
var DictType = newType((*C.PyObject)(unsafe.Pointer(C.getBasePyType(C.GoPyDict_Type))))

Expand All @@ -35,11 +33,7 @@ func dictCheck(obj Object) bool {
}

func newDict(obj *C.PyObject) *Dict {
if di, ok := dictObjMap[obj]; ok {
return di
}
di := &Dict{o: (*C.PyDictObject)(unsafe.Pointer(obj))}
dictObjMap[obj] = di
return di
}

Expand Down
6 changes: 0 additions & 6 deletions exceptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,8 @@ type ExceptionClass struct {
o *C.PyBaseExceptionObject
}

var exceptionObjMap = make(map[*C.PyObject]*ExceptionClass)

func newException(obj *C.PyObject) *ExceptionClass {
if e, ok := exceptionObjMap[obj]; ok {
return e
}
e := &ExceptionClass{o: (*C.PyBaseExceptionObject)(unsafe.Pointer(obj))}
exceptionObjMap[obj] = e
return e
}

Expand Down
6 changes: 0 additions & 6 deletions float.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ type Float struct {
o *C.PyFloatObject
}

var floatObjMap = make(map[*C.PyObject]*Float)

// FloatType is the Type object that represents the Float type.
var FloatType = newType((*C.PyObject)(unsafe.Pointer(C.getBasePyType(C.GoPyFloat_Type))))

Expand All @@ -29,11 +27,7 @@ func floatCheck(obj Object) bool {
}

func newFloat(obj *C.PyObject) *Float {
if f, ok := floatObjMap[obj]; ok {
return f
}
f := &Float{o: (*C.PyFloatObject)(unsafe.Pointer(obj))}
floatObjMap[obj] = f
return f
}

Expand Down
6 changes: 0 additions & 6 deletions function.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,11 @@ type Function struct {
o *C.PyFunctionObject
}

var functionObjMap = make(map[*C.PyObject]*Function)

// FunctionType is the Type object that represents the Function type.
var FunctionType = newType((*C.PyObject)(unsafe.Pointer(C.getBasePyType(C.GoPyFunction_Type))))

func newFunction(obj *C.PyObject) *Function {
if f, ok := functionObjMap[obj]; ok {
return f
}
f := &Function{o: (*C.PyFunctionObject)(unsafe.Pointer(obj))}
functionObjMap[obj] = f
return f
}

Expand Down
6 changes: 0 additions & 6 deletions list.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ type List struct {
o *C.PyListObject
}

var listObjMap = make(map[*C.PyObject]*List)

// ListType is the Type object that represents the List type.
var ListType = newType((*C.PyObject)(unsafe.Pointer(C.getBasePyType(C.GoPyList_Type))))

Expand All @@ -32,11 +30,7 @@ func listCheck(obj Object) bool {
}

func newList(obj *C.PyObject) *List {
if l, ok := listObjMap[obj]; ok {
return l
}
l := &List{o: (*C.PyListObject)(unsafe.Pointer(obj))}
listObjMap[obj] = l
return l
}

Expand Down
6 changes: 0 additions & 6 deletions long.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ type Long struct {
o *C.PyLongObject
}

var longObjMap = make(map[*C.PyObject]*Long)

// LongType is the Type object that represents the Long type.
var LongType = newType((*C.PyObject)(unsafe.Pointer(C.getBasePyType(C.GoPyLong_Type))))

Expand All @@ -32,11 +30,7 @@ func longCheck(obj Object) bool {
}

func newLong(obj *C.PyObject) *Long {
if l, ok := longObjMap[obj]; ok {
return l
}
l := &Long{o: (*C.PyLongObject)(unsafe.Pointer(obj))}
longObjMap[obj] = l
return l
}

Expand Down
6 changes: 0 additions & 6 deletions module.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ type Module struct {
o *C.PyObject
}

var moduleObjMap = make(map[*C.PyObject]*Module)

// ModuleType is the Type object that represents the Module type.
var ModuleType = newType((*C.PyObject)(unsafe.Pointer(C.getBasePyType(C.GoPyModule_Type))))

Expand All @@ -30,11 +28,7 @@ func moduleCheck(obj Object) bool {
}

func newModule(obj *C.PyObject) *Module {
if m, ok := moduleObjMap[obj]; ok {
return m
}
m := &Module{o: obj}
moduleObjMap[obj] = m
return m
}

Expand Down
6 changes: 0 additions & 6 deletions set.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ type FrozenSet struct {
Set
}

var setObjMap = make(map[*C.PyObject]*Set)

// SetType is the Type object that represents the Set type.
var SetType = newType((*C.PyObject)(unsafe.Pointer(C.getBasePyType(C.GoPySet_Type))))

Expand All @@ -41,11 +39,7 @@ func frozenSetCheck(obj Object) bool {
}

func newSet(obj *C.PyObject) *Set {
if s, ok := setObjMap[obj]; ok {
return s
}
s := &Set{o: (*C.PySetObject)(unsafe.Pointer(obj))}
setObjMap[obj] = s
return s
}

Expand Down
6 changes: 0 additions & 6 deletions tuple.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ type Tuple struct {
o *C.PyTupleObject
}

var tupleObjMap = make(map[*C.PyObject]*Tuple)

// TupleType is the Type object that represents the Tuple type.
var TupleType = newType((*C.PyObject)(unsafe.Pointer(C.getBasePyType(C.GoPyTuple_Type))))

Expand All @@ -33,11 +31,7 @@ func tupleCheck(obj Object) bool {
}

func newTuple(obj *C.PyObject) *Tuple {
if t, ok := tupleObjMap[obj]; ok {
return t
}
t := &Tuple{o: (*C.PyTupleObject)(unsafe.Pointer(obj))}
tupleObjMap[obj] = t
return t
}

Expand Down
6 changes: 0 additions & 6 deletions type.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ type Type struct {
o *C.PyTypeObject
}

var typeObjMap = make(map[*C.PyObject]*Type)

// TypeType is the Type object that represents the Type type.
var TypeType = newType((*C.PyObject)(unsafe.Pointer(C.getBasePyType(C.GoPyType_Type))))

Expand All @@ -35,11 +33,7 @@ func typeCheck(obj Object) bool {
}

func newType(obj *C.PyObject) *Type {
if t, ok := typeObjMap[obj]; ok {
return t
}
t := &Type{o: (*C.PyTypeObject)(unsafe.Pointer(obj))}
typeObjMap[obj] = t
return t
}

Expand Down
6 changes: 0 additions & 6 deletions unicode.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ type Unicode struct {
o *C.PyUnicodeObject
}

var unicodeObjMap = make(map[*C.PyObject]*Unicode)

// UnicodeType is the Type object that represents the Unicode type.
var UnicodeType = newType((*C.PyObject)(unsafe.Pointer(C.getBasePyType(C.GoPyUnicode_Type))))

Expand All @@ -25,11 +23,7 @@ func unicodeCheck(obj Object) bool {
}

func newUnicode(obj *C.PyObject) *Unicode {
if u, ok := unicodeObjMap[obj]; ok {
return u
}
u := &Unicode{o: (*C.PyUnicodeObject)(unsafe.Pointer(obj))}
unicodeObjMap[obj] = u
return u
}

Expand Down