Skip to content

Commit 8c6f547

Browse files
authored
[3.13] gh-152236: Fix skips on _testcapi.set_nomemory tests (GH-152253) (#152286)
* [3.13] 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 fba87d7 commit 8c6f547

7 files changed

Lines changed: 28 additions & 34 deletions

File tree

Lib/test/support/__init__.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"requires_gil_enabled", "requires_linux_version", "requires_mac_ver",
3535
"check_syntax_error",
3636
"requires_gzip", "requires_bz2", "requires_lzma",
37-
"bigmemtest", "bigaddrspacetest", "cpython_only", "get_attribute",
37+
"bigmemtest", "nomemtest", "bigaddrspacetest", "cpython_only", "get_attribute",
3838
"requires_IEEE_754", "requires_zlib",
3939
"has_fork_support", "requires_fork",
4040
"has_subprocess_support", "requires_subprocess",
@@ -1215,6 +1215,22 @@ def wrapper(self):
12151215
return wrapper
12161216
return decorator
12171217

1218+
def nomemtest(f):
1219+
"""Check that we can use this test with `_testcapi.set_nomemory`."""
1220+
from .import_helper import import_module
1221+
1222+
@functools.wraps(f)
1223+
def internal(*args, **kwargs):
1224+
import_module('_testcapi')
1225+
return f(*args, **kwargs)
1226+
1227+
return unittest.skipIf(
1228+
# Python built with Py_TRACE_REFS fail with a fatal error in
1229+
# _PyRefchain_Trace() on memory allocation error.
1230+
Py_TRACE_REFS,
1231+
'cannot test Py_TRACE_REFS build',
1232+
)(cpython_only(internal))
1233+
12181234
def bigaddrspacetest(f):
12191235
"""Decorator for tests that fill the address space."""
12201236
def wrapper(self):

Lib/test/test_capi/test_mem.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,7 @@ def test_pyobject_forbidden_bytes_is_freed(self):
112112
def test_pyobject_freed_is_freed(self):
113113
self.check_pyobject_is_freed('check_pyobject_freed_is_freed')
114114

115-
# Python built with Py_TRACE_REFS fail with a fatal error in
116-
# _PyRefchain_Trace() on memory allocation error.
117-
@unittest.skipIf(support.Py_TRACE_REFS, 'cannot test Py_TRACE_REFS build')
115+
@support.nomemtest
118116
def test_set_nomemory(self):
119117
code = """if 1:
120118
import _testcapi

Lib/test/test_class.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"Test the functionality of Python classes implementing operators."
22

33
import unittest
4+
from test import support
45
from test.support import cpython_only, import_helper, script_helper
56

67
testmeths = [
@@ -447,7 +448,6 @@ def __delattr__(self, *args):
447448

448449
def testHasAttrString(self):
449450
import sys
450-
from test.support import import_helper
451451
_testlimitedcapi = import_helper.import_module('_testlimitedcapi')
452452

453453
class A:
@@ -995,11 +995,8 @@ class C:
995995
C.a = X()
996996
C.a = X()
997997

998-
@cpython_only
998+
@support.nomemtest
999999
def test_detach_materialized_dict_no_memory(self):
1000-
# Skip test if _testcapi is not available:
1001-
import_helper.import_module('_testcapi')
1002-
10031000
code = """if 1:
10041001
import test.support
10051002
import _testcapi

Lib/test/test_exceptions.py

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

15331533

1534-
@cpython_only
1535-
# Python built with Py_TRACE_REFS fail with a fatal error in
1536-
# _PyRefchain_Trace() on memory allocation error.
1537-
@unittest.skipIf(support.Py_TRACE_REFS, 'cannot test Py_TRACE_REFS build')
1538-
@unittest.skipIf(_testcapi is None, "requires _testcapi")
1534+
@support.nomemtest
15391535
def test_recursion_normalizing_with_no_memory(self):
15401536
# Issue #30697. Test that in the abort that occurs when there is no
15411537
# memory left and the size of the Python frames stack is greater than
@@ -1719,11 +1715,7 @@ def test_unhandled(self):
17191715
self.assertIn("test message", report)
17201716
self.assertTrue(report.endswith("\n"))
17211717

1722-
@cpython_only
1723-
# Python built with Py_TRACE_REFS fail with a fatal error in
1724-
# _PyRefchain_Trace() on memory allocation error.
1725-
@unittest.skipIf(support.Py_TRACE_REFS, 'cannot test Py_TRACE_REFS build')
1726-
@unittest.skipIf(_testcapi is None, "requires _testcapi")
1718+
@support.nomemtest
17271719
def test_memory_error_in_PyErr_PrintEx(self):
17281720
code = """if 1:
17291721
import _testcapi
@@ -1857,12 +1849,8 @@ def test_memory_error_in_subinterp(self):
18571849
rc, _, err = script_helper.assert_python_ok("-c", code)
18581850
self.assertIn(b'MemoryError', err)
18591851

1860-
@cpython_only
1861-
# Python built with Py_TRACE_REFS fail with a fatal error in
1862-
# _PyRefchain_Trace() on memory allocation error.
1863-
@unittest.skipIf(support.Py_TRACE_REFS, 'cannot test Py_TRACE_REFS build')
1852+
@support.nomemtest
18641853
def test_exec_set_nomemory_hang(self):
1865-
import_module("_testcapi")
18661854
# gh-134163: A MemoryError inside code that was wrapped by a try/except
18671855
# block would lead to an infinite loop.
18681856

Lib/test/test_interpreters/test_stress.py

Lines changed: 3 additions & 3 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 test.support import interpreters
1010
from test.support.interpreters import InterpreterError
1111
from .utils import TestBase
@@ -75,9 +75,9 @@ 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

8282
with self.assertRaises(InterpreterError):
8383
_testcapi.set_nomemory(0, 1)

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
@@ -1022,7 +1022,7 @@ def __del__(self): pass
10221022
del x
10231023
support.gc_collect()
10241024

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

0 commit comments

Comments
 (0)