Skip to content

Commit 8b48a0c

Browse files
XuehaiPanhenryiii
authored andcommitted
test: trigger gc manually
1 parent 39d0aa4 commit 8b48a0c

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

tests/test_methods_and_attributes.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import gc
34
import sys
45

56
import pytest
@@ -19,6 +20,12 @@
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+
2229
def 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

Comments
 (0)