11from __future__ import annotations
22
3+ import gc
34import sys
45
56import pytest
1920)
2021
2122
23+ def gc_collect (repeat = 5 ):
24+ """Collect garbage multiple times to ensure that objects are actually deleted."""
25+ for _ in range (repeat ):
26+ gc .collect ()
27+
28+
2229def test_self_only_pos_only ():
2330 assert (
2431 m .ExampleMandA .__str__ .__doc__
@@ -337,19 +344,21 @@ def test_dynamic_attributes():
337344 cstats = ConstructorStats .get (m .DynamicClass )
338345 assert cstats .alive () == 1
339346 del instance
347+ gc_collect (repeat = 10 )
340348 assert cstats .alive () == 0
341349
342350 # Derived classes should work as well
343351 class PythonDerivedDynamicClass (m .DynamicClass ):
344352 pass
345353
346- for cls in m .CppDerivedDynamicClass , PythonDerivedDynamicClass :
354+ for cls in ( m .CppDerivedDynamicClass , PythonDerivedDynamicClass ) :
347355 derived = cls ()
348356 derived .foobar = 100
349357 assert derived .foobar == 100
350358
351359 assert cstats .alive () == 1
352360 del derived
361+ gc_collect (repeat = 10 )
353362 assert cstats .alive () == 0
354363
355364
@@ -364,6 +373,7 @@ def test_cyclic_gc():
364373 cstats = ConstructorStats .get (m .DynamicClass )
365374 assert cstats .alive () == 1
366375 del instance
376+ gc_collect (repeat = 10 )
367377 assert cstats .alive () == 0
368378
369379 # Two object reference each other
@@ -374,6 +384,7 @@ def test_cyclic_gc():
374384
375385 assert cstats .alive () == 2
376386 del i1 , i2
387+ gc_collect (repeat = 10 )
377388 assert cstats .alive () == 0
378389
379390
0 commit comments