https://github.com/python/cpython/commit/051b168e63af80872222a2d91d43af4de16980b1
commit: 051b168e63af80872222a2d91d43af4de16980b1
branch: main
author: Victor Stinner <[email protected]>
committer: vstinner <[email protected]>
date: 2026-09-12T03:27:05Z
summary:
gh-157242: Fix set_nomemory() on Py_TRACE_REFS build (#157351)
On Py_TRACE_REFS build, use malloc() and free() functions of the C
library for the "reference chain" hash table. So it becomes possible
to use _testcapi.set_nomemory() with Py_TRACE_REFS.
Mark new tests using set_nomemory() with @support.nomemtest.
files:
M Lib/test/support/__init__.py
M Lib/test/test_bytes.py
M Lib/test/test_capi/test_bytes.py
M Objects/object.c
diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py
index 28a0ba6c666629b..210982fae236d5f 100644
--- a/Lib/test/support/__init__.py
+++ b/Lib/test/support/__init__.py
@@ -1364,21 +1364,16 @@ def wrapper(self):
return wrapper
return decorator
-def nomemtest(f):
+def nomemtest(test):
"""Check that we can use this test with `_testcapi.set_nomemory`."""
from .import_helper import import_module
- @functools.wraps(f)
+ @functools.wraps(test)
def internal(*args, **kwargs):
import_module('_testcapi')
- return f(*args, **kwargs)
+ return test(*args, **kwargs)
- return unittest.skipIf(
- # Python built with Py_TRACE_REFS fail with a fatal error in
- # _PyRefchain_Trace() on memory allocation error.
- Py_TRACE_REFS,
- 'cannot test Py_TRACE_REFS build',
- )(cpython_only(internal))
+ return cpython_only(internal)
def bigaddrspacetest(f):
"""Decorator for tests that fill the address space."""
diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py
index 0f21ffb5ecb9b4f..4b2bed9a0a56446 100644
--- a/Lib/test/test_bytes.py
+++ b/Lib/test/test_bytes.py
@@ -1569,6 +1569,7 @@ def test_resize(self):
self.assertRaises(MemoryError, bytearray().resize, sys.maxsize)
self.assertRaises(MemoryError, bytearray(1000).resize, sys.maxsize)
+ @support.nomemtest
def test_resize_error(self):
# gh-157242: If bytearray.resize() fails (MemoryError),
# the bytearray must be left unchanged.
@@ -1662,6 +1663,7 @@ def test_take_bytes(self):
self.assertEqual(ba, bytearray(b'A'))
self.assertEqual(ord(b'c'), ord('c'))
+ @support.nomemtest
def test_take_bytes_error(self):
# gh-157242: If bytearray.take_bytes() fails (MemoryError),
# the bytearray must be left unchanged.
diff --git a/Lib/test/test_capi/test_bytes.py b/Lib/test/test_capi/test_bytes.py
index 2c0476f2e1faf3d..799a17617be1a3f 100644
--- a/Lib/test/test_capi/test_bytes.py
+++ b/Lib/test/test_capi/test_bytes.py
@@ -1,5 +1,6 @@
import sys
import unittest
+from test import support
from test.support import import_helper
_testlimitedcapi = import_helper.import_module('_testlimitedcapi')
@@ -389,6 +390,7 @@ def test_resize(self):
writer.resize(len(b'number=123456'), b'456')
self.assertEqual(writer.finish(), self.result_type(b'number=123456'))
+ @support.nomemtest
def test_resize_error(self):
small_buffer = _testcapi.PyBytesWriter_small_buffer
init = b'x' * (small_buffer * 2)
diff --git a/Objects/object.c b/Objects/object.c
index 856d9fc41a41546..a83f8d4c04ca079 100644
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -198,10 +198,11 @@ refchain_init(PyInterpreterState *interp)
return 0;
}
_Py_hashtable_allocator_t alloc = {
- // Don't use default PyMem_Malloc() and PyMem_Free() which
- // require the caller to hold the GIL.
- .malloc = PyMem_RawMalloc,
- .free = PyMem_RawFree,
+ // Use directly malloc() and free() of the C library. Using
+ // PyMem_RawMalloc() and PyMem_RawFree() prevents testing
+ // _testcapi.set_nomemory().
+ .malloc = malloc,
+ .free = free,
};
REFCHAIN(interp) = _Py_hashtable_new_full(
_Py_hashtable_hash_ptr, _Py_hashtable_compare_direct,
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]