-
-
Notifications
You must be signed in to change notification settings - Fork 30.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[C-API Docs] Clarify About When PyMem_SetAllocator() Can Be Called #96997
Comments
CC @vstinner |
Victor mentioned that some limitations for
|
I am preparing a PR to document this in the API docs, which I think is the correct place. This should also be a warning box, I think |
Pablo's documentation LGTM. |
Allocating memory is easy. The problem is more to know how to release allocated memory :-) Python/initconfig.c temporarily override the memory allocators to get a known memory allocator at exit. Example: /* Py_SetStandardStreamEncoding() can be called before Py_Initialize(),
but Py_Initialize() can change the allocator. Use a known allocator
to be able to release the memory later. */
PyMemAllocatorEx old_alloc;
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
// ... code using PyMem_RawMalloc() and PyMem_RawFree() ...
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc); An alternative would be to add functions with a "context" parameter which can be used to get memory allocator functions. I considered that for Python/fileutils.c functions like Py_DecodeLocale() and _Py_EncodeLocaleRaw() which use PyMem_RawMalloc(). But I wasn't convinced that it's worth it, since Code search, where I annotated public functions:
|
There are two uses for
PyMem_SetAllocator()
:The first must be done (only by embedders) "just after the pre-initialization, and before the Python initialization", while the second may be done at any point (e.g. by extension modules, like tracemalloc). See #96975 (comment).
The docs for
PyMem_SetAllocator()
should make the distinction more clear, as well as identifying the restriction on (1).The text was updated successfully, but these errors were encountered: