diff -r 5619bc2d8207 Objects/obmalloc.c --- a/Objects/obmalloc.c Fri Jun 14 19:02:34 2013 -0400 +++ b/Objects/obmalloc.c Sat Jun 15 02:06:22 2013 +0200 @@ -1679,6 +1679,22 @@ pool_is_in_list(const poolp target, pool #endif /* Py_DEBUG */ +static void +_PyMem_DebugCheckGIL(void) +{ + PyThreadState *tstate; + static int check = 1; + + if (!check) + return; + + if (PyGILState_Check()) + return; + + check = 0; + Py_FatalError("GIL must be held to call PyMem_Malloc and PyObject_Malloc"); +} + /* Let S = sizeof(size_t). The debug malloc asks for 4*S extra bytes and fills them with useful stuff, here calling the underlying malloc's result p: @@ -1717,6 +1733,9 @@ static void * /* overflow: can't represent total as a size_t */ return NULL; + if (api->api_id != 'r') + _PyMem_DebugCheckGIL(); + p = (uchar *)api->alloc.malloc(api->alloc.ctx, total); if (p == NULL) return NULL; @@ -1756,6 +1775,10 @@ static void nbytes += 4*SST; if (nbytes > 0) memset(q, DEADBYTE, nbytes); + + if (api->api_id != 'r') + _PyMem_DebugCheckGIL(); + api->alloc.free(api->alloc.ctx, q); } @@ -1785,6 +1808,9 @@ static void * memset(q + nbytes, DEADBYTE, original_nbytes - nbytes + 2*SST); } + if (api->api_id != 'r') + _PyMem_DebugCheckGIL(); + /* Resize and add decorations. We may get a new pointer here, in which * case we didn't get the chance to mark the old memory with DEADBYTE, * but we live with that.