-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
Description
Feature or enhancement
The free-threaded build requires that Python objects -- and only Python objects -- be allocated through the Python object allocation APIs (like PyObject_Malloc()
or PyType_GenericNew()
) 1.
There are a few places internally 2 that use PyObject_Malloc()
for non Python objects. We should switch those call sites to use PyMem_Malloc()/Free()
instead.
Note that there is not a significant difference between using PyObject_Malloc()
and PyMem_Malloc()
in the default build. Both calls use obmalloc under the hood, so switching from one to the other should not matter for the default build.
Here are some examples, but this list may not be exhaustive:
cpython/Modules/_sre/sre_lib.h
Line 1125 in 841eacd
ctx->u.rep = (SRE_REPEAT*) PyObject_Malloc(sizeof(*ctx->u.rep)); |
cpython/Modules/_elementtree.c
Line 270 in 841eacd
self->extra = PyObject_Malloc(sizeof(ElementObjectExtra)); |
cpython/Modules/_elementtree.c
Lines 498 to 499 in 841eacd
children = PyObject_Realloc(self->extra->children, | |
size * sizeof(PyObject*)); |
https://github.com/python/cpython/blob/main/Modules/mathmodule.c#L2573
Lines 23 to 24 in 841eacd
static XML_Memory_Handling_Suite ExpatMemoryHandler = { | |
PyObject_Malloc, PyObject_Realloc, PyObject_Free}; |
cpython/Objects/bytearrayobject.c
Line 135 in 841eacd
new->ob_bytes = PyObject_Malloc(alloc); |
Line 132 in 841eacd
char *result = (char *)PyObject_Malloc((input_length + 1) * sizeof(char)); |
Linked PRs
- gh-114569: Use PyMem_* APIs for non-PyObjects in Modules/, Objects/ and Parser/ #114574
- gh-114569: Use PyMem_* APIs for non-PyObjects in compiler #114587
- gh-114569: Use PyMem_* APIs for non-PyObjects in unicodeobject.c #114690