| 1 |
import gc |
| 2 |
import sys |
| 3 |
import warnings |
| 4 |
import weakref |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
from gi import PyGIWarning |
| 9 |
from gi.repository import GObject, Gio, Regress |
| 10 |
|
| 11 |
|
| 12 |
class DerivedObj(Regress.TestObj): |
| 13 |
def __init__(self): |
| 14 |
super().__init__() |
| 15 |
|
| 16 |
|
| 17 |
@pytest.mark.parametrize("obj_type", [Regress.TestObj, DerivedObj]) |
| 18 |
def test_same_object_if_object_is_alive(obj_type): |
| 19 |
container = Regress.TestObj() |
| 20 |
obj = obj_type() |
| 21 |
container.set_bare(obj) |
| 22 |
|
| 23 |
assert container.props.bare is obj |
| 24 |
|
| 25 |
|
| 26 |
@pytest.mark.parametrize("obj_type", [Regress.TestObj, DerivedObj]) |
| 27 |
def test_object_without_instance_data_gets_deleted(obj_type): |
| 28 |
container = Regress.TestObj() |
| 29 |
obj = obj_type() |
| 30 |
container.props.bare = obj |
| 31 |
|
| 32 |
ref = weakref.ref(obj) |
| 33 |
del obj |
| 34 |
gc.collect() # for pypy |
| 35 |
|
| 36 |
assert container.props.bare |
| 37 |
assert ref() is None |
| 38 |
|
| 39 |
|
| 40 |
@pytest.mark.parametrize("obj_type", [Regress.TestObj, DerivedObj]) |
| 41 |
def test_object_with_instance_data_gets_deleted(obj_type): |
| 42 |
container = Regress.TestObj() |
| 43 |
obj = obj_type() |
| 44 |
obj.instance_data = 1 |
| 45 |
container.props.bare = obj |
| 46 |
|
| 47 |
ref = weakref.ref(obj) |
| 48 |
del obj |
| 49 |
gc.collect() # for pypy |
| 50 |
|
| 51 |
assert ref() is None |
| 52 |
|
| 53 |
|
| 54 |
@pytest.mark.parametrize("obj_type", [Regress.TestObj, DerivedObj]) |
| 55 |
def test_object_with_instance_data_retains_data(obj_type): |
| 56 |
container = Regress.TestObj() |
| 57 |
obj = obj_type() |
| 58 |
obj.instance_data = 1 |
| 59 |
container.props.bare = obj |
| 60 |
|
| 61 |
del obj |
| 62 |
new_obj = container.props.bare |
| 63 |
|
| 64 |
assert new_obj.instance_data == 1 |
| 65 |
|
| 66 |
|
| 67 |
class ValueObj(Regress.TestObj): |
| 68 |
py_int = GObject.Property(type=int) |
| 69 |
|
| 70 |
py_model = GObject.Property(type=Gio.ListModel) |
| 71 |
|
| 72 |
def __init__(self, value=0): |
| 73 |
super().__init__() |
| 74 |
self.value = value |
| 75 |
|
| 76 |
|
| 77 |
def test_value_object_retains_init_value(): |
| 78 |
container = Regress.TestObj() |
| 79 |
obj = ValueObj(42) |
| 80 |
container.props.bare = obj |
| 81 |
|
| 82 |
del obj |
| 83 |
gc.collect() # for pypy |
| 84 |
new_obj = container.props.bare |
| 85 |
|
| 86 |
assert new_obj.value == 42 |
| 87 |
|
| 88 |
|
| 89 |
def test_value_object_retains_property_value(): |
| 90 |
container = Regress.TestObj() |
| 91 |
obj = ValueObj() |
| 92 |
obj.py_int = 42 |
| 93 |
container.props.bare = obj |
| 94 |
|
| 95 |
del obj |
| 96 |
gc.collect() # for pypy |
| 97 |
new_obj = container.props.bare |
| 98 |
|
| 99 |
assert new_obj.py_int == 42 |
| 100 |
|
| 101 |
|
| 102 |
class MySpecialListStore(GObject.Object, Gio.ListModel): |
| 103 |
def __init__(self): |
| 104 |
super().__init__() |
| 105 |
|
| 106 |
def do_get_item_type(self): |
| 107 |
return Regress.TestObj.__gtype__ |
| 108 |
|
| 109 |
def do_get_n_items(self): |
| 110 |
return 0 |
| 111 |
|
| 112 |
def do_get_item(self, n): |
| 113 |
raise NotImplementedError |
| 114 |
|
| 115 |
|
| 116 |
def test_value_object_retains_object_property_value(): |
| 117 |
container = Regress.TestObj() |
| 118 |
obj = ValueObj() |
| 119 |
obj.model = MySpecialListStore() |
| 120 |
container.props.bare = obj |
| 121 |
|
| 122 |
del obj |
| 123 |
gc.collect() # for pypy |
| 124 |
new_obj = container.props.bare |
| 125 |
|
| 126 |
assert new_obj.model is not None |
| 127 |
|
| 128 |
|
| 129 |
def test_object_with_property_binding(): |
| 130 |
containers = [Regress.TestObj(), Regress.TestObj()] |
| 131 |
binder = Regress.TestObj() |
| 132 |
bindee = Regress.TestObj() |
| 133 |
|
| 134 |
binder.bind_property("bare", bindee, "bare", GObject.BindingFlags.SYNC_CREATE) |
| 135 |
containers[0].props.bare = binder |
| 136 |
containers[1].props.bare = bindee |
| 137 |
|
| 138 |
del binder |
| 139 |
del bindee |
| 140 |
|
| 141 |
gc.collect() # for pypy |
| 142 |
|
| 143 |
new_binder = containers[0].props.bare |
| 144 |
new_bindee = containers[1].props.bare |
| 145 |
|
| 146 |
obj = Regress.TestObj() |
| 147 |
|
| 148 |
new_binder.props.bare = obj |
| 149 |
|
| 150 |
assert new_bindee.props.bare is obj |
| 151 |
|
| 152 |
|
| 153 |
def test_object_with_signal_callback(): |
| 154 |
container = Regress.TestObj() |
| 155 |
obj = ValueObj() |
| 156 |
|
| 157 |
def set_up_signal_handler(self): |
| 158 |
self.connect("first", lambda *_: self.set_property("int", 42)) |
| 159 |
|
| 160 |
set_up_signal_handler(obj) |
| 161 |
|
| 162 |
container.props.bare = obj |
| 163 |
obj_id = id(obj) |
| 164 |
|
| 165 |
del obj |
| 166 |
gc.collect() # for pypy |
| 167 |
new_obj = container.props.bare |
| 168 |
|
| 169 |
new_obj.emit("first") |
| 170 |
|
| 171 |
assert obj_id == id(new_obj) |
| 172 |
assert new_obj.props.int == 42 |
| 173 |
|
| 174 |
|
| 175 |
with warnings.catch_warnings(): |
| 176 |
warnings.filterwarnings("ignore", ".*use __slots__.*") |
| 177 |
|
| 178 |
class SlotObj(Regress.TestObj): |
| 179 |
__slots__ = ["py_int"] |
| 180 |
|
| 181 |
def __init__(self): |
| 182 |
super().__init__() |
| 183 |
|
| 184 |
|
| 185 |
def test_class_with_slots_raises_warning(): |
| 186 |
with warnings.catch_warnings(record=True) as warn: |
| 187 |
|
| 188 |
class _SlotClass(GObject.Object): |
| 189 |
__slots__ = ["py_int"] |
| 190 |
|
| 191 |
assert warn[0].category is PyGIWarning |
| 192 |
assert "SlotClass shouldn't use __slots__." in str(warn[0].message) |
| 193 |
|
| 194 |
|
| 195 |
def test_subclass_with_slots_raises_warning(): |
| 196 |
class BaseClass(GObject.Object): |
| 197 |
pass |
| 198 |
|
| 199 |
with warnings.catch_warnings(record=True) as warn: |
| 200 |
|
| 201 |
class _SlotClass(BaseClass): |
| 202 |
__slots__ = ["py_int"] |
| 203 |
|
| 204 |
assert warn[0].category is PyGIWarning |
| 205 |
assert "SlotClass shouldn't use __slots__." in str(warn[0].message) |
| 206 |
|
| 207 |
|
| 208 |
def test_slot_object_can_be_created(): |
| 209 |
container = Regress.TestObj() |
| 210 |
obj = SlotObj() |
| 211 |
obj.py_int = 42 |
| 212 |
container.props.bare = obj |
| 213 |
|
| 214 |
del obj |
| 215 |
gc.collect() # for pypy |
| 216 |
new_obj = container.props.bare |
| 217 |
|
| 218 |
assert new_obj.py_int == 42 |
| 219 |
|
| 220 |
|
| 221 |
def test_slot_object_without_values_can_be_created(): |
| 222 |
container = Regress.TestObj() |
| 223 |
obj = SlotObj() |
| 224 |
container.props.bare = obj |
| 225 |
|
| 226 |
del obj |
| 227 |
gc.collect() # for pypy |
| 228 |
new_obj = container.props.bare |
| 229 |
|
| 230 |
assert new_obj |
| 231 |
|
| 232 |
|
| 233 |
def test_objects_with_cyclic_dependency_and_instance_dict(): |
| 234 |
# Test with a cycle: |
| 235 |
# |
| 236 |
# a --> b --> c --> d |
| 237 |
# ^-----------' |
| 238 |
a, b, c, d = [Regress.TestObj(int=i) for i in range(4)] |
| 239 |
|
| 240 |
b.name = "b" |
| 241 |
|
| 242 |
a.props.bare = b |
| 243 |
b.props.bare = c |
| 244 |
b.c = c |
| 245 |
c.props.bare = d |
| 246 |
c.d = d |
| 247 |
d.props.bare = b |
| 248 |
d.b = b |
| 249 |
|
| 250 |
del b, c, d |
| 251 |
|
| 252 |
gc.collect() |
| 253 |
gc.collect() |
| 254 |
|
| 255 |
new_b = a.props.bare |
| 256 |
new_c = new_b.props.bare |
| 257 |
new_d = new_c.props.bare |
| 258 |
|
| 259 |
assert new_d.props.bare is new_b |
| 260 |
assert a.props.bare.name == "b" |
| 261 |
|
| 262 |
|
| 263 |
def test_objects_with_cyclic_dependency_without_instance_dict(): |
| 264 |
# Test with a cycle: |
| 265 |
# |
| 266 |
# a --> b --> c --> d |
| 267 |
# ^-----------' |
| 268 |
a, b, c, d = [Regress.TestObj(int=i) for i in range(4)] |
| 269 |
|
| 270 |
a.props.bare = b |
| 271 |
b.props.bare = c |
| 272 |
c.props.bare = d |
| 273 |
d.props.bare = b |
| 274 |
|
| 275 |
del b, c, d |
| 276 |
|
| 277 |
gc.collect() |
| 278 |
gc.collect() |
| 279 |
|
| 280 |
new_b = a.props.bare |
| 281 |
new_c = new_b.props.bare |
| 282 |
new_d = new_c.props.bare |
| 283 |
|
| 284 |
assert new_d.props.bare is new_b |
| 285 |
|
| 286 |
|
| 287 |
def test_objects_with_cyclic_dependency_and_instance_dict_no_content(): |
| 288 |
# Test with a cycle: |
| 289 |
# |
| 290 |
# b <-> a <-> c |
| 291 |
# ^---> d |
| 292 |
a, b, c, d = [Regress.TestObj(int=i) for i in range(4)] |
| 293 |
|
| 294 |
b.name = "b" |
| 295 |
|
| 296 |
a.props.bare = b |
| 297 |
a.all = [b, c, d] |
| 298 |
b.props.bare = a |
| 299 |
b.a = a |
| 300 |
c.props.bare = a |
| 301 |
c.a = a |
| 302 |
d.props.bare = a |
| 303 |
d.a = a |
| 304 |
del b, c, d |
| 305 |
|
| 306 |
gc.collect() |
| 307 |
gc.collect() |
| 308 |
|
| 309 |
assert a.props.bare.name == "b" |
| 310 |
|
| 311 |
|
| 312 |
def test_chained_objects_are_collected(): |
| 313 |
# a --> b --> c |
| 314 |
a, b, c = [Regress.TestObj(int=i) for i in range(3)] |
| 315 |
a.b = b |
| 316 |
b.c = c |
| 317 |
|
| 318 |
aref = a.weak_ref() |
| 319 |
cref = c.weak_ref() |
| 320 |
del a, b, c |
| 321 |
|
| 322 |
gc.collect() |
| 323 |
gc.collect() # some more for PyPy |
| 324 |
gc.collect() |
| 325 |
gc.collect() |
| 326 |
|
| 327 |
assert aref() is None |
| 328 |
assert cref() is None |
| 329 |
|
| 330 |
|
| 331 |
def test_objects_can_be_deleted(): |
| 332 |
a = Regress.TestObj() |
| 333 |
|
| 334 |
pyref = weakref.ref(a) |
| 335 |
gref = a.weak_ref() |
| 336 |
del a |
| 337 |
|
| 338 |
gc.collect() # for PyPy |
| 339 |
gc.collect() |
| 340 |
|
| 341 |
assert pyref() is None |
| 342 |
assert gref() is None |
| 343 |
|
| 344 |
|
| 345 |
@pytest.mark.skipif( |
| 346 |
sys.implementation.name == "pypy", reason="Doesn't play nice with PyPy GC" |
| 347 |
) |
| 348 |
def test_gobject_cycle_is_collected(): |
| 349 |
# Test with a cycle: |
| 350 |
# |
| 351 |
# a --> b --> c |
| 352 |
# ^-----------' |
| 353 |
a, b, c = [Regress.TestObj(int=i) for i in range(3)] |
| 354 |
|
| 355 |
a.b = b |
| 356 |
b.c = c |
| 357 |
c.a = a |
| 358 |
|
| 359 |
ref = a.weak_ref() |
| 360 |
del a, b, c |
| 361 |
|
| 362 |
gc.collect() |
| 363 |
gc.collect() |
| 364 |
|
| 365 |
assert ref() is None |
| 366 |
|
| 367 |
|
| 368 |
def test_object_with_post_init(): |
| 369 |
class PostInit(Regress.TestObj): |
| 370 |
number = GObject.Property( |
| 371 |
type=int, flags=GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT |
| 372 |
) |
| 373 |
|
| 374 |
def do_constructed(self): |
| 375 |
self.post_init_called = True |
| 376 |
assert self.number == 42 |
| 377 |
|
| 378 |
obj = PostInit(number=42) |
| 379 |
|
| 380 |
assert obj.post_init_called |
| 381 |
assert obj.number == 42 |
| 382 |
|
| 383 |
|
| 384 |
def test_object_with_post_init_created_by_new(): |
| 385 |
class PostInit(Regress.TestObj): |
| 386 |
number = GObject.Property( |
| 387 |
type=int, flags=GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT |
| 388 |
) |
| 389 |
|
| 390 |
def do_constructed(self): |
| 391 |
self.post_init_called = True |
| 392 |
assert self.number == 42 |
| 393 |
|
| 394 |
obj = GObject.new(PostInit, number=42) |
| 395 |
|
| 396 |
assert obj.post_init_called |
| 397 |
assert obj.number == 42 |
| 398 |
|
| 399 |
|
| 400 |
class InitializationOrder(Regress.TestObj): |
| 401 |
def __init__(self): |
| 402 |
self.order = ["__init__1"] |
| 403 |
super().__init__() |
| 404 |
self.order.append("__init__2") |
| 405 |
|
| 406 |
def do_constructed(self): |
| 407 |
self.order.append("do_constructed") |
| 408 |
|
| 409 |
|
| 410 |
def test_object_constructed_after_init(): |
| 411 |
obj = InitializationOrder() |
| 412 |
|
| 413 |
assert obj.order == ["__init__1", "do_constructed", "__init__2"] |
| 414 |
|
| 415 |
|
| 416 |
def test_object_constructed_after_init_by_new(): |
| 417 |
obj = GObject.new(InitializationOrder) |
| 418 |
|
| 419 |
assert obj.order == ["__init__1", "__init__2", "do_constructed"] |
| 420 |
|
| 421 |
|
| 422 |
def test_object_with_post_init_and_interface(): |
| 423 |
class PostInit(Regress.TestObj, Regress.TestInterface): |
| 424 |
number = GObject.Property(type=int) |
| 425 |
|
| 426 |
def __init__(self): |
| 427 |
self.post_init_called = 0 |
| 428 |
super().__init__() |
| 429 |
|
| 430 |
def do_constructed(self): |
| 431 |
super().do_constructed() |
| 432 |
self.post_init_called += 1 |
| 433 |
|
| 434 |
class SubPostInit(PostInit): |
| 435 |
def do_constructed(self): |
| 436 |
super().do_constructed() |
| 437 |
|
| 438 |
obj = PostInit() |
| 439 |
subobj = SubPostInit() |
| 440 |
|
| 441 |
assert obj.post_init_called == 1 |
| 442 |
assert subobj.post_init_called == 1 |
| 443 |
|
| 444 |
|
| 445 |
def test_object_with_post_init_raises_exception(): |
| 446 |
class PostInit(Regress.TestObj): |
| 447 |
def do_constructed(self): |
| 448 |
raise ValueError("Catch me") |
| 449 |
|
| 450 |
with pytest.raises(ValueError, match="Catch me"): |
| 451 |
PostInit() |
| 452 |
with pytest.raises(ValueError, match="Catch me"): |
| 453 |
GObject.new(PostInit) |