[pypy-svn] r74264 - pypy/trunk/pypy/module/cpyext/test
fijal at codespeak.net
fijal at codespeak.net
Fri Apr 30 04:54:24 CEST 2010
Author: fijal
Date: Fri Apr 30 04:54:22 2010
New Revision: 74264
Modified:
pypy/trunk/pypy/module/cpyext/test/test_api.py
pypy/trunk/pypy/module/cpyext/test/test_cpyext.py
Log:
A function that takes self is a method. A nice way to share methods is via
inheritance.
Modified: pypy/trunk/pypy/module/cpyext/test/test_api.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/test/test_api.py (original)
+++ pypy/trunk/pypy/module/cpyext/test/test_api.py Fri Apr 30 04:54:22 2010
@@ -3,7 +3,7 @@
from pypy.interpreter.baseobjspace import W_Root
from pypy.module.cpyext.state import State
from pypy.module.cpyext import api
-from pypy.module.cpyext.test.test_cpyext import freeze_refcnts, check_and_print_leaks
+from pypy.module.cpyext.test.test_cpyext import freeze_refcnts, LeakCheckingTest
PyObject = api.PyObject
from pypy.interpreter.error import OperationError
from pypy.module.cpyext.state import State
@@ -15,7 +15,7 @@
def PyPy_GetReference(space, arg):
assert lltype.typeOf(arg) == PyObject
-class BaseApiTest:
+class BaseApiTest(LeakCheckingTest):
def setup_class(cls):
cls.space = gettestobjspace(usemodules=['cpyext', 'thread'])
cls.space.getbuiltinmodule("cpyext")
@@ -47,7 +47,7 @@
except OperationError, e:
print e.errorstr(self.space)
raise
- if check_and_print_leaks(self):
+ if self.check_and_print_leaks():
assert False, "Test leaks or loses object(s)."
class TestConversion(BaseApiTest):
Modified: pypy/trunk/pypy/module/cpyext/test/test_cpyext.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/test/test_cpyext.py (original)
+++ pypy/trunk/pypy/module/cpyext/test/test_cpyext.py Fri Apr 30 04:54:22 2010
@@ -78,56 +78,57 @@
self.frozen_lltallocations = lltype.ALLOCATED.copy()
lltype.TRACK_ALLOCATIONS = True
-def check_and_print_leaks(self):
- # check for sane refcnts
- leaking = False
- state = self.space.fromcache(State)
- import gc
- gc.collect()
- lost_objects_w = identity_dict()
- lost_objects_w.update((key, None) for key in self.frozen_refcounts.keys())
- for w_obj, obj in state.py_objects_w2r.iteritems():
- base_refcnt = self.frozen_refcounts.get(w_obj)
- delta = obj.c_ob_refcnt
- if base_refcnt is not None:
- delta -= base_refcnt
- lost_objects_w.pop(w_obj)
- if delta != 0:
+class LeakCheckingTest(object):
+ def check_and_print_leaks(self):
+ # check for sane refcnts
+ import gc
+
+ leaking = False
+ state = self.space.fromcache(State)
+ gc.collect()
+ lost_objects_w = identity_dict()
+ lost_objects_w.update((key, None) for key in self.frozen_refcounts.keys())
+ for w_obj, obj in state.py_objects_w2r.iteritems():
+ base_refcnt = self.frozen_refcounts.get(w_obj)
+ delta = obj.c_ob_refcnt
+ if base_refcnt is not None:
+ delta -= base_refcnt
+ lost_objects_w.pop(w_obj)
+ if delta != 0:
+ leaking = True
+ print >>sys.stderr, "Leaking %r: %i references" % (w_obj, delta)
+ lifeline = api.lifeline_dict.get(w_obj)
+ if lifeline is not None:
+ refcnt = lifeline.pyo.c_ob_refcnt
+ if refcnt > 0:
+ print >>sys.stderr, "\tThe object also held by C code."
+ else:
+ referrers_repr = []
+ for o in gc.get_referrers(w_obj):
+ try:
+ repr_str = repr(o)
+ except TypeError, e:
+ repr_str = "%s (type of o is %s)" % (str(e), type(o))
+ referrers_repr.append(repr_str)
+ referrers = ", ".join(referrers_repr)
+ print >>sys.stderr, "\tThe object is referenced by these objects:", \
+ referrers
+ for w_obj in lost_objects_w:
+ print >>sys.stderr, "Lost object %r" % (w_obj, )
leaking = True
- print >>sys.stderr, "Leaking %r: %i references" % (w_obj, delta)
- lifeline = api.lifeline_dict.get(w_obj)
- if lifeline is not None:
- refcnt = lifeline.pyo.c_ob_refcnt
- if refcnt > 0:
- print >>sys.stderr, "\tThe object also held by C code."
- else:
- referrers_repr = []
- for o in gc.get_referrers(w_obj):
- try:
- repr_str = repr(o)
- except TypeError, e:
- repr_str = "%s (type of o is %s)" % (str(e), type(o))
- referrers_repr.append(repr_str)
- referrers = ", ".join(referrers_repr)
- print >>sys.stderr, "\tThe object is referenced by these objects:", \
- referrers
- for w_obj in lost_objects_w:
- print >>sys.stderr, "Lost object %r" % (w_obj, )
- leaking = True
- for llvalue in set(ll2ctypes.ALLOCATED.values()) - self.frozen_ll2callocations:
- if getattr(llvalue, "_traceback", None): # this means that the allocation should be tracked
+ for llvalue in set(ll2ctypes.ALLOCATED.values()) - self.frozen_ll2callocations:
+ if getattr(llvalue, "_traceback", None): # this means that the allocation should be tracked
+ leaking = True
+ print >>sys.stderr, "Did not deallocate %r (ll2ctypes)" % (llvalue, )
+ print >>sys.stderr, "\t" + "\n\t".join(llvalue._traceback.splitlines())
+ for llvalue in set_difference(lltype.ALLOCATED, self.frozen_lltallocations).keys():
leaking = True
- print >>sys.stderr, "Did not deallocate %r (ll2ctypes)" % (llvalue, )
+ print >>sys.stderr, "Did not deallocate %r (llvalue)" % (llvalue, )
print >>sys.stderr, "\t" + "\n\t".join(llvalue._traceback.splitlines())
- for llvalue in set_difference(lltype.ALLOCATED, self.frozen_lltallocations).keys():
- leaking = True
- print >>sys.stderr, "Did not deallocate %r (llvalue)" % (llvalue, )
- print >>sys.stderr, "\t" + "\n\t".join(llvalue._traceback.splitlines())
-
- return leaking
+ return leaking
-class AppTestCpythonExtensionBase:
+class AppTestCpythonExtensionBase(LeakCheckingTest):
def setup_class(cls):
cls.space = gettestobjspace(usemodules=['cpyext', 'thread'])
cls.space.getbuiltinmodule("cpyext")
@@ -234,7 +235,7 @@
pass
except AttributeError:
pass
- if check_and_print_leaks(self):
+ if self.check_and_print_leaks():
assert False, "Test leaks or loses object(s)."
More information about the Pypy-commit
mailing list