Merge from ast-arena. This reduces the code in Python/ast.c by ~300 … · python/cpython@adb69fc · GitHub
Skip to content

Commit adb69fc

Browse files
committed
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
simplifies a lot of error handling code, and fixes many memory leaks.
1 parent 23a6958 commit adb69fc

16 files changed

Lines changed: 704 additions & 1242 deletions

File tree

Include/Python-ast.h

Lines changed: 66 additions & 68 deletions

Include/Python.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113

114114
#include "pystate.h"
115115

116+
#include "pyarena.h"
116117
#include "modsupport.h"
117118
#include "pythonrun.h"
118119
#include "ceval.h"

Include/asdl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ typedef struct {
2323
void *elements[1];
2424
} asdl_seq;
2525

26-
asdl_seq *asdl_seq_new(int size);
26+
asdl_seq *asdl_seq_new(int size, PyArena *arena);
2727
void asdl_seq_free(asdl_seq *);
2828

2929
#ifdef Py_DEBUG

Include/ast.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ extern "C" {
55
#endif
66

77
PyAPI_FUNC(mod_ty) PyAST_FromNode(const node *, PyCompilerFlags *flags,
8-
const char *);
8+
const char *, PyArena *);
99

1010
#ifdef __cplusplus
1111
}

Include/compile.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ typedef struct {
2525

2626
struct _mod; /* Declare the existence of this type */
2727
PyAPI_FUNC(PyCodeObject *) PyAST_Compile(struct _mod *, const char *,
28-
PyCompilerFlags *);
28+
PyCompilerFlags *, PyArena *);
2929
PyAPI_FUNC(PyFutureFeatures *) PyFuture_FromAST(struct _mod *, const char *);
3030

3131
#define ERR_LATE_FUTURE \

Include/pyarena.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/* An arena-like memory interface for the compiler.
2+
*/
3+
4+
#ifndef Py_PYARENA_H
5+
#define Py_PYARENA_H
6+
7+
#ifdef __cplusplus
8+
extern "C" {
9+
#endif
10+
11+
typedef struct _arena PyArena;
12+
13+
/* PyArena_New() and PyArena_Free() create a new arena and free it,
14+
respectively. Once an arena has been created, it can be used
15+
to allocate memory. Once it is freed, all the memory it allocated
16+
is freed and none of its pointers are valid.
17+
18+
PyArena_New() returns an arena pointer. On error, it
19+
returns a negative number and sets an exception.
20+
*/
21+
PyAPI_FUNC(PyArena *) PyArena_New(void);
22+
PyAPI_FUNC(void) PyArena_Free(PyArena *);
23+
24+
PyAPI_FUNC(void *) PyArena_Malloc(PyArena *, size_t);
25+
26+
/* The next two routines aren't proper arena allocation routines.
27+
They exist to experiment with the arena API without making wholesale
28+
changes to the implementation.
29+
30+
The two functions register pointers with the arena id. These
31+
are externally allocated pointers that will be freed when the
32+
arena is freed. One takes a pointer allocated with malloc. The
33+
other takes a PyObject that is DECREFed when the arena is freed.
34+
*/
35+
PyAPI_FUNC(int) PyArena_AddMallocPointer(PyArena *, void *);
36+
PyAPI_FUNC(int) PyArena_AddPyObject(PyArena *, PyObject *);
37+
38+
#ifdef __cplusplus
39+
}
40+
#endif
41+
42+
#endif /* !Py_PYARENA_H */

Include/pythonrun.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ PyAPI_FUNC(int) PyRun_InteractiveOneFlags(FILE *, const char *, PyCompilerFlags
3737
PyAPI_FUNC(int) PyRun_InteractiveLoopFlags(FILE *, const char *, PyCompilerFlags *);
3838

3939
PyAPI_FUNC(struct _mod *) PyParser_ASTFromString(const char *, const char *,
40-
int, PyCompilerFlags *flags);
40+
int, PyCompilerFlags *flags,
41+
PyArena *);
4142
PyAPI_FUNC(struct _mod *) PyParser_ASTFromFile(FILE *, const char *, int,
4243
char *, char *,
43-
PyCompilerFlags *, int *);
44+
PyCompilerFlags *, int *,
45+
PyArena *);
4446
#define PyParser_SimpleParseString(S, B) \
4547
PyParser_SimpleParseStringFlags(S, B, 0)
4648
#define PyParser_SimpleParseFile(FP, S, B) \

Makefile.pre.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ PYTHON_OBJS= \
254254
Python/modsupport.o \
255255
Python/mystrtoul.o \
256256
Python/mysnprintf.o \
257+
Python/pyarena.o \
257258
Python/pyfpe.o \
258259
Python/pystate.o \
259260
Python/pythonrun.o \
@@ -520,6 +521,7 @@ PYTHON_HEADERS= \
520521
Include/object.h \
521522
Include/objimpl.h \
522523
Include/patchlevel.h \
524+
Include/pyarena.h \
523525
Include/pydebug.h \
524526
Include/pyerrors.h \
525527
Include/pyfpe.h \

Parser/asdl_c.py

Lines changed: 10 additions & 5 deletions

0 commit comments

Comments
 (0)