diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index 8c44ad205b..3f9a032836 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -4148,6 +4148,39 @@ test_pyobject_fastcallkeywords(PyObject *self, PyObject *args) return _PyObject_FastCallKeywords(func, stack, nargs, kwnames); } +static void* nomem_malloc (void* ctx, size_t size) +{ + return PyErr_NoMemory(); +} + +static void* nomem_calloc (void* ctx, size_t nelem, size_t elsize) +{ + return PyErr_NoMemory(); +} + +static void* nomem_realloc (void* ctx, void* ptr, size_t new_size) +{ + return PyErr_NoMemory(); +} + +static void nomem_free (void *ctx, void *ptr) {} + +static PyMemAllocatorEx nomemory_allocator; + +static PyObject * +set_nomemory_allocator(PyObject *self) +{ + nomemory_allocator.ctx = NULL; + nomemory_allocator.malloc = &nomem_malloc; + nomemory_allocator.calloc = &nomem_calloc; + nomemory_allocator.realloc = &nomem_realloc; + nomemory_allocator.free = &nomem_free; + PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &nomemory_allocator); + PyMem_SetAllocator(PYMEM_DOMAIN_MEM, &nomemory_allocator); + PyMem_SetAllocator(PYMEM_DOMAIN_OBJ, &nomemory_allocator); + Py_RETURN_NONE; +} + static PyMethodDef TestMethods[] = { {"raise_exception", raise_exception, METH_VARARGS}, @@ -4353,6 +4386,7 @@ static PyMethodDef TestMethods[] = { {"tracemalloc_untrack", tracemalloc_untrack, METH_VARARGS}, {"tracemalloc_get_traceback", tracemalloc_get_traceback, METH_VARARGS}, {"dict_get_version", dict_get_version, METH_VARARGS}, + {"set_nomemory_allocator", (PyCFunction)set_nomemory_allocator, METH_NOARGS}, {"raise_SIGINT_then_send_None", raise_SIGINT_then_send_None, METH_VARARGS}, {"pyobject_fastcall", test_pyobject_fastcall, METH_VARARGS}, {"pyobject_fastcalldict", test_pyobject_fastcalldict, METH_VARARGS},