Commit 079d698d authored by Stefan Behnel's avatar Stefan Behnel

avoid using sys.getrefcount() where possible (PyPy doesn't have it)

parent 4cda4f13
__doc__ = u""" __doc__ = u"""
>>> class SampleException(Exception): pass >>> class SampleException(Exception): pass
>>> import sys
>>> def assert_refcount(rc1, rc2, func): >>> def assert_refcount(rc1, rc2, func):
... # test ref-counts, but allow a bit of freedom ... # test ref-counts, but allow a bit of freedom
...@@ -8,17 +7,17 @@ __doc__ = u""" ...@@ -8,17 +7,17 @@ __doc__ = u"""
... func.__name__, rc1, rc2) ... func.__name__, rc1, rc2)
>>> def run_test(repeat, test_func): >>> def run_test(repeat, test_func):
... initial_refcount = sys.getrefcount(SampleException) ... initial_refcount = get_refcount(SampleException)
... for i in range(repeat): ... for i in range(repeat):
... try: raise SampleException ... try: raise SampleException
... except: ... except:
... refcount1 = sys.getrefcount(SampleException) ... refcount1 = get_refcount(SampleException)
... test_func() ... test_func()
... refcount2 = sys.getrefcount(SampleException) ... refcount2 = get_refcount(SampleException)
... ...
... assert_refcount(refcount1, refcount2, test_func) ... assert_refcount(refcount1, refcount2, test_func)
... assert_refcount(initial_refcount, refcount2, test_func) ... assert_refcount(initial_refcount, refcount2, test_func)
... refcount3 = sys.getrefcount(SampleException) ... refcount3 = get_refcount(SampleException)
... assert_refcount(refcount1, refcount3, test_func) ... assert_refcount(refcount1, refcount3, test_func)
... assert_refcount(initial_refcount, refcount3, test_func) ... assert_refcount(initial_refcount, refcount3, test_func)
...@@ -28,6 +27,11 @@ __doc__ = u""" ...@@ -28,6 +27,11 @@ __doc__ = u"""
>>> run_test(50, test_finally) >>> run_test(50, test_finally)
""" """
from cpython.ref cimport PyObject
def get_refcount(obj):
return (<PyObject*>obj).ob_refcnt
def test_no_exception(): def test_no_exception():
try: try:
a = 1+1 a = 1+1
......
...@@ -12,7 +12,10 @@ True ...@@ -12,7 +12,10 @@ True
True True
""" """
import sys from cpython.ref cimport PyObject
def get_refcount(obj):
return (<PyObject*>obj).ob_refcnt
cdef class RefCountInMeth(object): cdef class RefCountInMeth(object):
cdef double value cdef double value
...@@ -32,27 +35,27 @@ cdef class RefCountInMeth(object): ...@@ -32,27 +35,27 @@ cdef class RefCountInMeth(object):
cdef int c_meth(self): cdef int c_meth(self):
cdef int v cdef int v
v = sys.getrefcount(self) v = get_refcount(self)
return v return v
cdef int c_meth_if(self): cdef int c_meth_if(self):
cdef int v cdef int v
if 5>6: if 5>6:
v = 7 v = 7
v = sys.getrefcount(self) v = get_refcount(self)
return v return v
def chk_meth(self): def chk_meth(self):
cdef int a,b cdef int a,b
a = sys.getrefcount(self) a = get_refcount(self)
b = self.c_meth() b = self.c_meth()
return a==b return a==b
def chk_meth_if(self): def chk_meth_if(self):
cdef int a,b cdef int a,b
a = sys.getrefcount(self) a = get_refcount(self)
b = self.c_meth_if() b = self.c_meth_if()
return a==b return a==b
......
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