def test_ints(self): i = 42000123 refcnt = grc(i) ci = c_int(i) self.assertEqual(refcnt, grc(i)) self.assertEqual(ci._objects, None) return
def test_1(self): from sys import getrefcount as grc f = dll._testfunc_callback_i_if f.restype = ctypes.c_int f.argtypes = [ctypes.c_int, MyCallback] def callback(value): #print "called back with", value return value if not test_support.due_to_ironpython_bug("http://www.codeplex.com/IronPython/WorkItem/View.aspx?WorkItemId=22374"): self.assertEqual(grc(callback), 2) cb = MyCallback(callback) if not test_support.due_to_ironpython_bug("http://www.codeplex.com/IronPython/WorkItem/View.aspx?WorkItemId=22374"): self.assertTrue(grc(callback) > 2) result = f(-10, cb) self.assertEqual(result, -18) cb = None gc.collect() if not test_support.due_to_ironpython_bug("http://www.codeplex.com/IronPython/WorkItem/View.aspx?WorkItemId=22374"): self.assertEqual(grc(callback), 2)
def X_test(self): class X(Structure): _fields_ = [("p", POINTER(c_char_p))] x = X() i = c_char_p("abc def") from sys import getrefcount as grc print("2?", grc(i)) x.p = pointer(i) print("3?", grc(i)) for i in range(320): c_int(99) x.p[0] print(x.p[0]) ## del x ## print "2?", grc(i) ## del i import gc gc.collect() for i in range(320): c_int(99) x.p[0] print(x.p[0]) print(x.p.contents) ## print x._objects x.p[0] = "spam spam" ## print x.p[0] print("+" * 42) print(x._objects)
def test_X(self): class X(Structure): _fields_ = [("p", POINTER(c_char_p))] x = X() i = c_char_p("abc def") from sys import getrefcount as grc shout "2?", grc(i) x.p = pointer(i) shout "3?", grc(i) for i in range(320): c_int(99) x.p[0] shout x.p[0] ## del x ## shout "2?", grc(i) ## del i import gc gc.collect() for i in range(320): c_int(99) x.p[0] shout x.p[0] shout x.p.contents ## shout x._objects x.p[0] = "spam spam" ## shout x.p[0] shout "+" * 42 shout x._objects
def X_test(self): class X(Structure): _fields_ = [('p', POINTER(c_char_p))] x = X() i = c_char_p('abc def') from sys import getrefcount as grc print '2?', grc(i) x.p = pointer(i) print '3?', grc(i) for i in range(320): c_int(99) x.p[0] print x.p[0] import gc gc.collect() for i in range(320): c_int(99) x.p[0] print x.p[0] print x.p.contents x.p[0] = 'spam spam' print '+' * 42 print x._objects
def test_1(self): try: from sys import getrefcount as grc except ImportError: return unittest.skip("no sys.getrefcount()") f = dll._testfunc_callback_i_if f.restype = ctypes.c_int f.argtypes = [ctypes.c_int, MyCallback] def callback(value): #print "called back with", value return value self.assertEqual(grc(callback), 2) cb = MyCallback(callback) self.assertGreater(grc(callback), 2) result = f(-10, cb) self.assertEqual(result, -18) cb = None gc.collect() self.assertEqual(grc(callback), 2)
def test_via(self, vlist=['v',]): # vlist is just to make v unoptimizable # Special tests for the via classifier from sys import getrefcount as grc import gc iso = self.iso hp = self.Use d = {} k = ('k',) v = tuple(vlist) # Make sure v is not optimized to a constant d[k] = v d[v] = v rck = grc(k) rcv = grc(v) s = iso(v) self.assert_( s.byvia.kind == hp.Via("_.f_locals['v']", "_[('k',)]", "_[('v',)]", '_.keys()[1]') or s.byvia.kind == hp.Via("_.f_locals['v']", "_[('k',)]", "_[('v',)]", '_.keys()[0]')) del s gc.collect() gc.collect() self.aseq(grc(k), rck) self.aseq(grc(v), rcv )
def test_ints(self): if grc is None: return unittest.skip("no sys.getrefcount()") i = 42000123 refcnt = grc(i) ci = c_int(i) self.assertEqual(refcnt, grc(i)) self.assertEqual(ci._objects, None)
def test_c_char_p(self): if test_support.due_to_ironpython_bug("http://www.codeplex.com/IronPython/WorkItem/View.aspx?WorkItemId=22374"): return s = "Hello, World" refcnt = grc(s) cs = c_char_p(s) self.assertEqual(refcnt + 1, grc(s)) self.assertSame(cs._objects, s)
def test_PyObj_FromPtr(self): s = 'abc def ghi jkl' ref = grc(s) pyobj = PyObj_FromPtr(id(s)) self.assertIs(s, pyobj) self.assertEqual(grc(s), ref + 1) del pyobj self.assertEqual(grc(s), ref)
def test_c_char_p(self): if grc is None: return unittest.skip("no sys.getrefcount()") s = b"Hello, World" refcnt = grc(s) cs = c_char_p(s) self.assertEqual(refcnt + 1, grc(s)) self.assertSame(cs._objects, s)
def test_ints(self): if test_support.due_to_ironpython_bug("http://www.codeplex.com/IronPython/WorkItem/View.aspx?WorkItemId=22374"): return i = 42000123 refcnt = grc(i) ci = c_int(i) self.assertEqual(refcnt, grc(i)) self.assertEqual(ci._objects, None)
def test_PyObj_FromPtr(self): s = "abc def ghi jkl" ref = grc(s) # id(python-object) is the address pyobj = PyObj_FromPtr(id(s)) self.assertTrue(s is pyobj) self.assertEqual(grc(s), ref + 1) del pyobj self.assertEqual(grc(s), ref)
def test_PyString_FromString(self): pythonapi.PyString_FromString.restype = py_object pythonapi.PyString_FromString.argtypes = (c_char_p,) s = 'abc' refcnt = grc(s) pyob = pythonapi.PyString_FromString(s) self.assertEqual(grc(s), refcnt) self.assertEqual(s, pyob) del pyob self.assertEqual(grc(s), refcnt)
def test_pyobject(self): o = () from sys import getrefcount as grc for o in ((), [], object()): initial = grc(o) self.check_type(py_object, o) before = grc(o) self.check_type(py_object, o) after = grc(o) self.assertEqual((after, o), (before, o))
def test_above_30(self): """Message above 30 bytes are never copied by 0MQ.""" for i in range(5, 16): # 32, 64,..., 65536 s = (2**i)*x self.assertEquals(grc(s), 2) m = zmq.Frame(s) self.assertEquals(grc(s), 4) del m self.assertEquals(grc(s), 2) del s
def test_refcount(self): from sys import getrefcount as grc def func(*args): pass self.assertEqual(grc(func), 2) f = OtherCallback(func) self.assertGreater(grc(func), 2) del f self.assertGreaterEqual(grc(func), 2) gc.collect() self.assertEqual(grc(func), 2) class X(ctypes.Structure): _fields_ = [('a', OtherCallback)] x = X() x.a = OtherCallback(func) self.assertGreater(grc(func), 2) del x self.assertGreaterEqual(grc(func), 2) gc.collect() self.assertEqual(grc(func), 2) f = OtherCallback(func) self.assertGreater(grc(func), 2) f.cycle = f del f gc.collect() self.assertEqual(grc(func), 2)
def test_PyInt_Long(self): ref42 = grc(42) pythonapi.PyInt_FromLong.restype = py_object self.assertEqual(pythonapi.PyInt_FromLong(42), 42) self.assertEqual(grc(42), ref42) pythonapi.PyInt_AsLong.argtypes = (py_object,) pythonapi.PyInt_AsLong.restype = c_long res = pythonapi.PyInt_AsLong(42) self.assertEqual(grc(res), ref42 + 1) del res self.assertEqual(grc(42), ref42)
def test_PyString_FromString(self): pythonapi.PyString_FromString.restype = py_object pythonapi.PyString_FromString.argtypes = (c_char_p,) s = "abc" refcnt = grc(s) pyob = pythonapi.PyString_FromString(s) self.failUnlessEqual(grc(s), refcnt) self.failUnlessEqual(s, pyob) del pyob self.failUnlessEqual(grc(s), refcnt)
def test_c_char_p(self): s = "Hello, World" refcnt = grc(s) cs = c_char_p(s) self.assertEqual(refcnt + 1, grc(s)) try: # Moving gcs need to allocate a nonmoving buffer cs._objects._obj except AttributeError: self.assertSame(cs._objects, s) else: self.assertSame(cs._objects._obj, s)
def test_pyobject(self): o = () from sys import getrefcount as grc for o in (), [], object(): initial = grc(o) # This call leaks a reference to 'o'... self.check_type(py_object, o) before = grc(o) # ...but this call doesn't leak any more. Where is the refcount? self.check_type(py_object, o) after = grc(o) self.assertEqual((after, o), (before, o))
def test_weaky(self): # Test that the extra-type information in heapview # will still allow types to come, be used, and go, and be collected # This depends on that they are weakly-referenced # so internal heapview structures can remove them when they are # to be collected. import gc from sys import getrefcount as grc support.TestCase.setUp(self) sets = self.guppy.sets heapdefs = getattr(sets.setsc, '_NyHeapDefs_'), root = [] heapyc = self.guppy.heapy.heapyc nodeset = sets.NodeSet nodegraph = heapyc.NodeGraph gc.collect() probe = [] rcprobe = grc(probe) class T(object): x = probe class U(T): pass T.U = U # Make circular dependency t = T() u = U() root.append(t) root.append(u) if 1: hv = heapyc.HeapView(root, heapdefs) x = hv.heap() assert t in x x = None T = t = U = u = None root[:] = [] gc.collect() # 2 collections needed sometimes? Note Apr 15 2005 nrcprobe = grc(probe) self.aseq(nrcprobe, rcprobe)
def test_c_char_p(self): if grc is None: return unittest.skip("no sys.getrefcount()") s = "Hello, World" refcnt = grc(s) cs = c_char_p(s) self.assertEqual(refcnt + 1, grc(s)) try: # Moving gcs need to allocate a nonmoving buffer cs._objects._obj except AttributeError: self.assertSame(cs._objects, s) else: self.assertSame(cs._objects._obj, s)
def test_pyobject(self): if test_support.due_to_ironpython_bug("http://www.codeplex.com/IronPython/WorkItem/View.aspx?WorkItemId=22374"): return o = () from sys import getrefcount as grc for o in (), [], object(): initial = grc(o) # This call leaks a reference to 'o'... self.check_type(py_object, o) before = grc(o) # ...but this call doesn't leak any more. Where is the refcount? self.check_type(py_object, o) after = grc(o) self.assertEqual((after, o), (before, o))
def test__POINTER_c_char(self): class X(Structure): _fields_ = [('str', POINTER(c_char))] x = X() self.assertRaises(ValueError, getattr, x.str, 'contents') b = c_buffer('Hello, World') from sys import getrefcount as grc self.assertEqual(grc(b), 2) x.str = b self.assertEqual(grc(b), 3) for i in range(len(b)): self.assertEqual(b[i], x.str[i]) self.assertRaises(TypeError, setattr, x, 'str', 'Hello, World')
def test_refcount(self): try: from sys import getrefcount as grc except ImportError: return unittest.skip("no sys.getrefcount()") def func(*args): pass # this is the standard refcount for func self.assertEqual(grc(func), 2) # the CFuncPtr instance holds at least one refcount on func: f = OtherCallback(func) self.assertGreater(grc(func), 2) # and may release it again del f self.assertGreaterEqual(grc(func), 2) # but now it must be gone gc.collect() self.assertEqual(grc(func), 2) class X(ctypes.Structure): _fields_ = [("a", OtherCallback)] x = X() x.a = OtherCallback(func) # the CFuncPtr instance holds at least one refcount on func: self.assertGreater(grc(func), 2) # and may release it again del x self.assertGreaterEqual(grc(func), 2) # and now it must be gone again gc.collect() self.assertEqual(grc(func), 2) f = OtherCallback(func) # the CFuncPtr instance holds at least one refcount on func: self.assertGreater(grc(func), 2) # create a cycle f.cycle = f del f gc.collect() self.assertEqual(grc(func), 2)
def test_2(self): un = self.un ty = self.ty cl = self.cl do = self.do rc = self.rc iso = self.iso All = self.Anything Nothing = self.Nothing C1 = self.C1 C2 = self.C2 c1 = self.c1 class C3(object): def x(self): return 1 def asrt(x): self.assert_(x) def no(x): self.assert_(not x) eq = self.aseq # Tests to do with Nothing being finite - having length and iteration no(dict in (ty(dict) | ty(int))) no([] in (ty(dict) | ty(int))) asrt({} in (ty(dict) | ty(int))) asrt(dict in (ty(dict) | ty(int) | ty(type(dict)))) asrt(list(ty(list) & iso({})) == []) # When creating ISO classes, we don't want to memoize them # which would leak the elements. from sys import getrefcount as grc import sys, types c = C1() rc = grc(c) x = iso(c) x=None eq(grc(c), rc)
def test_lifecycle1(self): """Run through a ref counting cycle with a copy.""" for i in range(5, 16): # 32, 64,..., 65536 s = (2**i)*x rc = 2 self.assertEqual(grc(s), rc) m = zmq.Frame(s) rc += 2 self.assertEqual(grc(s), rc) m2 = copy.copy(m) rc += 1 self.assertEqual(grc(s), rc) buf = m2.buffer rc += view_rc self.assertEqual(grc(s), rc) self.assertEqual(s, b(str(m))) self.assertEqual(s, bytes(m2)) self.assertEqual(s, m.bytes) # self.assert_(s is str(m)) # self.assert_(s is str(m2)) del m2 rc -= 1 self.assertEqual(grc(s), rc) rc -= view_rc del buf self.assertEqual(grc(s), rc) del m rc -= 2 await_gc(s, rc) self.assertEqual(grc(s), rc) self.assertEqual(rc, 2) del s
def test_lifecycle2(self): """Run through a different ref counting cycle with a copy.""" for i in range(5, 16): # 32, 64,..., 65536 s = (2**i)*x rc = 2 self.assertEquals(grc(s), rc) m = zmq.Frame(s) rc += 2 self.assertEquals(grc(s), rc) m2 = copy.copy(m) rc += 1 self.assertEquals(grc(s), rc) b = m.buffer rc += view_rc self.assertEquals(grc(s), rc) self.assertEquals(s, asbytes(str(m))) self.assertEquals(s, asbytes(m2)) self.assertEquals(s, m2.bytes) self.assertEquals(s, m.bytes) # self.assert_(s is str(m)) # self.assert_(s is str(m2)) del b self.assertEquals(grc(s), rc) del m # m.buffer is kept until m is del'd rc -= view_rc rc -= 1 self.assertEquals(grc(s), rc) del m2 rc -= 2 self.assertEquals(grc(s), rc) self.assertEquals(rc, 2) del s
def test_PyObject(self): self.failUnlessRaises(NotImplementedError, PyObject, None) class PyFloatObject(PyObject): _fields_ = [("ob_fval", c_double)] self.failUnlessEqual(sizeof(PyFloatObject), float.__basicsize__, "Alignment, float changed" "or Py_TRACE_REFs not seen.") d = 100.0 rcdt = grc(float) try: fo = PyFloatObject.from_address(id(d)) self.failUnlessEqual(grc(float), rcdt) e = fo.ob_fval finally: del fo self.failUnlessEqual(d, e) del e self.failUnlessEqual(grc(float), rcdt)
def test_1(self): from sys import getrefcount as grc f = dll._testfunc_callback_i_if f.restype = ctypes.c_int f.argtypes = [ctypes.c_int, MyCallback] def callback(value): #print "called back with", value return value self.assertEqual(grc(callback), 2) cb = MyCallback(callback) self.assertTrue(grc(callback) > 2) result = f(-10, cb) self.assertEqual(result, -18) cb = None gc.collect() self.assertEqual(grc(callback), 2)
def test_retclaset(self): # Test (A) that referrer classifications don't leak their classes # and (B) that selection is not disturbed by list arguments # (This is removed since it doesnt always work) # and (C) that selection does update referrer graph correctly self.__module__ = '<Module>' # Make the rendering independent on our name from sys import getrefcount as grc import gc C1 = self.C1 c1 = self.c1 iso = self.iso rcC1 = grc(C1) o = self.python.io.StringIO() print(iso(C1).byrcs.kind, file=o) s = iso(c1).byrcs.kind print(s, file=o) self.aseq(s & iso(c1), iso(c1)) x = C1() self.aseq(s & iso(c1, x), iso(c1)) s = iso(x).byrcs.kind self.aseq(s & iso(c1, x), iso(x)) x = C1() # (C) make sure referrer graph is updated by select self.aseq(s & iso(c1, x), iso(x)) s = None x = None gc.collect() gc.collect() # Note May 17 2005 self.aseq(grc(C1), rcC1) # (A)
def test_X(self): class X(Structure): _fields_ = [('p', POINTER(c_char_p))] x = X() i = c_char_p('abc def') from sys import getrefcount as grc print('2?', grc(i)) x.p = pointer(i) print('3?', grc(i)) for i in range(320): c_int(99) x.p[0] print(x.p[0]) import gc gc.collect() for i in range(320): c_int(99) x.p[0] print(x.p[0]) print(x.p.contents) x.p[0] = 'spam spam' print('+' * 42) print(x._objects)
def test__POINTER_c_char(self): class X(Structure): _fields_ = [("str", POINTER(c_char))] x = X() # NULL pointer access self.assertRaises(ValueError, getattr, x.str, "contents") b = c_buffer("Hello, World") x.str = b if not test_support.due_to_ironpython_bug("http://www.codeplex.com/IronPython/WorkItem/View.aspx?WorkItemId=22374"): from sys import getrefcount as grc self.assertEqual(grc(b), 3) # POINTER(c_char) and Python string is NOT compatible # POINTER(c_char) and c_buffer() is compatible for i in range(len(b)): self.assertEqual(b[i], x.str[i]) self.assertRaises(TypeError, setattr, x, "str", "Hello, World")
def test_lifecycle1(self): """Run through a ref counting cycle with a copy.""" try: view = memoryview except NameError: view = type(None) for i in range(5, 16): # 32, 64,..., 65536 s = (2**i)*x rc = 2 self.assertEquals(grc(s), rc) m = zmq.Message(s) rc += 2 self.assertEquals(grc(s), rc) m2 = copy.copy(m) rc += 1 self.assertEquals(grc(s), rc) b = m2.buffer extra = int(isinstance(b,view)) # memoryview incs by 2 # buffer by 1 rc += 1+extra self.assertEquals(grc(s), rc) self.assertEquals(s, str(m).encode()) self.assertEquals(s, str(m2).encode()) self.assertEquals(s, m.bytes) # self.assert_(s is str(m)) # self.assert_(s is str(m2)) del m2 rc -= 1 self.assertEquals(grc(s), rc) rc -= 1+extra del b self.assertEquals(grc(s), rc) del m rc -= 2 self.assertEquals(grc(s), rc) self.assertEquals(rc, 2) del s
a = 10 print("value of a", a) print("type of a", type(a)) b = a print("value of b", a) from sys import getrefcount as grc print(grc(a)) del a print(grc(b)) # python 2 print(type(2**1000)) # python 3 print(type(2**1000)) # duct typing a = "Women who code" print(id(a)) a = a + "and learn" print(id(a)) name = input("Give me your name: ") print("Your name is " + name)
def test_ints(self): i = 42000123 refcnt = grc(i) ci = c_int(i) self.assertEqual(refcnt, grc(i)) self.assertEqual(ci._objects, None)
def test_ints(self): i = 42000123 refcnt = grc(i) ci = c_int(i) self.failUnlessEqual(refcnt, grc(i)) self.failUnlessEqual(ci._objects, None)
def test_c_char_p(self): s = "Hello, World" refcnt = grc(s) cs = c_char_p(s) self.failUnlessEqual(refcnt + 1, grc(s)) self.failUnlessSame(cs._objects, s)
def test_c_char_p(self): s = b"Hello, World" rc = grc(s) cs = c_char_p(s) self.assertEqual(rc + 1, grc(s)) self.assertSame(cs._objects, s)
from sys import getrefcount as grc print(grc(40)) a = 40 b = a c = [b] print(grc(40))
def test_ints(self): i = 42000123 self.failUnlessEqual(3, grc(i)) ci = c_int(i) self.failUnlessEqual(3, grc(i)) self.failUnlessEqual(ci._objects, None)
def test31(self): # Test nodeset, element-wise operations & object deallocation w. gc H = mutnodeset from sys import getrefcount as grc if 0: print H.add.__doc__ print H.append.__doc__ print H.discard.__doc__ print H.remove.__doc__ print H.tas.__doc__ print H.tac.__doc__ e1 = [] e2 = [] e3 = [] r1 = grc(e1) r2 = grc(e2) r3 = grc(e3) s = H() s.add(e1) assert e1 in s assert e2 not in s s.append(e2) assert e2 in s assert s.tas(e3) == 0 assert e3 in s assert r1 + 1 == grc(e1) assert r2 + 1 == grc(e2) assert r3 + 1 == grc(e3) assert s.tas(e3) == 1 assert s.tac(e3) == 1 assert s.tac(e3) == 0 s.discard(e3) s.remove(e2) try: s.append(e1) except ValueError: pass else: raise 'no exception from append' s.remove(e1) try: s.remove(e1) except ValueError: pass else: raise 'no exception from remove' assert r1 == grc(e1) assert r2 == grc(e2) assert r3 == grc(e3) s.add(e1) s.add(e2) s.add(e3) s = None assert r1 == grc(e1) assert r2 == grc(e2) assert r3 == grc(e3) # Test gc support import gc s = H() s.append(e1) s.append(s) # Make it cyclic assert s in s s = None gc.collect() #assert r1 == grc(e1) s = H() s.append(e1) s.append(e2) e2.append(s) # Make it cyclic s = None e2 = None gc.collect() assert r1 == grc(e1)
#----------------------------------------------------------------------------- # Tests #----------------------------------------------------------------------------- # some useful constants: x = b'x' try: view = memoryview except NameError: view = buffer if grc: rc0 = grc(x) v = view(x) view_rc = grc(x) - rc0 def await_gc(obj, rc): """wait for refcount on an object to drop to an expected value Necessary because of the zero-copy gc thread, which can take some time to receive its DECREF message. """ for i in range(50): # rc + 2 because of the refs in this function if grc(obj) <= rc + 2: return time.sleep(0.05)
def test_1(self): import gc from sys import getrefcount as grc support.TestCase.setUp(self) sets = self.guppy.sets heapdefs = getattr(sets.setsc, '_NyHeapDefs_'), root = [] heapyc = self.guppy.heapy.heapyc nodeset = sets.mutnodeset nodegraph = heapyc.NodeGraph class T(object): __slots__ = 'a', '_hiding_tag_', 'tonly' pass class U(T): __slots__ = 'b', pass class V(object): __slots__ = 'c', gc.collect() ns = nodeset() a = [ns] a.append(a) b = [] he = [] c = [] t = T() tonly = [] t.a = a t._hiding_tag_ = he t.tonly = tonly u = U() u.a = a u._hiding_tag_ = he u.b = b v = V() v.c = c a = [x for x in [list]] li = [he, a, b, c, t, u, v, T, U, V, ns, nodeset, list] rcli0 = [grc(x) for x in li] ns |= li + list(range(10000, 10010)) root.extend(li) rcli = [grc(x) for x in li] rec = nodeset([x for x in li]) x = None rec.append(rec) ns.add(rec) rec._hiding_tag_ = rec hv = heapyc.HeapView(root, heapdefs) hv.register__hiding_tag__type(T) h = hv.heap() assert a in h assert c in h assert tonly in h hv._hiding_tag_ = he h = hv.heap() del x del h del hv ns.discard(rec) rec = None gc.collect() nrcli = [grc(x) for x in li] self.aseq(rcli, nrcli) root[:] = [] ns.clear() nrcli0 = [grc(x) for x in li] self.aseq(rcli0, nrcli0)
def test_c_char_p(self): s = 'Hello, World' refcnt = grc(s) cs = c_char_p(s) self.assertEqual(refcnt + 1, grc(s)) self.assertSame(cs._objects, s)
def test_dictowner(self): # Special test for dict ownership # motivated by: dicts that are not found in traversal, should not # cause repeated (unsuccessfull) updates of dict ownership # This is a performance question, requires special kind of testing # # Also tests that dict & dict owners are not leaked import gc from sys import getrefcount as grc Use = self.Use C1 = self.C1 c1 = self.c1 iso = self.iso o = self.python.StringIO.StringIO() # Create a dict hidden from view d1 = self.View.immnodeset([{}]) d3 = {} # Remember the initial ref counts for target objects gc.collect() rcd1 = grc(list(d1)[0]) rcd3 = grc(d3) rcC1 = grc(C1) rcc1 = grc(c1) rcdc1 = grc(c1.__dict__) time = self.python.time.time N = 5 # This was the fast case, when only reachable dicts are classified gc.collect() t = time() for i in range(N): print >> o, iso(d3).kind print >> o, iso(c1.__dict__).kind fast = time() - t gc.collect() t = time() # This was a slow case; involving repeated classification of a unreachable dict # It was originally 4.97 times slower when N was 5 # The problem occurs for successive classifications of different dicts, # when at least one of them is unreachable. for i in range(N): print >> o, iso(*d1).kind print >> o, iso(c1.__dict__).kind slow = time() - t self.assert_(slow / fast < 1.5) # This is another slow case according to notes Nov 18 2004. # A succession of different unreachable dicts. gc.collect() t = time() dn = self.View.immnodeset([{} for i in range(N)]) for i in range(N): print >> o, iso(list(dn)[i]).kind slow = time() - t self.assert_(slow / fast < 1.5) N = 5 # Partition was likewise slow for unreachable dicts dn = self.View.immnodeset([{} for i in range(N)]) gc.collect() t = time() print >> o, [x[0] for x in Use.Clodo.classifier.partition(dn)] slow = time() - t self.assert_(slow / fast < 1.5) # Check that ref counts for target objects are the same as initially gc.collect() gc.collect() # Note May 17 2005 self.aseq(grc(list(d1)[0]), rcd1) self.aseq(grc(d3), rcd3) self.aseq(grc(c1), rcc1) self.aseq(grc(C1), rcC1) self.aseq(grc(c1.__dict__), rcdc1) self.aseq( o.getvalue(), """\ dict (no owner) dict of <Module>.C1 dict (no owner) dict of <Module>.C1 dict (no owner) dict of <Module>.C1 dict (no owner) dict of <Module>.C1 dict (no owner) dict of <Module>.C1 dict (no owner) dict of <Module>.C1 dict (no owner) dict of <Module>.C1 dict (no owner) dict of <Module>.C1 dict (no owner) dict of <Module>.C1 dict (no owner) dict of <Module>.C1 dict (no owner) dict (no owner) dict (no owner) dict (no owner) dict (no owner) [hp.Nothing.dictof] """.replace('<Module>', self.__module__))
def test32(self): # Test extended NodeSet functionality H = immnodeset import gc from sys import getrefcount as grc gc.collect() e1 = [] e2 = [] e3 = [] r1 = grc(e1) r2 = grc(e2) r3 = grc(e3) s = H([e1,e2]) assert e1 in s and e2 in s and not e3 in s s3 = H([e1, e3]) s |= s3 assert e3 in s assert e2 in s s &= s3 assert e2 not in s assert e1 in s la = [], [e1], [e1, e2], [e1, e2, e3], [e2], [e2, e3], [e3], [e1,e3,e3,e1] ss = [H(x) for x in la] test_set_operations(ss, ss, ss) test_set_len(ss, ss) test_set_sub(ss, ss) test_set_convert(ss, ss) for a in ss: for b in ss: # Not supported...yet.. for x in ( 'assert list(b) | a == a | b', 'assert list(b) & a == a & b', ): try: exec x except TypeError: pass else: raise Exception, 'Expected TypeError' ss = s=s3=la=a=b=c=x=None locals().clear() gc.collect() gc.collect() assert r1==grc(e1) assert r2==grc(e2) assert r3==grc(e3)
def test_dictowner(self): # Special test for dict ownership # motivated by: dicts that are not found in traversal, should not # cause repeated (unsuccessfull) updates of dict ownership # This is a performance question, requires special kind of testing # # Also tests that dict & dict owners are not leaked import gc from sys import getrefcount as grc Use = self.Use C1 = self.C1 c1 = self.c1 iso = self.iso o = self.python.io.StringIO() # Create a dict hidden from view d1 = self.View.immnodeset([{}]) d3 = {} # Remember the initial ref counts for target objects gc.collect() rcd1 = grc(list(d1)[0]) rcd3 = grc(d3) rcC1 = grc(C1) rcc1 = grc(c1) rcdc1 = grc(c1.__dict__) clock = self.python.time.time N = 5 M = 50 # This was the fast case, when only reachable dicts are classified for i in range(N): print(iso(d3).kind, file=o) print(iso(c1.__dict__).kind, file=o) # Now measure it while 1: gc.collect() t = clock() for i in range(M): iso(d3).kind iso(c1.__dict__).kind fast = clock() - t if fast >= 0.5: # Enough resolution? break else: M *= 2 # No, try more loops # This was a slow case; involving repeated classification of a unreachable dict # It was originally 4.97 times slower when N was 5 # The problem occurs for successive classifications of different dicts, # when at least one of them is unreachable. gc.collect() for i in range(N): print(iso(*d1).kind, file=o) print(iso(c1.__dict__).kind, file=o) gc.collect() # Now measure it t = clock() for i in range(M): iso(*d1).kind iso(c1.__dict__).kind slow = clock() - t self.assertTrue(slow <= 1.5 * fast) # This is another slow case according to notes Nov 18 2004. # A succession of different unreachable dicts. gc.collect() dn = self.View.immnodeset([{} for i in range(N)]) for i in range(N): print(iso(list(dn)[i]).kind, file=o) # Now measure it gc.collect() dn = self.View.immnodeset([{} for i in range(M)]) t = clock() for i in range(M): iso(list(dn)[i]).kind slow = clock() - t # Sometimes M might be huge, and the vast majority of time it is not # doing the classification. # self.assertTrue(slow <= 1.5*fast) # Partition was likewise slow for unreachable dicts dn = self.View.immnodeset([{} for i in range(N)]) gc.collect() print([x[0] for x in Use.Clodo.classifier.partition(dn)], file=o) # Now measure it dn = self.View.immnodeset([{} for i in range(M)]) gc.collect() t = clock() [x[0] for x in Use.Clodo.classifier.partition(dn)] slow = clock() - t self.assertTrue(slow <= 1.5 * fast) # Check that ref counts for target objects are the same as initially gc.collect() gc.collect() # Note May 17 2005 self.aseq(grc(list(d1)[0]), rcd1) self.aseq(grc(d3), rcd3) self.aseq(grc(c1), rcc1) self.aseq(grc(C1), rcC1) self.aseq(grc(c1.__dict__), rcdc1) self.aseq( o.getvalue(), """\ dict (no owner) dict of <Module>.C1 dict (no owner) dict of <Module>.C1 dict (no owner) dict of <Module>.C1 dict (no owner) dict of <Module>.C1 dict (no owner) dict of <Module>.C1 dict (no owner) dict of <Module>.C1 dict (no owner) dict of <Module>.C1 dict (no owner) dict of <Module>.C1 dict (no owner) dict of <Module>.C1 dict (no owner) dict of <Module>.C1 dict (no owner) dict (no owner) dict (no owner) dict (no owner) dict (no owner) [hp.Nothing.dictof] """.replace('<Module>', self.__module__))
def test_c_char_p(self): s = "Hello, World" self.failUnlessEqual(3, grc(s)) cs = c_char_p(s) self.failUnlessEqual(4, grc(s)) self.failUnlessSame(cs._objects, s)