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