From 383a44e5c8e875b671ce5568b635ed0fd00490ad Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 10 Mar 2016 10:33:46 +0100 Subject: [PATCH] Use PyMem_RawMalloc on Python 3.4 and newer Change PyArray_malloc() macro to use PyMem_RawMalloc() on Python 3.4 and newer. This macro can be called indirectly from ufunc_at() which releases the GIL, whereas PyMem_Malloc() requires the GIL to be held: https://docs.python.org/dev/c-api/memory.html#memory-interface PyMem_RawMalloc() can be called without the GIL. --- numpy/core/include/numpy/ndarraytypes.h | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/numpy/core/include/numpy/ndarraytypes.h b/numpy/core/include/numpy/ndarraytypes.h index df43122d01a8..a9848f43496e 100644 --- a/numpy/core/include/numpy/ndarraytypes.h +++ b/numpy/core/include/numpy/ndarraytypes.h @@ -328,9 +328,20 @@ struct NpyAuxData_tag { #define NPY_USE_PYMEM 1 #if NPY_USE_PYMEM == 1 -#define PyArray_malloc PyMem_Malloc -#define PyArray_free PyMem_Free -#define PyArray_realloc PyMem_Realloc + /* numpy sometimes calls PyArray_malloc() with the GIL released. On Python + 3.3 and older, it was safe to call PyMem_Malloc() with the GIL released. + On Python 3.4 and newer, it's better to use PyMem_RawMalloc() to be able + to use tracemalloc. On Python 3.6, calling PyMem_Malloc() with the GIL + released is now a fatal error in debug mode. */ +# if PY_VERSION_HEX >= 0x03040000 +# define PyArray_malloc PyMem_RawMalloc +# define PyArray_free PyMem_RawFree +# define PyArray_realloc PyMem_RawRealloc +# else +# define PyArray_malloc PyMem_Malloc +# define PyArray_free PyMem_Free +# define PyArray_realloc PyMem_Realloc +# endif #else #define PyArray_malloc malloc #define PyArray_free free