|
| 1 | +import gc |
| 2 | + |
1 | 3 | from test import support |
2 | 4 | from test.support import is_apple_mobile, os_helper, requires_debug_ranges, is_emscripten |
3 | 5 | from test.support.script_helper import assert_python_ok |
@@ -872,6 +874,55 @@ def test_read_object_from_file(self): |
872 | 874 | _testcapi.pymarshal_read_object_from_file(os_helper.TESTFN) |
873 | 875 | os_helper.unlink(os_helper.TESTFN) |
874 | 876 |
|
| 877 | +@support.cpython_only |
| 878 | +class GCTrackingTestCase(unittest.TestCase): |
| 879 | + |
| 880 | + def _not_tracked_instantly(self, t): |
| 881 | + new = marshal.loads(marshal.dumps(t)) |
| 882 | + |
| 883 | + self.assertFalse(gc.is_tracked(t), t) |
| 884 | + self.assertFalse(gc.is_tracked(new), new) |
| 885 | + |
| 886 | + def _not_tracked(self, t): |
| 887 | + # Nested tuples can take several collections to untrack |
| 888 | + gc.collect() |
| 889 | + gc.collect() |
| 890 | + |
| 891 | + new = marshal.loads(marshal.dumps(t)) |
| 892 | + |
| 893 | + self.assertFalse(gc.is_tracked(t), t) |
| 894 | + self.assertFalse(gc.is_tracked(new), new) |
| 895 | + |
| 896 | + def _tracked(self, t): |
| 897 | + new = marshal.loads(marshal.dumps(t)) |
| 898 | + |
| 899 | + self.assertTrue(gc.is_tracked(t), t) |
| 900 | + self.assertTrue(gc.is_tracked(new), new) |
| 901 | + |
| 902 | + def testTuple(self): |
| 903 | + x, y, z = 1.5, "a", [] |
| 904 | + |
| 905 | + self._not_tracked_instantly(()) |
| 906 | + self._not_tracked_instantly((1,)) |
| 907 | + self._not_tracked_instantly((1, 2)) |
| 908 | + self._not_tracked_instantly((1, 2, "a")) |
| 909 | + self._not_tracked_instantly((12, 10**10, 'a_' * 100)) |
| 910 | + |
| 911 | + # Test for _PyTuple_Concat |
| 912 | + self._not_tracked_instantly((1, 2) + (2, 3)) |
| 913 | + |
| 914 | + # Test for _PyTuple_Repeat |
| 915 | + self._not_tracked_instantly((1, 2) * 5) |
| 916 | + |
| 917 | + self._not_tracked(((1, x), y, (2, 3))) |
| 918 | + self._not_tracked((1, 2, (True, False, ()))) |
| 919 | + |
| 920 | + self._tracked(([],)) |
| 921 | + self._tracked(([1],)) |
| 922 | + self._tracked(({},)) |
| 923 | + self._tracked((set(),)) |
| 924 | + self._tracked((x, y, z)) |
| 925 | + |
875 | 926 |
|
876 | 927 | if __name__ == "__main__": |
877 | 928 | unittest.main() |
0 commit comments