|
1 | 1 | import unittest |
2 | 2 | from test.support import (verbose, refcount_test, run_unittest, |
3 | 3 | strip_python_stderr) |
| 4 | +from test.script_helper import assert_python_ok, make_script, temp_dir |
| 5 | + |
4 | 6 | import sys |
5 | 7 | import time |
6 | 8 | import gc |
@@ -610,6 +612,40 @@ def run_command(code): |
610 | 612 | stderr = run_command(code % "gc.DEBUG_SAVEALL") |
611 | 613 | self.assertNotIn(b"uncollectable objects at shutdown", stderr) |
612 | 614 |
|
| 615 | + def test_gc_main_module_at_shutdown(self): |
| 616 | + # Create a reference cycle through the __main__ module and check |
| 617 | + # it gets collected at interpreter shutdown. |
| 618 | + code = """if 1: |
| 619 | + import weakref |
| 620 | + class C: |
| 621 | + def __del__(self): |
| 622 | + print('__del__ called') |
| 623 | + l = [C()] |
| 624 | + l.append(l) |
| 625 | + """ |
| 626 | + rc, out, err = assert_python_ok('-c', code) |
| 627 | + self.assertEqual(out.strip(), b'__del__ called') |
| 628 | + |
| 629 | + def test_gc_ordinary_module_at_shutdown(self): |
| 630 | + # Same as above, but with a non-__main__ module. |
| 631 | + with temp_dir() as script_dir: |
| 632 | + module = """if 1: |
| 633 | + import weakref |
| 634 | + class C: |
| 635 | + def __del__(self): |
| 636 | + print('__del__ called') |
| 637 | + l = [C()] |
| 638 | + l.append(l) |
| 639 | + """ |
| 640 | + code = """if 1: |
| 641 | + import sys |
| 642 | + sys.path.insert(0, %r) |
| 643 | + import gctest |
| 644 | + """ % (script_dir,) |
| 645 | + make_script(script_dir, 'gctest', module) |
| 646 | + rc, out, err = assert_python_ok('-c', code) |
| 647 | + self.assertEqual(out.strip(), b'__del__ called') |
| 648 | + |
613 | 649 | def test_get_stats(self): |
614 | 650 | stats = gc.get_stats() |
615 | 651 | self.assertEqual(len(stats), 3) |
|
0 commit comments