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