Skip to content

Commit 2cd175c

Browse files
sobolevnmiss-islington
authored andcommitted
gh-152236: Fix skips on _testcapi.set_nomemory tests (GH-152253)
(cherry picked from commit 1cbe460) Co-authored-by: sobolevn <mail@sobolevn.me>
1 parent df7c9e1 commit 2cd175c

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",
@@ -1226,6 +1226,22 @@ def wrapper(self):
12261226
return wrapper
12271227
return decorator
12281228

1229+
def nomemtest(f):
1230+
"""Check that we can use this test with `_testcapi.set_nomemory`."""
1231+
from .import_helper import import_module
1232+
1233+
@functools.wraps(f)
1234+
def internal(*args, **kwargs):
1235+
import_module('_testcapi')
1236+
return f(*args, **kwargs)
1237+
1238+
return unittest.skipIf(
1239+
# Python built with Py_TRACE_REFS fail with a fatal error in
1240+
# _PyRefchain_Trace() on memory allocation error.
1241+
Py_TRACE_REFS,
1242+
'cannot test Py_TRACE_REFS build',
1243+
)(cpython_only(internal))
1244+
12291245
def bigaddrspacetest(f):
12301246
"""Decorator for tests that fill the address space."""
12311247
def wrapper(self):

Lib/test/test_atexit.py

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

138-
# Python built with Py_TRACE_REFS fail with a fatal error in
139-
# _PyRefchain_Trace() on memory allocation error.
140-
@unittest.skipIf(support.Py_TRACE_REFS, 'cannot test Py_TRACE_REFS build')
138+
@support.nomemtest
141139
def test_atexit_with_low_memory(self):
142140
# gh-140080: Test that setting low memory after registering an atexit
143141
# 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:
@@ -1012,11 +1011,8 @@ class C:
10121011
C.a = X()
10131012
C.a = X()
10141013

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

Lib/test/test_exceptions.py

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

15771577

1578-
@cpython_only
1579-
# Python built with Py_TRACE_REFS fail with a fatal error in
1580-
# _PyRefchain_Trace() on memory allocation error.
1581-
@unittest.skipIf(support.Py_TRACE_REFS, 'cannot test Py_TRACE_REFS build')
1582-
@unittest.skipIf(_testcapi is None, "requires _testcapi")
1578+
@support.nomemtest
15831579
def test_recursion_normalizing_with_no_memory(self):
15841580
# Issue #30697. Test that in the abort that occurs when there is no
15851581
# memory left and the size of the Python frames stack is greater than
@@ -1766,11 +1762,7 @@ def test_unhandled(self):
17661762
self.assertIn("test message", report)
17671763
self.assertEndsWith(report, "\n")
17681764

1769-
@cpython_only
1770-
# Python built with Py_TRACE_REFS fail with a fatal error in
1771-
# _PyRefchain_Trace() on memory allocation error.
1772-
@unittest.skipIf(support.Py_TRACE_REFS, 'cannot test Py_TRACE_REFS build')
1773-
@unittest.skipIf(_testcapi is None, "requires _testcapi")
1765+
@support.nomemtest
17741766
def test_memory_error_in_PyErr_PrintEx(self):
17751767
code = """if 1:
17761768
import _testcapi
@@ -1928,12 +1920,8 @@ def test_keyerror_context(self):
19281920
exc2 = None
19291921

19301922

1931-
@cpython_only
1932-
# Python built with Py_TRACE_REFS fail with a fatal error in
1933-
# _PyRefchain_Trace() on memory allocation error.
1934-
@unittest.skipIf(support.Py_TRACE_REFS, 'cannot test Py_TRACE_REFS build')
1923+
@support.nomemtest
19351924
def test_exec_set_nomemory_hang(self):
1936-
import_module("_testcapi")
19371925
# gh-134163: A MemoryError inside code that was wrapped by a try/except
19381926
# block would lead to an infinite loop.
19391927

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
@@ -1019,7 +1019,7 @@ def __del__(self): pass
10191019
del x
10201020
support.gc_collect()
10211021

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

0 commit comments

Comments
 (0)