Skip to content

Commit cc71cc9

Browse files
gh-85283: Add PyMem_RawMalloc() to the limited C API (#108570)
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]>
1 parent cf9c25c commit cc71cc9

File tree

8 files changed

+39
-6
lines changed

8 files changed

+39
-6
lines changed

Diff for: Doc/data/stable_abi.dat

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: Doc/whatsnew/3.13.rst

+5
Original file line numberDiff line numberDiff line change
@@ -1053,6 +1053,11 @@ New Features
10531053
parameter names.
10541054
(Contributed by Serhiy Storchaka in :gh:`110815`.)
10551055

1056+
* Add :c:func:`PyMem_RawMalloc`, :c:func:`PyMem_RawCalloc`,
1057+
:c:func:`PyMem_RawRealloc` and :c:func:`PyMem_RawFree` to the limited C API
1058+
(version 3.13).
1059+
(Contributed by Victor Stinner in :gh:`85283`.)
1060+
10561061
Porting to Python 3.13
10571062
----------------------
10581063

Diff for: Include/cpython/pymem.h

-6
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@
22
# error "this header file must not be included directly"
33
#endif
44

5-
PyAPI_FUNC(void *) PyMem_RawMalloc(size_t size);
6-
PyAPI_FUNC(void *) PyMem_RawCalloc(size_t nelem, size_t elsize);
7-
PyAPI_FUNC(void *) PyMem_RawRealloc(void *ptr, size_t new_size);
8-
PyAPI_FUNC(void) PyMem_RawFree(void *ptr);
9-
10-
115
typedef enum {
126
/* PyMem_RawMalloc(), PyMem_RawRealloc() and PyMem_RawFree() */
137
PYMEM_DOMAIN_RAW,

Diff for: Include/pymem.h

+11
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,17 @@ PyAPI_FUNC(void) PyMem_Free(void *ptr);
8787
#define PyMem_DEL(p) PyMem_Free((p))
8888

8989

90+
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030d0000
91+
// Memory allocator which doesn't require the GIL to be held.
92+
// Usually, it's just a thin wrapper to functions of the standard C library:
93+
// malloc(), calloc(), realloc() and free(). The difference is that
94+
// tracemalloc can track these memory allocations.
95+
PyAPI_FUNC(void *) PyMem_RawMalloc(size_t size);
96+
PyAPI_FUNC(void *) PyMem_RawCalloc(size_t nelem, size_t elsize);
97+
PyAPI_FUNC(void *) PyMem_RawRealloc(void *ptr, size_t new_size);
98+
PyAPI_FUNC(void) PyMem_RawFree(void *ptr);
99+
#endif
100+
90101
#ifndef Py_LIMITED_API
91102
# define Py_CPYTHON_PYMEM_H
92103
# include "cpython/pymem.h"

Diff for: Lib/test/test_stable_abi_ctypes.py

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Add :c:func:`PyMem_RawMalloc`, :c:func:`PyMem_RawCalloc`,
2+
:c:func:`PyMem_RawRealloc` and :c:func:`PyMem_RawFree` to the limited C API.
3+
Patch by Victor Stinner.

Diff for: Misc/stable_abi.toml

+8
Original file line numberDiff line numberDiff line change
@@ -2466,3 +2466,11 @@
24662466
added = '3.13'
24672467
[function.PyUnicode_EqualToUTF8AndSize]
24682468
added = '3.13'
2469+
[function.PyMem_RawMalloc]
2470+
added = '3.13'
2471+
[function.PyMem_RawCalloc]
2472+
added = '3.13'
2473+
[function.PyMem_RawRealloc]
2474+
added = '3.13'
2475+
[function.PyMem_RawFree]
2476+
added = '3.13'

Diff for: PC/python3dll.c

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)