gh-85283: Add PyMem_RawMalloc() to the limited C API#108570
Conversation
92484f4 to
fa30035
Compare
August 28, 2023 15:46
|
See also What To Do About Custom Allocators and the GIL? discussion. I don't think that PyMem_RawMalloc() is causing any trouble here. |
Sorry, something went wrong.
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. |
Sorry, something went wrong.
|
If nogil/mimalloc is merged, will there be any reason to use these instead of |
Sorry, something went wrong.
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 |
Sorry, something went wrong.
|
Code search using regex The following 34 projects use
|
Sorry, something went wrong.
|
What @vstinner wrote about nogil is accurate, except that |
Sorry, something went wrong.
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". |
Sorry, something went wrong.
|
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 |
Sorry, something went wrong.
Just to clarify, the GIL must still be held when using the "mem" and "object" domains ( |
Sorry, something went wrong.
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):
|
Sorry, something went wrong.
|
@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? |
Sorry, something went wrong.
|
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.) |
Sorry, something went wrong.
|
Seems fine to me too. (To be clear, I don't think this affects PEP 703 one way or the other.) |
Sorry, something went wrong.
2cc3b9a to
ae2f355
Compare
September 30, 2023 21:00
|
I rebased my PR on the main branch. |
Sorry, something went wrong.
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
October 17, 2023 00:06
|
I merged my PR, thanks for reviews. It should help many projects to use the limited C API. |
Sorry, something went wrong.
…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 <erlend@python.org>
…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 <erlend@python.org>
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/