| 1 |
import unittest |
| 2 |
import weakref |
| 3 |
import gc |
| 4 |
import sys |
| 5 |
import warnings |
| 6 |
|
| 7 |
from gi import PyGIDeprecationWarning |
| 8 |
from gi.repository import GObject |
| 9 |
from gi.repository import GIMarshallingTests |
| 10 |
import testhelper |
| 11 |
|
| 12 |
try: |
| 13 |
from gi.repository import Regress |
| 14 |
except ImportError: |
| 15 |
Regress = None |
| 16 |
|
| 17 |
|
| 18 |
class StrongRef: |
| 19 |
# A class that behaves like weakref.ref but holds a strong reference. |
| 20 |
# This allows re-use of the VFuncsBase by swapping out the ObjectRef |
| 21 |
# class var with either weakref.ref or StrongRef. |
| 22 |
|
| 23 |
def __init__(self, obj): |
| 24 |
self.obj = obj |
| 25 |
|
| 26 |
def __call__(self): |
| 27 |
return self.obj |
| 28 |
|
| 29 |
|
| 30 |
class VFuncsBase(GIMarshallingTests.Object): |
| 31 |
# Class which generically implements the vfuncs used for reference counting tests |
| 32 |
# in a way that can be easily sub-classed and modified. |
| 33 |
|
| 34 |
#: Object type used by this class for testing |
| 35 |
#: This can be GObject.Object or GObject.InitiallyUnowned |
| 36 |
Object = GObject.Object |
| 37 |
|
| 38 |
#: Reference type used by this class for holding refs to in/out objects. |
| 39 |
#: This can be set to weakref.ref or StrongRef |
| 40 |
ObjectRef = weakref.ref |
| 41 |
|
| 42 |
def __init__(self): |
| 43 |
super().__init__() |
| 44 |
|
| 45 |
#: Hold ref of input or output python wrappers |
| 46 |
self.object_ref = None |
| 47 |
|
| 48 |
#: store grefcount of input object |
| 49 |
self.in_object_grefcount = None |
| 50 |
self.in_object_is_floating = None |
| 51 |
|
| 52 |
def do_vfunc_return_object_transfer_none(self): |
| 53 |
# Return an object but keep a python reference to it. |
| 54 |
obj = self.Object() |
| 55 |
self.object_ref = self.ObjectRef(obj) |
| 56 |
return obj |
| 57 |
|
| 58 |
def do_vfunc_return_object_transfer_full(self): |
| 59 |
# Return an object and hand off the reference to the caller. |
| 60 |
obj = self.Object() |
| 61 |
self.object_ref = self.ObjectRef(obj) |
| 62 |
return obj |
| 63 |
|
| 64 |
def do_vfunc_out_object_transfer_none(self): |
| 65 |
# Same as do_vfunc_return_object_transfer_none but the pygi |
| 66 |
# internals convert the return here into an out arg. |
| 67 |
obj = self.Object() |
| 68 |
self.object_ref = self.ObjectRef(obj) |
| 69 |
return obj |
| 70 |
|
| 71 |
def do_vfunc_out_object_transfer_full(self): |
| 72 |
# Same as do_vfunc_return_object_transfer_full but the pygi |
| 73 |
# internals convert the return here into an out arg. |
| 74 |
obj = self.Object() |
| 75 |
self.object_ref = self.ObjectRef(obj) |
| 76 |
return obj |
| 77 |
|
| 78 |
def do_vfunc_in_object_transfer_none(self, obj): |
| 79 |
# 'obj' will have a python wrapper as well as still held |
| 80 |
# by the caller. |
| 81 |
self.object_ref = self.ObjectRef(obj) |
| 82 |
self.in_object_grefcount = obj.__grefcount__ |
| 83 |
self.in_object_is_floating = obj.is_floating() |
| 84 |
|
| 85 |
def do_vfunc_in_object_transfer_full(self, obj): |
| 86 |
# 'obj' will now be owned by the Python GObject wrapper. |
| 87 |
# When obj goes out of scope and is collected, the GObject |
| 88 |
# should also be fully released. |
| 89 |
self.object_ref = self.ObjectRef(obj) |
| 90 |
self.in_object_grefcount = obj.__grefcount__ |
| 91 |
self.in_object_is_floating = obj.is_floating() |
| 92 |
|
| 93 |
|
| 94 |
class TestVFuncsWithObjectArg(unittest.TestCase): |
| 95 |
# Basic set of tests which work on non-floating objects which python does |
| 96 |
# not keep an additional reference of. |
| 97 |
|
| 98 |
class VFuncs(VFuncsBase): |
| 99 |
# Object for testing non-floating objects without holding any refs. |
| 100 |
Object = GObject.Object |
| 101 |
ObjectRef = weakref.ref |
| 102 |
|
| 103 |
def test_vfunc_self_arg_ref_count(self): |
| 104 |
# Check to make sure vfunc "self" arguments don't leak. |
| 105 |
vfuncs = self.VFuncs() |
| 106 |
vfuncs_ref = weakref.ref(vfuncs) |
| 107 |
vfuncs.get_ref_info_for_vfunc_return_object_transfer_full() # Use any vfunc to test this. |
| 108 |
|
| 109 |
gc.collect() |
| 110 |
if hasattr(sys, "getrefcount"): |
| 111 |
self.assertEqual( |
| 112 |
sys.getrefcount(vfuncs), 2 if sys.version_info < (3, 14) else 1 |
| 113 |
) |
| 114 |
self.assertEqual(vfuncs.__grefcount__, 1) |
| 115 |
|
| 116 |
del vfuncs |
| 117 |
gc.collect() |
| 118 |
self.assertIsNone(vfuncs_ref()) |
| 119 |
|
| 120 |
def test_vfunc_return_object_transfer_none(self): |
| 121 |
# This tests a problem case where the vfunc returns a GObject owned solely by Python |
| 122 |
# but the argument is marked as transfer-none. |
| 123 |
# In this case pygobject marshaling adds an additional ref and gives a warning |
| 124 |
# of a potential leak. If this occures it is really a bug in the underlying library |
| 125 |
# but pygobject tries to react to this in a reasonable way. |
| 126 |
vfuncs = self.VFuncs() |
| 127 |
with warnings.catch_warnings(record=True): |
| 128 |
warnings.simplefilter("always") |
| 129 |
ref_count, is_floating = ( |
| 130 |
vfuncs.get_ref_info_for_vfunc_return_object_transfer_none() |
| 131 |
) |
| 132 |
|
| 133 |
# The ref count of the GObject returned to the caller (get_ref_info_for_vfunc_return_object_transfer_none) |
| 134 |
# should be a single ref |
| 135 |
if hasattr(sys, "getrefcount"): |
| 136 |
self.assertEqual(ref_count, 1) |
| 137 |
self.assertFalse(is_floating) |
| 138 |
|
| 139 |
gc.collect() |
| 140 |
self.assertTrue(vfuncs.object_ref() is None) |
| 141 |
|
| 142 |
def test_vfunc_out_object_transfer_none(self): |
| 143 |
# Same as above except uses out arg instead of return |
| 144 |
vfuncs = self.VFuncs() |
| 145 |
with warnings.catch_warnings(record=True): |
| 146 |
warnings.simplefilter("always") |
| 147 |
ref_count, is_floating = ( |
| 148 |
vfuncs.get_ref_info_for_vfunc_out_object_transfer_none() |
| 149 |
) |
| 150 |
|
| 151 |
if hasattr(sys, "getrefcount"): |
| 152 |
self.assertEqual(ref_count, 1) |
| 153 |
self.assertFalse(is_floating) |
| 154 |
|
| 155 |
gc.collect() |
| 156 |
self.assertTrue(vfuncs.object_ref() is None) |
| 157 |
|
| 158 |
def test_vfunc_return_object_transfer_full(self): |
| 159 |
vfuncs = self.VFuncs() |
| 160 |
ref_count, is_floating = ( |
| 161 |
vfuncs.get_ref_info_for_vfunc_return_object_transfer_full() |
| 162 |
) |
| 163 |
|
| 164 |
# The vfunc caller receives full ownership of a single ref which should not |
| 165 |
# be floating. |
| 166 |
if hasattr(sys, "getrefcount"): |
| 167 |
self.assertEqual(ref_count, 1) |
| 168 |
self.assertFalse(is_floating) |
| 169 |
|
| 170 |
gc.collect() |
| 171 |
self.assertTrue(vfuncs.object_ref() is None) |
| 172 |
|
| 173 |
def test_vfunc_out_object_transfer_full(self): |
| 174 |
# Same as above except uses out arg instead of return |
| 175 |
vfuncs = self.VFuncs() |
| 176 |
ref_count, is_floating = ( |
| 177 |
vfuncs.get_ref_info_for_vfunc_out_object_transfer_full() |
| 178 |
) |
| 179 |
|
| 180 |
if hasattr(sys, "getrefcount"): |
| 181 |
self.assertEqual(ref_count, 1) |
| 182 |
self.assertFalse(is_floating) |
| 183 |
|
| 184 |
gc.collect() |
| 185 |
self.assertTrue(vfuncs.object_ref() is None) |
| 186 |
|
| 187 |
def test_vfunc_in_object_transfer_none(self): |
| 188 |
vfuncs = self.VFuncs() |
| 189 |
ref_count, is_floating = vfuncs.get_ref_info_for_vfunc_in_object_transfer_none( |
| 190 |
self.VFuncs.Object |
| 191 |
) |
| 192 |
|
| 193 |
gc.collect() |
| 194 |
self.assertEqual(vfuncs.in_object_grefcount, 2) # initial + python wrapper |
| 195 |
self.assertFalse(vfuncs.in_object_is_floating) |
| 196 |
|
| 197 |
if hasattr(sys, "getrefcount"): |
| 198 |
self.assertEqual(ref_count, 1) # ensure python wrapper released |
| 199 |
self.assertFalse(is_floating) |
| 200 |
|
| 201 |
gc.collect() |
| 202 |
self.assertTrue(vfuncs.object_ref() is None) |
| 203 |
|
| 204 |
def test_vfunc_in_object_transfer_full(self): |
| 205 |
vfuncs = self.VFuncs() |
| 206 |
ref_count, is_floating = vfuncs.get_ref_info_for_vfunc_in_object_transfer_full( |
| 207 |
self.VFuncs.Object |
| 208 |
) |
| 209 |
|
| 210 |
gc.collect() |
| 211 |
|
| 212 |
# python wrapper should take sole ownership of the gobject |
| 213 |
self.assertEqual(vfuncs.in_object_grefcount, 1) |
| 214 |
self.assertFalse(vfuncs.in_object_is_floating) |
| 215 |
|
| 216 |
# ensure python wrapper took ownership and released, after vfunc was complete |
| 217 |
if hasattr(sys, "getrefcount"): |
| 218 |
self.assertEqual(ref_count, 0) |
| 219 |
self.assertFalse(is_floating) |
| 220 |
|
| 221 |
gc.collect() |
| 222 |
self.assertTrue(vfuncs.object_ref() is None) |
| 223 |
|
| 224 |
|
| 225 |
class TestVFuncsWithFloatingArg(unittest.TestCase): |
| 226 |
# All tests here work with a floating object by using InitiallyUnowned as the argument |
| 227 |
# The floating object is sunk as soon as possible. |
| 228 |
|
| 229 |
class VFuncs(VFuncsBase): |
| 230 |
# Object for testing non-floating objects without holding any refs. |
| 231 |
Object = GObject.InitiallyUnowned |
| 232 |
ObjectRef = weakref.ref |
| 233 |
|
| 234 |
@unittest.skipUnless(hasattr(sys, "getrefcount"), "refcount specific") |
| 235 |
def test_vfunc_return_object_transfer_none_with_floating(self): |
| 236 |
# See TestVFuncWithObjectArg.test_vfunc_return_object_transfer_none for an explaination. |
| 237 |
vfuncs = self.VFuncs() |
| 238 |
with warnings.catch_warnings(record=True): |
| 239 |
warnings.simplefilter("always") |
| 240 |
ref_count, is_floating = ( |
| 241 |
vfuncs.get_ref_info_for_vfunc_return_object_transfer_none() |
| 242 |
) |
| 243 |
|
| 244 |
# The ref count of the GObject returned to the caller (get_ref_info_for_vfunc_return_object_transfer_none) |
| 245 |
# should be a single ref |
| 246 |
self.assertEqual(ref_count, 1) |
| 247 |
self.assertFalse(is_floating) |
| 248 |
|
| 249 |
gc.collect() |
| 250 |
self.assertTrue(vfuncs.object_ref() is None) |
| 251 |
|
| 252 |
@unittest.skipUnless(hasattr(sys, "getrefcount"), "refcount specific") |
| 253 |
def test_vfunc_out_object_transfer_none_with_floating(self): |
| 254 |
# Same as above except uses out arg instead of return |
| 255 |
vfuncs = self.VFuncs() |
| 256 |
with warnings.catch_warnings(record=True): |
| 257 |
warnings.simplefilter("always") |
| 258 |
ref_count, is_floating = ( |
| 259 |
vfuncs.get_ref_info_for_vfunc_out_object_transfer_none() |
| 260 |
) |
| 261 |
|
| 262 |
self.assertEqual(ref_count, 1) |
| 263 |
self.assertFalse(is_floating) |
| 264 |
|
| 265 |
gc.collect() |
| 266 |
self.assertTrue(vfuncs.object_ref() is None) |
| 267 |
|
| 268 |
def test_vfunc_return_object_transfer_full_with_floating(self): |
| 269 |
vfuncs = self.VFuncs() |
| 270 |
ref_count, is_floating = ( |
| 271 |
vfuncs.get_ref_info_for_vfunc_return_object_transfer_full() |
| 272 |
) |
| 273 |
|
| 274 |
# The vfunc caller receives full ownership of a single ref. |
| 275 |
if hasattr(sys, "getrefcount"): |
| 276 |
self.assertEqual(ref_count, 1) |
| 277 |
self.assertFalse(is_floating) |
| 278 |
|
| 279 |
gc.collect() |
| 280 |
self.assertTrue(vfuncs.object_ref() is None) |
| 281 |
|
| 282 |
def test_vfunc_out_object_transfer_full_with_floating(self): |
| 283 |
# Same as above except uses out arg instead of return |
| 284 |
vfuncs = self.VFuncs() |
| 285 |
ref_count, is_floating = ( |
| 286 |
vfuncs.get_ref_info_for_vfunc_out_object_transfer_full() |
| 287 |
) |
| 288 |
|
| 289 |
if hasattr(sys, "getrefcount"): |
| 290 |
self.assertEqual(ref_count, 1) |
| 291 |
self.assertFalse(is_floating) |
| 292 |
|
| 293 |
gc.collect() |
| 294 |
self.assertTrue(vfuncs.object_ref() is None) |
| 295 |
|
| 296 |
def test_vfunc_in_object_transfer_none_with_floating(self): |
| 297 |
vfuncs = self.VFuncs() |
| 298 |
|
| 299 |
def ref(obj): |
| 300 |
testhelper.force_g_object_ref(obj) |
| 301 |
|
| 302 |
# NB. weak reference the underlying GObject! |
| 303 |
return obj.weak_ref() |
| 304 |
|
| 305 |
vfuncs.ObjectRef = ref |
| 306 |
|
| 307 |
ref_count, is_floating = vfuncs.get_ref_info_for_vfunc_in_object_transfer_none( |
| 308 |
self.VFuncs.Object |
| 309 |
) |
| 310 |
gc.collect() |
| 311 |
|
| 312 |
# New: all objects are "sink"'ed (sunken?). Avoid using floating objects altogether. |
| 313 |
|
| 314 |
# python wrapper should maintain the object as floating and add an additional ref |
| 315 |
# So, two plus the one we added explicitly. |
| 316 |
self.assertEqual(vfuncs.in_object_grefcount, 2 + 1) |
| 317 |
self.assertFalse(vfuncs.in_object_is_floating) |
| 318 |
|
| 319 |
# Special case for PyPy: the GC has not run, so we have registered an extra reference |
| 320 |
# Add a call to `PyGC_Collect ()` at the end of `_pygi_closure_handle` to check. |
| 321 |
pypy_needs_gc = int(sys.implementation.name == "pypy") |
| 322 |
self.assertEqual(ref_count, 2 + pypy_needs_gc) |
| 323 |
self.assertFalse(is_floating) |
| 324 |
|
| 325 |
# There are two references now, one explicit and one from the wrapper |
| 326 |
self.assertEqual(vfuncs.object_ref().__grefcount__, 2) |
| 327 |
self.assertFalse(vfuncs.object_ref().is_floating()) |
| 328 |
|
| 329 |
testhelper.force_g_object_unref(vfuncs.object_ref()) |
| 330 |
|
| 331 |
gc.collect() |
| 332 |
assert vfuncs.object_ref() is None |
| 333 |
|
| 334 |
def test_vfunc_in_object_transfer_full_with_floating(self): |
| 335 |
vfuncs = self.VFuncs() |
| 336 |
ref_count, is_floating = vfuncs.get_ref_info_for_vfunc_in_object_transfer_full( |
| 337 |
self.VFuncs.Object |
| 338 |
) |
| 339 |
|
| 340 |
gc.collect() |
| 341 |
|
| 342 |
# python wrapper sinks and owns the gobject |
| 343 |
self.assertEqual(vfuncs.in_object_grefcount, 1) |
| 344 |
self.assertFalse(vfuncs.in_object_is_floating) |
| 345 |
|
| 346 |
# ensure python wrapper took ownership and released |
| 347 |
if hasattr(sys, "getrefcount"): |
| 348 |
self.assertEqual(ref_count, 0) |
| 349 |
self.assertFalse(is_floating) |
| 350 |
|
| 351 |
gc.collect() |
| 352 |
self.assertTrue(vfuncs.object_ref() is None) |
| 353 |
|
| 354 |
|
| 355 |
class TestVFuncsWithHeldObjectArg(unittest.TestCase): |
| 356 |
# Same tests as TestVFuncsWithObjectArg except we hold |
| 357 |
# onto the python object reference in all cases. |
| 358 |
|
| 359 |
class VFuncs(VFuncsBase): |
| 360 |
# Object for testing non-floating objects with a held ref. |
| 361 |
Object = GObject.Object |
| 362 |
ObjectRef = StrongRef |
| 363 |
|
| 364 |
def test_vfunc_return_object_transfer_none_with_held_object(self): |
| 365 |
vfuncs = self.VFuncs() |
| 366 |
ref_count, is_floating = ( |
| 367 |
vfuncs.get_ref_info_for_vfunc_return_object_transfer_none() |
| 368 |
) |
| 369 |
|
| 370 |
# Python holds the single gobject ref in 'vfuncs.object_ref' |
| 371 |
# Because of this, we do not expect a floating ref or a ref increase. |
| 372 |
self.assertEqual(ref_count, 1) |
| 373 |
self.assertFalse(is_floating) |
| 374 |
|
| 375 |
# The actual grefcount should stay at 1 even after the vfunc return. |
| 376 |
self.assertEqual(vfuncs.object_ref().__grefcount__, 1) |
| 377 |
self.assertFalse(vfuncs.in_object_is_floating) |
| 378 |
|
| 379 |
held_object_ref = weakref.ref(vfuncs.object_ref) |
| 380 |
del vfuncs.object_ref |
| 381 |
gc.collect() |
| 382 |
self.assertTrue(held_object_ref() is None) |
| 383 |
|
| 384 |
def test_vfunc_out_object_transfer_none_with_held_object(self): |
| 385 |
vfuncs = self.VFuncs() |
| 386 |
ref_count, is_floating = ( |
| 387 |
vfuncs.get_ref_info_for_vfunc_out_object_transfer_none() |
| 388 |
) |
| 389 |
|
| 390 |
self.assertEqual(ref_count, 1) |
| 391 |
self.assertFalse(is_floating) |
| 392 |
|
| 393 |
self.assertEqual(vfuncs.object_ref().__grefcount__, 1) |
| 394 |
self.assertFalse(vfuncs.in_object_is_floating) |
| 395 |
|
| 396 |
held_object_ref = weakref.ref(vfuncs.object_ref) |
| 397 |
del vfuncs.object_ref |
| 398 |
gc.collect() |
| 399 |
self.assertTrue(held_object_ref() is None) |
| 400 |
|
| 401 |
def test_vfunc_return_object_transfer_full_with_held_object(self): |
| 402 |
# The vfunc caller receives full ownership which should not |
| 403 |
# be floating. However, the held python wrapper also has a ref. |
| 404 |
|
| 405 |
vfuncs = self.VFuncs() |
| 406 |
ref_count, is_floating = ( |
| 407 |
vfuncs.get_ref_info_for_vfunc_return_object_transfer_full() |
| 408 |
) |
| 409 |
|
| 410 |
# Ref count from the perspective of C after the vfunc is called |
| 411 |
# The vfunc caller receives a new reference which should not |
| 412 |
# be floating. However, the held python wrapper also has a ref. |
| 413 |
self.assertEqual(ref_count, 2) |
| 414 |
self.assertFalse(is_floating) |
| 415 |
|
| 416 |
# Current ref count |
| 417 |
# The vfunc caller should have decremented its reference. |
| 418 |
self.assertEqual(vfuncs.object_ref().__grefcount__, 1) |
| 419 |
|
| 420 |
held_object_ref = weakref.ref(vfuncs.object_ref) |
| 421 |
del vfuncs.object_ref |
| 422 |
gc.collect() |
| 423 |
self.assertTrue(held_object_ref() is None) |
| 424 |
|
| 425 |
def test_vfunc_out_object_transfer_full_with_held_object(self): |
| 426 |
# Same test as above except uses out arg instead of return |
| 427 |
vfuncs = self.VFuncs() |
| 428 |
ref_count, is_floating = ( |
| 429 |
vfuncs.get_ref_info_for_vfunc_out_object_transfer_full() |
| 430 |
) |
| 431 |
|
| 432 |
# Ref count from the perspective of C after the vfunc is called |
| 433 |
# The vfunc caller receives a new reference which should not |
| 434 |
# be floating. However, the held python wrapper also has a ref. |
| 435 |
self.assertEqual(ref_count, 2) |
| 436 |
self.assertFalse(is_floating) |
| 437 |
|
| 438 |
# Current ref count |
| 439 |
# The vfunc caller should have decremented its reference. |
| 440 |
self.assertEqual(vfuncs.object_ref().__grefcount__, 1) |
| 441 |
|
| 442 |
held_object_ref = weakref.ref(vfuncs.object_ref()) |
| 443 |
del vfuncs.object_ref |
| 444 |
gc.collect() |
| 445 |
self.assertTrue(held_object_ref() is None) |
| 446 |
|
| 447 |
def test_vfunc_in_object_transfer_none_with_held_object(self): |
| 448 |
vfuncs = self.VFuncs() |
| 449 |
ref_count, is_floating = vfuncs.get_ref_info_for_vfunc_in_object_transfer_none( |
| 450 |
self.VFuncs.Object |
| 451 |
) |
| 452 |
|
| 453 |
gc.collect() |
| 454 |
|
| 455 |
# Ref count inside vfunc from the perspective of Python |
| 456 |
self.assertEqual(vfuncs.in_object_grefcount, 2) # initial + python wrapper |
| 457 |
self.assertFalse(vfuncs.in_object_is_floating) |
| 458 |
|
| 459 |
# Ref count from the perspective of C after the vfunc is called |
| 460 |
self.assertEqual(ref_count, 2) # kept after vfunc + held python wrapper |
| 461 |
self.assertFalse(is_floating) |
| 462 |
|
| 463 |
# Current ref count after C cleans up its reference |
| 464 |
self.assertEqual(vfuncs.object_ref().__grefcount__, 1) |
| 465 |
|
| 466 |
held_object_ref = weakref.ref(vfuncs.object_ref()) |
| 467 |
del vfuncs.object_ref |
| 468 |
gc.collect() |
| 469 |
self.assertTrue(held_object_ref() is None) |
| 470 |
|
| 471 |
def test_vfunc_in_object_transfer_full_with_held_object(self): |
| 472 |
vfuncs = self.VFuncs() |
| 473 |
ref_count, is_floating = vfuncs.get_ref_info_for_vfunc_in_object_transfer_full( |
| 474 |
self.VFuncs.Object |
| 475 |
) |
| 476 |
|
| 477 |
gc.collect() |
| 478 |
|
| 479 |
# Ref count inside vfunc from the perspective of Python |
| 480 |
self.assertEqual( |
| 481 |
vfuncs.in_object_grefcount, 1 |
| 482 |
) # python wrapper takes ownership of the gobject |
| 483 |
self.assertFalse(vfuncs.in_object_is_floating) |
| 484 |
|
| 485 |
# Ref count from the perspective of C after the vfunc is called |
| 486 |
self.assertEqual(ref_count, 1) |
| 487 |
self.assertFalse(is_floating) |
| 488 |
|
| 489 |
# Current ref count |
| 490 |
self.assertEqual(vfuncs.object_ref().__grefcount__, 1) |
| 491 |
|
| 492 |
held_object_ref = weakref.ref(vfuncs.object_ref()) |
| 493 |
del vfuncs.object_ref |
| 494 |
gc.collect() |
| 495 |
self.assertTrue(held_object_ref() is None) |
| 496 |
|
| 497 |
|
| 498 |
class TestVFuncsWithHeldFloatingArg(unittest.TestCase): |
| 499 |
# Tests for a floating object which we hold a reference to the python wrapper |
| 500 |
# on the VFuncs test class. |
| 501 |
|
| 502 |
class VFuncs(VFuncsBase): |
| 503 |
# Object for testing floating objects with a held ref. |
| 504 |
Object = GObject.InitiallyUnowned |
| 505 |
ObjectRef = StrongRef |
| 506 |
|
| 507 |
def test_vfunc_return_object_transfer_none_with_held_floating(self): |
| 508 |
# Python holds onto the wrapper which basically means the floating ref |
| 509 |
# should also be owned by python. |
| 510 |
|
| 511 |
vfuncs = self.VFuncs() |
| 512 |
ref_count, is_floating = ( |
| 513 |
vfuncs.get_ref_info_for_vfunc_return_object_transfer_none() |
| 514 |
) |
| 515 |
|
| 516 |
# This is a borrowed ref from what is held in python. |
| 517 |
self.assertEqual(ref_count, 1) |
| 518 |
self.assertFalse(is_floating) |
| 519 |
|
| 520 |
# The actual grefcount should stay at 1 even after the vfunc return. |
| 521 |
self.assertEqual(vfuncs.object_ref().__grefcount__, 1) |
| 522 |
|
| 523 |
held_object_ref = weakref.ref(vfuncs.object_ref) |
| 524 |
del vfuncs.object_ref |
| 525 |
gc.collect() |
| 526 |
self.assertTrue(held_object_ref() is None) |
| 527 |
|
| 528 |
def test_vfunc_out_object_transfer_none_with_held_floating(self): |
| 529 |
# Same as above |
| 530 |
|
| 531 |
vfuncs = self.VFuncs() |
| 532 |
ref_count, is_floating = ( |
| 533 |
vfuncs.get_ref_info_for_vfunc_out_object_transfer_none() |
| 534 |
) |
| 535 |
|
| 536 |
self.assertEqual(ref_count, 1) |
| 537 |
self.assertFalse(is_floating) |
| 538 |
|
| 539 |
self.assertEqual(vfuncs.object_ref().__grefcount__, 1) |
| 540 |
|
| 541 |
held_object_ref = weakref.ref(vfuncs.object_ref) |
| 542 |
del vfuncs.object_ref |
| 543 |
gc.collect() |
| 544 |
self.assertTrue(held_object_ref() is None) |
| 545 |
|
| 546 |
def test_vfunc_return_object_transfer_full_with_held_floating(self): |
| 547 |
vfuncs = self.VFuncs() |
| 548 |
ref_count, is_floating = ( |
| 549 |
vfuncs.get_ref_info_for_vfunc_return_object_transfer_full() |
| 550 |
) |
| 551 |
|
| 552 |
# Ref count from the perspective of C after the vfunc is called |
| 553 |
self.assertEqual(ref_count, 2) |
| 554 |
self.assertFalse(is_floating) |
| 555 |
|
| 556 |
# Current ref count |
| 557 |
# vfunc wrapper destroyes ref it was given |
| 558 |
self.assertEqual(vfuncs.object_ref().__grefcount__, 1) |
| 559 |
|
| 560 |
held_object_ref = weakref.ref(vfuncs.object_ref) |
| 561 |
del vfuncs.object_ref |
| 562 |
gc.collect() |
| 563 |
self.assertTrue(held_object_ref() is None) |
| 564 |
|
| 565 |
def test_vfunc_out_object_transfer_full_with_held_floating(self): |
| 566 |
# Same test as above except uses out arg instead of return |
| 567 |
vfuncs = self.VFuncs() |
| 568 |
ref_count, is_floating = ( |
| 569 |
vfuncs.get_ref_info_for_vfunc_out_object_transfer_full() |
| 570 |
) |
| 571 |
|
| 572 |
# Ref count from the perspective of C after the vfunc is called |
| 573 |
self.assertEqual(ref_count, 2) |
| 574 |
self.assertFalse(is_floating) |
| 575 |
|
| 576 |
# Current ref count |
| 577 |
# vfunc wrapper destroyes ref it was given |
| 578 |
self.assertEqual(vfuncs.object_ref().__grefcount__, 1) |
| 579 |
|
| 580 |
held_object_ref = weakref.ref(vfuncs.object_ref()) |
| 581 |
del vfuncs.object_ref |
| 582 |
gc.collect() |
| 583 |
self.assertTrue(held_object_ref() is None) |
| 584 |
|
| 585 |
def test_vfunc_in_floating_transfer_none_with_held_floating(self): |
| 586 |
vfuncs = self.VFuncs() |
| 587 |
ref_count, is_floating = vfuncs.get_ref_info_for_vfunc_in_object_transfer_none( |
| 588 |
self.VFuncs.Object |
| 589 |
) |
| 590 |
gc.collect() |
| 591 |
|
| 592 |
# Ref count inside vfunc from the perspective of Python |
| 593 |
self.assertFalse(vfuncs.in_object_is_floating) |
| 594 |
self.assertEqual( |
| 595 |
vfuncs.in_object_grefcount, 2 |
| 596 |
) # python wrapper sinks and owns the gobject |
| 597 |
|
| 598 |
# Ref count from the perspective of C after the vfunc is called |
| 599 |
self.assertFalse(is_floating) |
| 600 |
self.assertEqual(ref_count, 2) # floating + held by wrapper |
| 601 |
|
| 602 |
# Current ref count after C cleans up its reference |
| 603 |
self.assertEqual(vfuncs.object_ref().__grefcount__, 1) |
| 604 |
|
| 605 |
# Our reference is still floating at this point ... |
| 606 |
self.assertFalse(vfuncs.object_ref().is_floating()) |
| 607 |
# ... usually it should have been sunk within C at some point, |
| 608 |
# do it here to avoid a critical warning. |
| 609 |
with warnings.catch_warnings(): |
| 610 |
warnings.simplefilter("ignore", PyGIDeprecationWarning) |
| 611 |
vfuncs.object_ref()._ref_sink() |
| 612 |
# However, that means that the above comment of "sinks and owns" is wrong. |
| 613 |
|
| 614 |
held_object_ref = weakref.ref(vfuncs.object_ref()) |
| 615 |
del vfuncs.object_ref |
| 616 |
gc.collect() |
| 617 |
self.assertTrue(held_object_ref() is None) |
| 618 |
|
| 619 |
def test_vfunc_in_floating_transfer_full_with_held_floating(self): |
| 620 |
vfuncs = self.VFuncs() |
| 621 |
ref_count, is_floating = vfuncs.get_ref_info_for_vfunc_in_object_transfer_full( |
| 622 |
self.VFuncs.Object |
| 623 |
) |
| 624 |
gc.collect() |
| 625 |
|
| 626 |
# Ref count from the perspective of C after the vfunc is called |
| 627 |
self.assertEqual( |
| 628 |
vfuncs.in_object_grefcount, 1 |
| 629 |
) # python wrapper sinks and owns the gobject |
| 630 |
self.assertFalse(vfuncs.in_object_is_floating) |
| 631 |
|
| 632 |
# Ref count from the perspective of C after the vfunc is called |
| 633 |
self.assertEqual(ref_count, 1) # held by wrapper |
| 634 |
self.assertFalse(is_floating) |
| 635 |
|
| 636 |
# Current ref count |
| 637 |
self.assertEqual(vfuncs.object_ref().__grefcount__, 1) |
| 638 |
|
| 639 |
held_object_ref = weakref.ref(vfuncs.object_ref()) |
| 640 |
del vfuncs.object_ref |
| 641 |
gc.collect() |
| 642 |
self.assertTrue(held_object_ref() is None) |
| 643 |
|
| 644 |
|
| 645 |
class TestArgumentTypeErrors(unittest.TestCase): |
| 646 |
def test_object_argument_type_error(self): |
| 647 |
# ensure TypeError is raised for things which are not GObjects |
| 648 |
obj = Regress.TestObj() |
| 649 |
obj.set_bare(GObject.Object()) |
| 650 |
obj.set_bare(None) |
| 651 |
|
| 652 |
self.assertRaises(TypeError, obj.set_bare, object()) |
| 653 |
self.assertRaises(TypeError, obj.set_bare, 42) |
| 654 |
self.assertRaises(TypeError, obj.set_bare, "not an object") |
| 655 |
|
| 656 |
def test_instance_argument_error(self): |
| 657 |
# ensure TypeError is raised for non Regress.TestObj instances. |
| 658 |
obj = Regress.TestObj() |
| 659 |
self.assertEqual(Regress.TestObj.instance_method(obj), -1) |
| 660 |
self.assertRaises(TypeError, Regress.TestObj.instance_method, object()) |
| 661 |
self.assertRaises(TypeError, Regress.TestObj.instance_method, GObject.Object()) |
| 662 |
self.assertRaises(TypeError, Regress.TestObj.instance_method, 42) |
| 663 |
self.assertRaises(TypeError, Regress.TestObj.instance_method, "not an object") |
| 664 |
|
| 665 |
def test_instance_argument_base_type_error(self): |
| 666 |
# ensure TypeError is raised when a base type is passed to something |
| 667 |
# expecting a derived type |
| 668 |
obj = Regress.TestSubObj() |
| 669 |
self.assertEqual(Regress.TestSubObj.instance_method(obj), 0) |
| 670 |
self.assertRaises( |
| 671 |
TypeError, Regress.TestSubObj.instance_method, GObject.Object() |
| 672 |
) |
| 673 |
self.assertRaises( |
| 674 |
TypeError, Regress.TestSubObj.instance_method, Regress.TestObj() |
| 675 |
) |