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/Objects/obmalloc.c b/Objects/obmalloc.c
index 8812f59..503fcdf 100644
--- a/Objects/obmalloc.c
+++ b/Objects/obmalloc.c
@@ -198,7 +198,7 @@
static PyMemAllocatorEx _PyMem = {
#ifdef Py_DEBUG
- &_PyMem_Debug.mem, PYRAWDBG_FUNCS
+ &_PyMem_Debug.mem, PYDBG_FUNCS
#else
NULL, PYMEM_FUNCS
#endif
@@ -321,17 +321,17 @@
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &alloc);
}
- if (_PyMem.malloc != _PyMem_DebugRawMalloc) {
- alloc.ctx = &_PyMem_Debug.mem;
- PyMem_GetAllocator(PYMEM_DOMAIN_MEM, &_PyMem_Debug.mem.alloc);
- PyMem_SetAllocator(PYMEM_DOMAIN_MEM, &alloc);
- }
-
alloc.malloc = _PyMem_DebugMalloc;
alloc.calloc = _PyMem_DebugCalloc;
alloc.realloc = _PyMem_DebugRealloc;
alloc.free = _PyMem_DebugFree;
+ if (_PyMem.malloc != _PyMem_DebugMalloc) {
+ alloc.ctx = &_PyMem_Debug.mem;
+ PyMem_GetAllocator(PYMEM_DOMAIN_MEM, &_PyMem_Debug.mem.alloc);
+ PyMem_SetAllocator(PYMEM_DOMAIN_MEM, &alloc);
+ }
+
if (_PyObject.malloc != _PyMem_DebugMalloc) {
alloc.ctx = &_PyMem_Debug.obj;
PyMem_GetAllocator(PYMEM_DOMAIN_OBJ, &_PyMem_Debug.obj.alloc);