https://github.com/python/cpython/commit/57a478fa0ac9e09a13562ab1b0f595dc2d481a8b
commit: 57a478fa0ac9e09a13562ab1b0f595dc2d481a8b
branch: 3.14
author: Victor Stinner <[email protected]>
committer: vstinner <[email protected]>
date: 2026-10-16T15:40:58+02:00
summary:

[3.14] gh-157242: Fix set_nomemory() on Py_TRACE_REFS build (#157351) (#159528)

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.

(cherry picked from commit 051b168e63af80872222a2d91d43af4de16980b1)

files:
M Lib/test/support/__init__.py
M Objects/object.c

diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py
index 8764d66514c4b3..dc6204d0a84658 100644
--- a/Lib/test/support/__init__.py
+++ b/Lib/test/support/__init__.py
@@ -1217,21 +1217,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/Objects/object.c b/Objects/object.c
index b17a5bd70caf91..b0bfaf0a5c4195 100644
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -196,10 +196,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]

Reply via email to