Fail if PyMem_Malloc() is called without holding the GIL
Issue #26563: Debug hooks on Python memory allocators now raise a fatal error
if functions of the PyMem_Malloc() family are called without holding the GIL.
diff --git a/Lib/test/test_capi.py b/Lib/test/test_capi.py
index 8e6245b..8f4836a 100644
--- a/Lib/test/test_capi.py
+++ b/Lib/test/test_capi.py
@@ -602,15 +602,24 @@
regex = regex.format(ptr=self.PTR_REGEX)
self.assertRegex(out, regex)
- def test_pyobject_malloc_without_gil(self):
- # Calling PyObject_Malloc() without holding the GIL must raise an
- # error in debug mode.
- code = 'import _testcapi; _testcapi.pyobject_malloc_without_gil()'
+ def check_malloc_without_gil(self, code):
out = self.check(code)
expected = ('Fatal Python error: Python memory allocator called '
'without holding the GIL')
self.assertIn(expected, out)
+ def test_pymem_malloc_without_gil(self):
+ # Debug hooks must raise an error if PyMem_Malloc() is called
+ # without holding the GIL
+ code = 'import _testcapi; _testcapi.pymem_malloc_without_gil()'
+ self.check_malloc_without_gil(code)
+
+ def test_pyobject_malloc_without_gil(self):
+ # Debug hooks must raise an error if PyObject_Malloc() is called
+ # without holding the GIL
+ code = 'import _testcapi; _testcapi.pyobject_malloc_without_gil()'
+ self.check_malloc_without_gil(code)
+
class PyMemMallocDebugTests(PyMemDebugTests):
PYTHONMALLOC = 'malloc_debug'