10 class TestGObjectAPI(unittest.TestCase):
11 def testGObjectModule(self):
12 obj = gobject.GObject()
13 self.assertEquals(obj.__module__,
17 class TestReferenceCounting(unittest.TestCase):
18 def testRegularObject(self):
19 obj = gobject.GObject()
20 self.assertEquals(obj.__grefcount__, 1)
22 obj = gobject.new(gobject.GObject)
23 self.assertEquals(obj.__grefcount__, 1)
25 def testFloatingWithSinkFunc(self):
26 obj = testhelper.FloatingWithSinkFunc()
27 self.assertEquals(obj.__grefcount__, 1)
29 obj = gobject.new(testhelper.FloatingWithSinkFunc)
30 self.assertEquals(obj.__grefcount__, 1)
32 def testFloatingWithoutSinkFunc(self):
33 obj = testhelper.FloatingWithoutSinkFunc()
34 self.assertEquals(obj.__grefcount__, 1)
36 obj = gobject.new(testhelper.FloatingWithoutSinkFunc)
37 self.assertEquals(obj.__grefcount__, 1)
39 def testOwnedByLibrary(self):
40 # Upon creation, the refcount of the object should be 2:
41 # - someone already has a reference on the new object.
42 # - the python wrapper should hold its own reference.
43 obj = testhelper.OwnedByLibrary()
44 self.assertEquals(obj.__grefcount__, 2)
46 # We ask the library to release its reference, so the only
47 # remaining ref should be our wrapper's. Once the wrapper
48 # will run out of scope, the object will get finalized.
50 self.assertEquals(obj.__grefcount__, 1)
52 def testOwnedByLibraryOutOfScope(self):
53 obj = testhelper.OwnedByLibrary()
54 self.assertEquals(obj.__grefcount__, 2)
56 # We are manually taking the object out of scope. This means
57 # that our wrapper has been freed, and its reference dropped. We
58 # cannot check it but the refcount should now be 1 (the ref held
59 # by the library is still there, we didn't call release()
62 # When we get the object back from the lib, the wrapper is
63 # re-created, so our refcount will be 2 once again.
64 obj = testhelper.owned_by_library_get_instance_list()[0]
65 self.assertEquals(obj.__grefcount__, 2)
68 self.assertEquals(obj.__grefcount__, 1)
70 def testOwnedByLibraryUsingGObjectNew(self):
71 # Upon creation, the refcount of the object should be 2:
72 # - someone already has a reference on the new object.
73 # - the python wrapper should hold its own reference.
74 obj = gobject.new(testhelper.OwnedByLibrary)
75 self.assertEquals(obj.__grefcount__, 2)
77 # We ask the library to release its reference, so the only
78 # remaining ref should be our wrapper's. Once the wrapper
79 # will run out of scope, the object will get finalized.
81 self.assertEquals(obj.__grefcount__, 1)
83 def testOwnedByLibraryOutOfScopeUsingGobjectNew(self):
84 obj = gobject.new(testhelper.OwnedByLibrary)
85 self.assertEquals(obj.__grefcount__, 2)
87 # We are manually taking the object out of scope. This means
88 # that our wrapper has been freed, and its reference dropped. We
89 # cannot check it but the refcount should now be 1 (the ref held
90 # by the library is still there, we didn't call release()
93 # When we get the object back from the lib, the wrapper is
94 # re-created, so our refcount will be 2 once again.
95 obj = testhelper.owned_by_library_get_instance_list()[0]
96 self.assertEquals(obj.__grefcount__, 2)
99 self.assertEquals(obj.__grefcount__, 1)
101 def testFloatingAndSunk(self):
102 # Upon creation, the refcount of the object should be 2:
103 # - someone already has a reference on the new object.
104 # - the python wrapper should hold its own reference.
105 obj = testhelper.FloatingAndSunk()
106 self.assertEquals(obj.__grefcount__, 2)
108 # We ask the library to release its reference, so the only
109 # remaining ref should be our wrapper's. Once the wrapper
110 # will run out of scope, the object will get finalized.
112 self.assertEquals(obj.__grefcount__, 1)
114 def testFloatingAndSunkOutOfScope(self):
115 obj = testhelper.FloatingAndSunk()
116 self.assertEquals(obj.__grefcount__, 2)
118 # We are manually taking the object out of scope. This means
119 # that our wrapper has been freed, and its reference dropped. We
120 # cannot check it but the refcount should now be 1 (the ref held
121 # by the library is still there, we didn't call release()
124 # When we get the object back from the lib, the wrapper is
125 # re-created, so our refcount will be 2 once again.
126 obj = testhelper.floating_and_sunk_get_instance_list()[0]
127 self.assertEquals(obj.__grefcount__, 2)
130 self.assertEquals(obj.__grefcount__, 1)
133 def testFloatingAndSunkUsingGObjectNew(self):
134 # Upon creation, the refcount of the object should be 2:
135 # - someone already has a reference on the new object.
136 # - the python wrapper should hold its own reference.
137 obj = gobject.new(testhelper.FloatingAndSunk)
138 self.assertEquals(obj.__grefcount__, 2)
140 # We ask the library to release its reference, so the only
141 # remaining ref should be our wrapper's. Once the wrapper
142 # will run out of scope, the object will get finalized.
144 self.assertEquals(obj.__grefcount__, 1)
146 def testFloatingAndSunkOutOfScopeUsingGObjectNew(self):
147 obj = gobject.new(testhelper.FloatingAndSunk)
148 self.assertEquals(obj.__grefcount__, 2)
150 # We are manually taking the object out of scope. This means
151 # that our wrapper has been freed, and its reference dropped. We
152 # cannot check it but the refcount should now be 1 (the ref held
153 # by the library is still there, we didn't call release()
156 # When we get the object back from the lib, the wrapper is
157 # re-created, so our refcount will be 2 once again.
158 obj = testhelper.floating_and_sunk_get_instance_list()[0]
159 self.assertEquals(obj.__grefcount__, 2)
162 self.assertEquals(obj.__grefcount__, 1)
164 class A(gobject.GObject):
166 super(A, self).__init__()
168 class TestPythonReferenceCounting(unittest.TestCase):
169 # Newly created instances should alwayshave two references: one for
170 # the GC, and one for the bound variable in the local scope.
172 def testNewInstanceHasTwoRefs(self):
173 obj = gobject.GObject()
174 self.assertEquals(sys.getrefcount(obj), 2)
176 def testNewInstanceHasTwoRefsUsingGObjectNew(self):
177 obj = gobject.new(gobject.GObject)
178 self.assertEquals(sys.getrefcount(obj), 2)
180 def testNewSubclassInstanceHasTwoRefs(self):
182 self.assertEquals(sys.getrefcount(obj), 2)
184 def testNewSubclassInstanceHasTwoRefsUsingGObjectNew(self):
186 self.assertEquals(sys.getrefcount(obj), 2)