-
-
Notifications
You must be signed in to change notification settings - Fork 31k
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
gh-85283: Add PyMem_RawMalloc() to the limited C API #108570
Conversation
92484f4
to
fa30035
Compare
See also What To Do About Custom Allocators and the GIL? discussion. I don't think that PyMem_RawMalloc() is causing any trouble here. |
The alternative is to no add these functions to limited C API, and convert grp and pwd to libc malloc() and free(). The drawback is that tracemalloc will no longer be able to trace their memory allocation. |
If nogil/mimalloc is merged, will there be any reason to use these instead of |
I understood that with the nogil implementation:
For me, PyMem_RawMalloc() will still be different than PyMem_RawMalloc(). On Python 3.12 and older, PyMem_RawMalloc() must be used if the GIL is not held. Such case was not compatible with the limited C API, since PyMem_RawMalloc() is not part of Python 3.12 limited C API. Having PyMem_RawMalloc() in the limited C API may help compatibility with older Python versions (which use the regular API, not the limited API). cc @colesbury |
Code search using regex The following 34 projects use
|
What @vstinner wrote about nogil is accurate, except that |
The important information is that even with nogil, PyMem_RawMalloc() still fit an use case which is not fitted by PyMem_Malloc(). So it's still relevant to use PyMem_RawMalloc() in "nogil Python". |
This shouldn't affect per-interpreter GIL since the only specific concern there is thread-safety. Note that the docs now (3.12+) say that all 3 domains must be thread-safe. (This was discussed in a DPO thread and changed for gh-105766.) So does the rationale here apply to the other two memory domains ( As to my opinion about this change, I think we should be a bit more cautious about expanding the limited API until we have more clarity on where we're going with the C-API (hopefully at the core sprint). I'm particularly reluctant to add |
Just to clarify, the GIL must still be held when using the "mem" and "object" domains ( |
My long term goal doesn't concern the Python stdlib extensions, but just converting all C extensions towards the limited C API. Cython uses A quick search on PyPI top 5,000 projects tells me that 113 projects use
Affected projects (113):
|
@encukou @colesbury @ericsnowcurrently: I'm sorry, I didn't get your opinion about adding these 4 functions to the limited C API. Do you approve this change? Or are you against it? |
For just (In general, however, I think we should slow down on moving things to the limited API until after the discussions at the core sprint next month. I'm not saying that should block this PR though.) |
Seems fine to me too. (To be clear, I don't think this affects PEP 703 one way or the other.) |
2cc3b9a
to
ae2f355
Compare
I rebased my PR on the main branch. |
Add PyMem_RawMalloc(), PyMem_RawCalloc(), PyMem_RawRealloc() and PyMem_RawFree() to the limited C API. These functions were added by Python 3.4 and are needed to port stdlib extensions to the limited C API, like grp and pwd.
ae2f355
to
940ddb1
Compare
I merged my PR, thanks for reviews. It should help many projects to use the limited C API. |
…8570) Add PyMem_RawMalloc(), PyMem_RawCalloc(), PyMem_RawRealloc() and PyMem_RawFree() to the limited C API. These functions were added by Python 3.4 and are needed to port stdlib extensions to the limited C API, like grp and pwd. Co-authored-by: Erlend E. Aasland <[email protected]>
…8570) Add PyMem_RawMalloc(), PyMem_RawCalloc(), PyMem_RawRealloc() and PyMem_RawFree() to the limited C API. These functions were added by Python 3.4 and are needed to port stdlib extensions to the limited C API, like grp and pwd. Co-authored-by: Erlend E. Aasland <[email protected]>
Add PyMem_RawMalloc(), PyMem_RawCalloc(), PyMem_RawRealloc() and PyMem_RawFree() to the limited C API.
These functions were added by Python 3.4 and are needed to port stdlib extensions to the limited C API, like grp and pwd.
📚 Documentation preview 📚: https://cpython-previews--108570.org.readthedocs.build/