Skip to content

Commit 1cbe460

Browse files
authored
gh-152236: Fix skips on _testcapi.set_nomemory tests (#152253)
1 parent a87d24a commit 1cbe460

9 files changed

Lines changed: 32 additions & 42 deletions

File tree

Lib/test/support/__init__.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"requires_gil_enabled", "requires_linux_version", "requires_mac_ver",
3636
"check_syntax_error",
3737
"requires_gzip", "requires_bz2", "requires_lzma", "requires_zstd",
38-
"bigmemtest", "bigaddrspacetest", "cpython_only", "get_attribute",
38+
"bigmemtest", "nomemtest", "bigaddrspacetest", "cpython_only", "get_attribute",
3939
"requires_IEEE_754", "requires_zlib",
4040
"has_fork_support", "requires_fork",
4141
"has_subprocess_support", "requires_subprocess",
@@ -1305,6 +1305,22 @@ def wrapper(self):
13051305
return wrapper
13061306
return decorator
13071307

1308+
def nomemtest(f):
1309+
"""Check that we can use this test with `_testcapi.set_nomemory`."""
1310+
from .import_helper import import_module
1311+
1312+
@functools.wraps(f)
1313+
def internal(*args, **kwargs):
1314+
import_module('_testcapi')
1315+
return f(*args, **kwargs)
1316+
1317+
return unittest.skipIf(
1318+
# Python built with Py_TRACE_REFS fail with a fatal error in
1319+
# _PyRefchain_Trace() on memory allocation error.
1320+
Py_TRACE_REFS,
1321+
'cannot test Py_TRACE_REFS build',
1322+
)(cpython_only(internal))
1323+
13081324
def bigaddrspacetest(f):
13091325
"""Decorator for tests that fill the address space."""
13101326
def wrapper(self):

Lib/test/test_atexit.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,7 @@ def callback():
191191
self.assertEqual(os.read(r, len(expected)), expected)
192192
os.close(r)
193193

194-
# Python built with Py_TRACE_REFS fail with a fatal error in
195-
# _PyRefchain_Trace() on memory allocation error.
196-
@unittest.skipIf(support.Py_TRACE_REFS, 'cannot test Py_TRACE_REFS build')
194+
@support.nomemtest
197195
def test_atexit_with_low_memory(self):
198196
# gh-140080: Test that setting low memory after registering an atexit
199197
# callback doesn't cause an infinite loop during finalization.

Lib/test/test_capi/test_mem.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,7 @@ def test_pyobject_forbidden_bytes_is_freed(self):
117117
def test_pyobject_freed_is_freed(self):
118118
self.check_pyobject_is_freed('check_pyobject_freed_is_freed')
119119

120-
# Python built with Py_TRACE_REFS fail with a fatal error in
121-
# _PyRefchain_Trace() on memory allocation error.
122-
@unittest.skipIf(support.Py_TRACE_REFS, 'cannot test Py_TRACE_REFS build')
120+
@support.nomemtest
123121
def test_set_nomemory(self):
124122
code = """if 1:
125123
import _testcapi

Lib/test/test_class.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,6 @@ def __delattr__(self, *args):
449449

450450
def testHasAttrString(self):
451451
import sys
452-
from test.support import import_helper
453452
_testlimitedcapi = import_helper.import_module('_testlimitedcapi')
454453

455454
class A:
@@ -1013,11 +1012,8 @@ class C:
10131012
C.a = X()
10141013
C.a = X()
10151014

1016-
@cpython_only
1015+
@support.nomemtest
10171016
def test_detach_materialized_dict_no_memory(self):
1018-
# Skip test if _testcapi is not available:
1019-
import_helper.import_module('_testcapi')
1020-
10211017
code = """if 1:
10221018
import test.support
10231019
import _testcapi

Lib/test/test_exceptions.py

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,11 +1583,7 @@ def recurse_in_body_and_except():
15831583
sys.setrecursionlimit(recursionlimit)
15841584

15851585

1586-
@cpython_only
1587-
# Python built with Py_TRACE_REFS fail with a fatal error in
1588-
# _PyRefchain_Trace() on memory allocation error.
1589-
@unittest.skipIf(support.Py_TRACE_REFS, 'cannot test Py_TRACE_REFS build')
1590-
@unittest.skipIf(_testcapi is None, "requires _testcapi")
1586+
@support.nomemtest
15911587
def test_recursion_normalizing_with_no_memory(self):
15921588
# Issue #30697. Test that in the abort that occurs when there is no
15931589
# memory left and the size of the Python frames stack is greater than
@@ -1774,11 +1770,7 @@ def test_unhandled(self):
17741770
self.assertIn("test message", report)
17751771
self.assertEndsWith(report, "\n")
17761772

1777-
@cpython_only
1778-
# Python built with Py_TRACE_REFS fail with a fatal error in
1779-
# _PyRefchain_Trace() on memory allocation error.
1780-
@unittest.skipIf(support.Py_TRACE_REFS, 'cannot test Py_TRACE_REFS build')
1781-
@unittest.skipIf(_testcapi is None, "requires _testcapi")
1773+
@support.nomemtest
17821774
def test_memory_error_in_PyErr_PrintEx(self):
17831775
code = """if 1:
17841776
import _testcapi
@@ -1936,12 +1928,8 @@ def test_keyerror_context(self):
19361928
exc2 = None
19371929

19381930

1939-
@cpython_only
1940-
# Python built with Py_TRACE_REFS fail with a fatal error in
1941-
# _PyRefchain_Trace() on memory allocation error.
1942-
@unittest.skipIf(support.Py_TRACE_REFS, 'cannot test Py_TRACE_REFS build')
1931+
@support.nomemtest
19431932
def test_exec_set_nomemory_hang(self):
1944-
import_module("_testcapi")
19451933
# gh-134163: A MemoryError inside code that was wrapped by a try/except
19461934
# block would lead to an infinite loop.
19471935

Lib/test/test_interpreters/test_stress.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from test.support import import_helper
66
from test.support import threading_helper
77
# Raise SkipTest if subinterpreters not supported.
8-
import_helper.import_module('_interpreters')
8+
_interpreters = import_helper.import_module('_interpreters')
99
from concurrent import interpreters
1010
from concurrent.interpreters import InterpreterError
1111
from .utils import TestBase
@@ -75,12 +75,13 @@ def run():
7575
start.set()
7676
support.gc_collect()
7777

78+
@support.nomemtest
7879
def test_create_interpreter_no_memory(self):
79-
import _interpreters
80-
_testcapi = import_helper.import_module("_testcapi")
80+
import _testcapi
8181

82-
with self.assertRaises(InterpreterError):
83-
_testcapi.set_nomemory(0, 1)
82+
assertion = self.assertRaises(InterpreterError)
83+
_testcapi.set_nomemory(0, 1)
84+
with assertion:
8485
_interpreters.create()
8586

8687

Lib/test/test_list.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import textwrap
44
from test import list_tests, support
55
from test.support import cpython_only
6-
from test.support.import_helper import import_module
76
from test.support.script_helper import assert_python_failure, assert_python_ok
87
import pickle
98
import unittest
@@ -326,10 +325,9 @@ def test_tier2_invalidates_iterator(self):
326325
a.append(4)
327326
self.assertEqual(list(it), [])
328327

329-
@support.cpython_only
328+
@support.nomemtest
330329
def test_no_memory(self):
331330
# gh-118331: Make sure we don't crash if list allocation fails
332-
import_module("_testcapi")
333331
code = textwrap.dedent("""
334332
import _testcapi, sys
335333
# Prime the freelist

Lib/test/test_repl.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
SHORT_TIMEOUT,
1818
)
1919
from test.support.script_helper import kill_python
20-
from test.support.import_helper import import_module
2120

2221
try:
2322
import pty
@@ -99,12 +98,8 @@ def run_on_interactive_mode(source):
9998
@support.force_not_colorized_test_class
10099
class TestInteractiveInterpreter(unittest.TestCase):
101100

102-
@cpython_only
103-
# Python built with Py_TRACE_REFS fail with a fatal error in
104-
# _PyRefchain_Trace() on memory allocation error.
105-
@unittest.skipIf(support.Py_TRACE_REFS, 'cannot test Py_TRACE_REFS build')
101+
@support.nomemtest
106102
def test_no_memory(self):
107-
import_module("_testcapi")
108103
# Issue #30696: Fix the interactive interpreter looping endlessly when
109104
# no memory. Check also that the fix does not break the interactive
110105
# loop when an exception is raised.

Lib/test/test_weakref.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1024,7 +1024,7 @@ def __del__(self): pass
10241024
del x
10251025
support.gc_collect()
10261026

1027-
@support.cpython_only
1027+
@support.nomemtest
10281028
def test_no_memory_when_clearing(self):
10291029
# gh-118331: Make sure we do not raise an exception from the destructor
10301030
# when clearing weakrefs if allocating the intermediate tuple fails.

0 commit comments

Comments
 (0)