diff options
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/support/__init__.py | 5 | ||||
-rw-r--r-- | Lib/test/test_capi/test_mem.py | 3 | ||||
-rw-r--r-- | Lib/test/test_exceptions.py | 6 | ||||
-rw-r--r-- | Lib/test/test_import/__init__.py | 4 | ||||
-rw-r--r-- | Lib/test/test_repl.py | 4 | ||||
-rw-r--r-- | Lib/test/test_sys.py | 21 | ||||
-rw-r--r-- | Lib/test/test_tracemalloc.py | 20 |
7 files changed, 49 insertions, 14 deletions
diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py index c3f8527bd69..16a5056a33a 100644 --- a/Lib/test/support/__init__.py +++ b/Lib/test/support/__init__.py @@ -779,9 +779,6 @@ def python_is_optimized(): _header = 'nP' _align = '0n' -if hasattr(sys, "getobjects"): - _header = '2P' + _header - _align = '0P' _vheader = _header + 'n' def calcobjsize(fmt): @@ -2469,3 +2466,5 @@ C_RECURSION_LIMIT = 1500 #Windows doesn't have os.uname() but it doesn't support s390x. skip_on_s390x = unittest.skipIf(hasattr(os, 'uname') and os.uname().machine == 's390x', 'skipped on s390x') + +Py_TRACE_REFS = hasattr(sys, 'getobjects') diff --git a/Lib/test/test_capi/test_mem.py b/Lib/test/test_capi/test_mem.py index 527000875b7..72f23b1a340 100644 --- a/Lib/test/test_capi/test_mem.py +++ b/Lib/test/test_capi/test_mem.py @@ -112,6 +112,9 @@ class PyMemDebugTests(unittest.TestCase): def test_pyobject_freed_is_freed(self): self.check_pyobject_is_freed('check_pyobject_freed_is_freed') + # Python built with Py_TRACE_REFS fail with a fatal error in + # _PyRefchain_Trace() on memory allocation error. + @unittest.skipIf(support.Py_TRACE_REFS, 'cannot test Py_TRACE_REFS build') def test_set_nomemory(self): code = """if 1: import _testcapi diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py index 764122ed4ef..c766f4d4331 100644 --- a/Lib/test/test_exceptions.py +++ b/Lib/test/test_exceptions.py @@ -1484,6 +1484,9 @@ class ExceptionTests(unittest.TestCase): @cpython_only + # Python built with Py_TRACE_REFS fail with a fatal error in + # _PyRefchain_Trace() on memory allocation error. + @unittest.skipIf(support.Py_TRACE_REFS, 'cannot test Py_TRACE_REFS build') def test_recursion_normalizing_with_no_memory(self): # Issue #30697. Test that in the abort that occurs when there is no # memory left and the size of the Python frames stack is greater than @@ -1652,6 +1655,9 @@ class ExceptionTests(unittest.TestCase): self.assertTrue(report.endswith("\n")) @cpython_only + # Python built with Py_TRACE_REFS fail with a fatal error in + # _PyRefchain_Trace() on memory allocation error. + @unittest.skipIf(support.Py_TRACE_REFS, 'cannot test Py_TRACE_REFS build') def test_memory_error_in_PyErr_PrintEx(self): code = """if 1: import _testcapi diff --git a/Lib/test/test_import/__init__.py b/Lib/test/test_import/__init__.py index 740ce7d5ef2..33bce779f6c 100644 --- a/Lib/test/test_import/__init__.py +++ b/Lib/test/test_import/__init__.py @@ -28,7 +28,7 @@ import _imp from test.support import os_helper from test.support import ( STDLIB_DIR, swap_attr, swap_item, cpython_only, is_emscripten, - is_wasi, run_in_subinterp, run_in_subinterp_with_config) + is_wasi, run_in_subinterp, run_in_subinterp_with_config, Py_TRACE_REFS) from test.support.import_helper import ( forget, make_legacy_pyc, unlink, unload, DirsOnSysPath, CleanImport) from test.support.os_helper import ( @@ -2555,7 +2555,7 @@ class SinglephaseInitTests(unittest.TestCase): def test_basic_multiple_interpreters_deleted_no_reset(self): # without resetting; already loaded in a deleted interpreter - if hasattr(sys, 'getobjects'): + if Py_TRACE_REFS: # It's a Py_TRACE_REFS build. # This test breaks interpreter isolation a little, # which causes problems on Py_TRACE_REF builds. diff --git a/Lib/test/test_repl.py b/Lib/test/test_repl.py index ddb4aa68048..58392f2384a 100644 --- a/Lib/test/test_repl.py +++ b/Lib/test/test_repl.py @@ -5,6 +5,7 @@ import os import unittest import subprocess from textwrap import dedent +from test import support from test.support import cpython_only, has_subprocess_support, SuppressCrashReport from test.support.script_helper import kill_python @@ -59,6 +60,9 @@ def run_on_interactive_mode(source): class TestInteractiveInterpreter(unittest.TestCase): @cpython_only + # Python built with Py_TRACE_REFS fail with a fatal error in + # _PyRefchain_Trace() on memory allocation error. + @unittest.skipIf(support.Py_TRACE_REFS, 'cannot test Py_TRACE_REFS build') def test_no_memory(self): # Issue #30696: Fix the interactive interpreter looping endlessly when # no memory. Check also that the fix does not break the interactive diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py index f3608ce142f..d8b684c8a00 100644 --- a/Lib/test/test_sys.py +++ b/Lib/test/test_sys.py @@ -1174,6 +1174,27 @@ class SysModuleTest(unittest.TestCase): self.assertEqual(os.path.normpath(sys._stdlib_dir), os.path.normpath(expected)) + @unittest.skipUnless(hasattr(sys, 'getobjects'), 'need sys.getobjects()') + def test_getobjects(self): + # sys.getobjects(0) + all_objects = sys.getobjects(0) + self.assertIsInstance(all_objects, list) + self.assertGreater(len(all_objects), 0) + + # sys.getobjects(0, MyType) + class MyType: + pass + size = 100 + my_objects = [MyType() for _ in range(size)] + get_objects = sys.getobjects(0, MyType) + self.assertEqual(len(get_objects), size) + for obj in get_objects: + self.assertIsInstance(obj, MyType) + + # sys.getobjects(3, MyType) + get_objects = sys.getobjects(3, MyType) + self.assertEqual(len(get_objects), 3) + @test.support.cpython_only class UnraisableHookTest(unittest.TestCase): diff --git a/Lib/test/test_tracemalloc.py b/Lib/test/test_tracemalloc.py index 4af4ca3b977..bea12452103 100644 --- a/Lib/test/test_tracemalloc.py +++ b/Lib/test/test_tracemalloc.py @@ -173,9 +173,11 @@ class TestTracemallocEnabled(unittest.TestCase): self.assertEqual(len(traceback), 1) self.assertEqual(traceback, obj_traceback) - def find_trace(self, traces, traceback): + def find_trace(self, traces, traceback, size): + # filter also by size to ignore the memory allocated by + # _PyRefchain_Trace() if Python is built with Py_TRACE_REFS. for trace in traces: - if trace[2] == traceback._frames: + if trace[2] == traceback._frames and trace[1] == size: return trace self.fail("trace not found") @@ -186,11 +188,10 @@ class TestTracemallocEnabled(unittest.TestCase): obj, obj_traceback = allocate_bytes(obj_size) traces = tracemalloc._get_traces() - trace = self.find_trace(traces, obj_traceback) + trace = self.find_trace(traces, obj_traceback, obj_size) self.assertIsInstance(trace, tuple) domain, size, traceback, length = trace - self.assertEqual(size, obj_size) self.assertEqual(traceback, obj_traceback._frames) tracemalloc.stop() @@ -208,17 +209,18 @@ class TestTracemallocEnabled(unittest.TestCase): # Ensure that two identical tracebacks are not duplicated tracemalloc.stop() tracemalloc.start(4) - obj_size = 123 - obj1, obj1_traceback = allocate_bytes4(obj_size) - obj2, obj2_traceback = allocate_bytes4(obj_size) + obj1_size = 123 + obj2_size = 125 + obj1, obj1_traceback = allocate_bytes4(obj1_size) + obj2, obj2_traceback = allocate_bytes4(obj2_size) traces = tracemalloc._get_traces() obj1_traceback._frames = tuple(reversed(obj1_traceback._frames)) obj2_traceback._frames = tuple(reversed(obj2_traceback._frames)) - trace1 = self.find_trace(traces, obj1_traceback) - trace2 = self.find_trace(traces, obj2_traceback) + trace1 = self.find_trace(traces, obj1_traceback, obj1_size) + trace2 = self.find_trace(traces, obj2_traceback, obj2_size) domain1, size1, traceback1, length1 = trace1 domain2, size2, traceback2, length2 = trace2 self.assertIs(traceback2, traceback1) |