Skip to content
This repository was archived by the owner on Jul 5, 2023. It is now read-only.

Commit f3355fd

Browse files
authored
Fix compilation on python 3.10.0a7 (#158)
PyArena was removed from the public api in python/cpython#25007 This commit adds two new files (a copy of each for ast27 and ast3): * pycore_pyarena.h: Taken from the cpython source code with minimal changes * pyarena.h: Maps the new, underscored function names to the old function names, allowing the code to work on both python 3.10 and older versions
1 parent adfe297 commit f3355fd

File tree

6 files changed

+156
-0
lines changed

6 files changed

+156
-0
lines changed

ast27/Include/asdl.h

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef Ta27_ASDL_H
22
#define Ta27_ASDL_H
33

4+
#include "../Include/pyarena.h"
5+
46
typedef PyObject * identifier;
57
typedef PyObject * string;
68
typedef PyObject * object;

ast27/Include/pyarena.h

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* An arena-like memory interface for the compiler.
2+
*/
3+
4+
#ifndef Ta27_PYARENA_H
5+
#define Ta27_PYARENA_H
6+
7+
#if PY_MINOR_VERSION >= 10
8+
#include "../Include/pycore_pyarena.h"
9+
10+
#define PyArena_New _PyArena_New
11+
#define PyArena_Free _PyArena_Free
12+
#define PyArena_Malloc _PyArena_Malloc
13+
#define PyArena_AddPyObject _PyArena_AddPyObject
14+
#endif
15+
16+
#endif /* !Ta27_PYARENA_H */

ast27/Include/pycore_pyarena.h

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

ast3/Include/asdl.h

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef Ta3_ASDL_H
22
#define Ta3_ASDL_H
33

4+
#include "../Include/pyarena.h"
5+
46
typedef PyObject * identifier;
57
typedef PyObject * string;
68
typedef PyObject * bytes;

ast3/Include/pyarena.h

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* An arena-like memory interface for the compiler.
2+
*/
3+
4+
#ifndef Ta3_PYARENA_H
5+
#define Ta3_PYARENA_H
6+
7+
#if PY_MINOR_VERSION >= 10
8+
#include "../Include/pycore_pyarena.h"
9+
10+
#define PyArena_New _PyArena_New
11+
#define PyArena_Free _PyArena_Free
12+
#define PyArena_Malloc _PyArena_Malloc
13+
#define PyArena_AddPyObject _PyArena_AddPyObject
14+
#endif
15+
16+
#endif /* !Ta3_PYARENA_H */

ast3/Include/pycore_pyarena.h

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

0 commit comments

Comments
 (0)