Back to index

python3.2  3.2.2
Public Member Functions
ctypes.test.test_refcounts.RefcountTestCase Class Reference

List of all members.

Public Member Functions

def test_1
def test_refcount

Detailed Description

Definition at line 11 of file test_refcounts.py.


Member Function Documentation

Definition at line 13 of file test_refcounts.py.

00013 
00014     def test_1(self):
00015         from sys import getrefcount as grc
00016 
00017         f = dll._testfunc_callback_i_if
00018         f.restype = ctypes.c_int
00019         f.argtypes = [ctypes.c_int, MyCallback]
00020 
00021         def callback(value):
00022             #print "called back with", value
00023             return value
00024 
00025         self.assertEqual(grc(callback), 2)
00026         cb = MyCallback(callback)
00027 
00028         self.assertTrue(grc(callback) > 2)
00029         result = f(-10, cb)
00030         self.assertEqual(result, -18)
00031         cb = None
00032 
00033         gc.collect()
00034 
00035         self.assertEqual(grc(callback), 2)
00036 

Here is the call graph for this function:

Definition at line 37 of file test_refcounts.py.

00037 
00038     def test_refcount(self):
00039         from sys import getrefcount as grc
00040         def func(*args):
00041             pass
00042         # this is the standard refcount for func
00043         self.assertEqual(grc(func), 2)
00044 
00045         # the CFuncPtr instance holds atr least one refcount on func:
00046         f = OtherCallback(func)
00047         self.assertTrue(grc(func) > 2)
00048 
00049         # and may release it again
00050         del f
00051         self.assertTrue(grc(func) >= 2)
00052 
00053         # but now it must be gone
00054         gc.collect()
00055         self.assertTrue(grc(func) == 2)
00056 
00057         class X(ctypes.Structure):
00058             _fields_ = [("a", OtherCallback)]
00059         x = X()
00060         x.a = OtherCallback(func)
00061 
00062         # the CFuncPtr instance holds atr least one refcount on func:
00063         self.assertTrue(grc(func) > 2)
00064 
00065         # and may release it again
00066         del x
00067         self.assertTrue(grc(func) >= 2)
00068 
00069         # and now it must be gone again
00070         gc.collect()
00071         self.assertEqual(grc(func), 2)
00072 
00073         f = OtherCallback(func)
00074 
00075         # the CFuncPtr instance holds atr least one refcount on func:
00076         self.assertTrue(grc(func) > 2)
00077 
00078         # create a cycle
00079         f.cycle = f
00080 
00081         del f
00082         gc.collect()
00083         self.assertEqual(grc(func), 2)


The documentation for this class was generated from the following file: