Commit 22448747 authored by gsamain's avatar gsamain

Amend cypclass __new__ test (memory correctness)

parent 190f01d5
# This is extremely hacky
cdef extern from "Python.h":
struct CyObject "CyObject"
void Cy_INCREF(CyObject* o) nogil
cdef cypclass SomeMemory: cdef cypclass SomeMemory:
int a int a
...@@ -9,6 +14,10 @@ cdef cypclass SomeMemory: ...@@ -9,6 +14,10 @@ cdef cypclass SomeMemory:
void* __new__(f, int a): void* __new__(f, int a):
o = f() o = f()
o.a = a o.a = a
# As we are returning a non-reference counted variable (void*),
# Cython will decref o (as it is reference-counted).
# To avoid premature memory deallocation, we have to keep a reference here
Cy_INCREF(<CyObject*>o)
return <void*> o return <void*> o
double __new__(unused, int a, double b): double __new__(unused, int a, double b):
...@@ -31,11 +40,11 @@ cdef cypclass SomeMemory: ...@@ -31,11 +40,11 @@ cdef cypclass SomeMemory:
self.a = e self.a = e
cdef int foo(): cdef int foo():
cdef SomeMemory o = SomeMemory() o = SomeMemory()
return o.a return o.a
cdef int bar(): cdef int bar():
cdef SomeMemory o = SomeMemory(3) o = SomeMemory(3)
return o.a return o.a
cdef int baz(): cdef int baz():
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment