Skip to content

Commit 8370e07

Browse files
authored
bpo-43244: Remove the pyarena.h header (pythonGH-25007)
Remove the pyarena.h header file with functions: * PyArena_New() * PyArena_Free() * PyArena_Malloc() * PyArena_AddPyObject() These functions were undocumented, excluded from the limited C API, and were only used internally by the compiler. Add pycore_pyarena.h header. Rename functions: * PyArena_New() => _PyArena_New() * PyArena_Free() => _PyArena_Free() * PyArena_Malloc() => _PyArena_Malloc() * PyArena_AddPyObject() => _PyArena_AddPyObject()
1 parent 919d42d commit 8370e07

20 files changed

+215
-187
lines changed

Doc/whatsnew/3.10.rst

+11
Original file line numberDiff line numberDiff line change
@@ -1419,3 +1419,14 @@ Removed
14191419
14201420
These functions were undocumented and excluded from the limited C API.
14211421
(Contributed by Victor Stinner in :issue:`43244`.)
1422+
1423+
* Remove the ``pyarena.h`` header file with functions:
1424+
1425+
* ``PyArena_New()``
1426+
* ``PyArena_Free()``
1427+
* ``PyArena_Malloc()``
1428+
* ``PyArena_AddPyObject()``
1429+
1430+
These functions were undocumented, excluded from the limited C API, and were
1431+
only used internally by the compiler.
1432+
(Contributed by Victor Stinner in :issue:`43244`.)

Include/Python.h

-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@
137137
#include "pystate.h"
138138
#include "context.h"
139139

140-
#include "cpython/pyarena.h"
141140
#include "modsupport.h"
142141
#include "compile.h"
143142
#include "pythonrun.h"

Include/cpython/pyarena.h

-64
This file was deleted.

Include/internal/pycore_asdl.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ extern "C" {
88
# error "this header requires Py_BUILD_CORE define"
99
#endif
1010

11+
#include "pycore_pyarena.h" // _PyArena_Malloc()
12+
1113
typedef PyObject * identifier;
1214
typedef PyObject * string;
1315
typedef PyObject * object;
@@ -65,7 +67,7 @@ asdl_ ## NAME ## _seq *_Py_asdl_ ## NAME ## _seq_new(Py_ssize_t size, PyArena *a
6567
return NULL; \
6668
} \
6769
n += sizeof(asdl_ ## NAME ## _seq); \
68-
seq = (asdl_ ## NAME ## _seq *)PyArena_Malloc(arena, n); \
70+
seq = (asdl_ ## NAME ## _seq *)_PyArena_Malloc(arena, n); \
6971
if (!seq) { \
7072
PyErr_NoMemory(); \
7173
return NULL; \

Include/internal/pycore_compile.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@ extern "C" {
88
# error "this header requires Py_BUILD_CORE define"
99
#endif
1010

11-
struct _mod; // Type defined in pycore_ast.h
11+
struct _arena; // Type defined in pycore_pyarena.h
12+
struct _mod; // Type defined in pycore_ast.h
1213

1314
// Export the symbol for test_peg_generator (built as a library)
1415
PyAPI_FUNC(PyCodeObject*) _PyAST_Compile(
1516
struct _mod *mod,
1617
PyObject *filename,
1718
PyCompilerFlags *flags,
1819
int optimize,
19-
PyArena *arena);
20+
struct _arena *arena);
2021
extern PyFutureFeatures* _PyFuture_FromAST(
2122
struct _mod * mod,
2223
PyObject *filename
@@ -31,7 +32,7 @@ typedef struct {
3132

3233
extern int _PyAST_Optimize(
3334
struct _mod *,
34-
PyArena *arena,
35+
struct _arena *arena,
3536
_PyASTOptimizeState *state);
3637

3738
#ifdef __cplusplus

Include/internal/pycore_pyarena.h

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/* An arena-like memory interface for the compiler.
2+
*/
3+
4+
#ifndef Py_INTERNAL_PYARENA_H
5+
#define Py_INTERNAL_PYARENA_H
6+
#ifdef __cplusplus
7+
extern "C" {
8+
#endif
9+
10+
#ifndef Py_BUILD_CORE
11+
# error "this header requires Py_BUILD_CORE define"
12+
#endif
13+
14+
typedef struct _arena PyArena;
15+
16+
/* _PyArena_New() and _PyArena_Free() create a new arena and free it,
17+
respectively. Once an arena has been created, it can be used
18+
to allocate memory via _PyArena_Malloc(). Pointers to PyObject can
19+
also be registered with the arena via _PyArena_AddPyObject(), and the
20+
arena will ensure that the PyObjects stay alive at least until
21+
_PyArena_Free() is called. When an arena is freed, all the memory it
22+
allocated is freed, the arena releases internal references to registered
23+
PyObject*, and none of its pointers are valid.
24+
XXX (tim) What does "none of its pointers are valid" mean? Does it
25+
XXX mean that pointers previously obtained via _PyArena_Malloc() are
26+
XXX no longer valid? (That's clearly true, but not sure that's what
27+
XXX the text is trying to say.)
28+
29+
_PyArena_New() returns an arena pointer. On error, it
30+
returns a negative number and sets an exception.
31+
XXX (tim): Not true. On error, _PyArena_New() actually returns NULL,
32+
XXX and looks like it may or may not set an exception (e.g., if the
33+
XXX internal PyList_New(0) returns NULL, _PyArena_New() passes that on
34+
XXX and an exception is set; OTOH, if the internal
35+
XXX block_new(DEFAULT_BLOCK_SIZE) returns NULL, that's passed on but
36+
XXX an exception is not set in that case).
37+
*/
38+
PyAPI_FUNC(PyArena*) _PyArena_New(void);
39+
PyAPI_FUNC(void) _PyArena_Free(PyArena *);
40+
41+
/* Mostly like malloc(), return the address of a block of memory spanning
42+
* `size` bytes, or return NULL (without setting an exception) if enough
43+
* new memory can't be obtained. Unlike malloc(0), _PyArena_Malloc() with
44+
* size=0 does not guarantee to return a unique pointer (the pointer
45+
* returned may equal one or more other pointers obtained from
46+
* _PyArena_Malloc()).
47+
* Note that pointers obtained via _PyArena_Malloc() must never be passed to
48+
* the system free() or realloc(), or to any of Python's similar memory-
49+
* management functions. _PyArena_Malloc()-obtained pointers remain valid
50+
* until _PyArena_Free(ar) is called, at which point all pointers obtained
51+
* from the arena `ar` become invalid simultaneously.
52+
*/
53+
PyAPI_FUNC(void*) _PyArena_Malloc(PyArena *, size_t size);
54+
55+
/* This routine isn't a proper arena allocation routine. It takes
56+
* a PyObject* and records it so that it can be DECREFed when the
57+
* arena is freed.
58+
*/
59+
PyAPI_FUNC(int) _PyArena_AddPyObject(PyArena *, PyObject *);
60+
61+
#ifdef __cplusplus
62+
}
63+
#endif
64+
#endif /* !Py_INTERNAL_PYARENA_H */

Makefile.pre.in

+1-1
Original file line numberDiff line numberDiff line change
@@ -1119,7 +1119,6 @@ PYTHON_HEADERS= \
11191119
$(srcdir)/Include/cpython/objimpl.h \
11201120
$(srcdir)/Include/cpython/odictobject.h \
11211121
$(srcdir)/Include/cpython/picklebufobject.h \
1122-
$(srcdir)/Include/cpython/pyarena.h \
11231122
$(srcdir)/Include/cpython/pyctype.h \
11241123
$(srcdir)/Include/cpython/pydebug.h \
11251124
$(srcdir)/Include/cpython/pyerrors.h \
@@ -1163,6 +1162,7 @@ PYTHON_HEADERS= \
11631162
$(srcdir)/Include/internal/pycore_long.h \
11641163
$(srcdir)/Include/internal/pycore_object.h \
11651164
$(srcdir)/Include/internal/pycore_pathconfig.h \
1165+
$(srcdir)/Include/internal/pycore_pyarena.h \
11661166
$(srcdir)/Include/internal/pycore_pyerrors.h \
11671167
$(srcdir)/Include/internal/pycore_pyhash.h \
11681168
$(srcdir)/Include/internal/pycore_pylifecycle.h \
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Remove the ``pyarena.h`` header file with functions:
2+
3+
* ``PyArena_New()``
4+
* ``PyArena_Free()``
5+
* ``PyArena_Malloc()``
6+
* ``PyArena_AddPyObject()``
7+
8+
These functions were undocumented, excluded from the limited C API, and were
9+
only used internally by the compiler.
10+
Patch by Victor Stinner.

PCbuild/pythoncore.vcxproj

+1
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@
201201
<ClInclude Include="..\Include\internal\pycore_long.h" />
202202
<ClInclude Include="..\Include\internal\pycore_object.h" />
203203
<ClInclude Include="..\Include\internal\pycore_pathconfig.h" />
204+
<ClInclude Include="..\Include\internal\pycore_pyarena.h" />
204205
<ClInclude Include="..\Include\internal\pycore_pyerrors.h" />
205206
<ClInclude Include="..\Include\internal\pycore_pyhash.h" />
206207
<ClInclude Include="..\Include\internal\pycore_pylifecycle.h" />

PCbuild/pythoncore.vcxproj.filters

+3
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,9 @@
564564
<ClInclude Include="..\Include\internal\pycore_pathconfig.h">
565565
<Filter>Include\internal</Filter>
566566
</ClInclude>
567+
<ClInclude Include="..\Include\internal\pycore_pyarena.h">
568+
<Filter>Include\internal</Filter>
569+
</ClInclude>
567570
<ClInclude Include="..\Include\internal\pycore_pyerrors.h">
568571
<Filter>Include\internal</Filter>
569572
</ClInclude>

Parser/asdl_c.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ def emit(s, depth=0, reflow=True):
362362
emit('return NULL;', 2)
363363
emit('}', 1)
364364

365-
emit("p = (%s)PyArena_Malloc(arena, sizeof(*p));" % ctype, 1);
365+
emit("p = (%s)_PyArena_Malloc(arena, sizeof(*p));" % ctype, 1);
366366
emit("if (!p)", 1)
367367
emit("return NULL;", 2)
368368
if union:
@@ -946,7 +946,7 @@ def visitModule(self, mod):
946946
if (obj == Py_None)
947947
obj = NULL;
948948
if (obj) {
949-
if (PyArena_AddPyObject(arena, obj) < 0) {
949+
if (_PyArena_AddPyObject(arena, obj) < 0) {
950950
*out = NULL;
951951
return -1;
952952
}
@@ -958,7 +958,7 @@ def visitModule(self, mod):
958958
959959
static int obj2ast_constant(struct ast_state *Py_UNUSED(state), PyObject* obj, PyObject** out, PyArena* arena)
960960
{
961-
if (PyArena_AddPyObject(arena, obj) < 0) {
961+
if (_PyArena_AddPyObject(arena, obj) < 0) {
962962
*out = NULL;
963963
return -1;
964964
}

0 commit comments

Comments
 (0)