diff --git a/ast27/Include/asdl.h b/ast27/Include/asdl.h index 7b9cf3f5..40df2224 100644 --- a/ast27/Include/asdl.h +++ b/ast27/Include/asdl.h @@ -34,8 +34,8 @@ typedef struct { #define _Py_asdl_seq_new asdl_seq_new #define _Py_asdl_int_seq_new asdl_int_seq_new #endif -asdl_seq *asdl_seq_new(int size, PyArena *arena); -asdl_int_seq *asdl_int_seq_new(int size, PyArena *arena); +asdl_seq *asdl_seq_new(Py_ssize_t size, PyArena *arena); +asdl_int_seq *asdl_int_seq_new(Py_ssize_t size, PyArena *arena); #define asdl_seq_GET(S, I) (S)->elements[(I)] #define asdl_seq_LEN(S) ((S) == NULL ? 0 : (S)->size) diff --git a/ast27/Include/ast.h b/ast27/Include/ast.h index 2db1a2b1..2911440a 100644 --- a/ast27/Include/ast.h +++ b/ast27/Include/ast.h @@ -4,7 +4,7 @@ extern "C" { #endif -PyAPI_FUNC(mod_ty) Ta27AST_FromNode(const node *, PyCompilerFlags *flags, +mod_ty Ta27AST_FromNode(const node *, PyCompilerFlags *flags, const char *, PyArena *); #ifdef __cplusplus diff --git a/ast27/Include/node.h b/ast27/Include/node.h index 32ced76b..32f9b173 100644 --- a/ast27/Include/node.h +++ b/ast27/Include/node.h @@ -16,11 +16,11 @@ typedef struct _node { struct _node *n_child; } node; -PyAPI_FUNC(node *) Ta27Node_New(int type); -PyAPI_FUNC(int) Ta27Node_AddChild(node *n, int type, - char *str, int lineno, int col_offset); -PyAPI_FUNC(void) Ta27Node_Free(node *n); -PyAPI_FUNC(Py_ssize_t) _Ta27Node_SizeOf(node *n); +node *Ta27Node_New(int type); +int Ta27Node_AddChild(node *n, int type, + char *str, int lineno, int col_offset); +void Ta27Node_Free(node *n); +Py_ssize_t _Ta27Node_SizeOf(node *n); /* Node access functions */ #define NCH(n) ((n)->n_nchildren) diff --git a/ast27/Include/parsetok.h b/ast27/Include/parsetok.h index ca652b81..38fc2e43 100644 --- a/ast27/Include/parsetok.h +++ b/ast27/Include/parsetok.h @@ -33,30 +33,30 @@ typedef struct { #define PyPARSE_IGNORE_COOKIE 0x0010 -PyAPI_FUNC(node *) Ta27Parser_ParseString(const char *, grammar *, int, - perrdetail *); -PyAPI_FUNC(node *) Ta27Parser_ParseFile (FILE *, const char *, grammar *, int, - char *, char *, perrdetail *); +node *Ta27Parser_ParseString(const char *, grammar *, int, + perrdetail *); +node *Ta27Parser_ParseFile (FILE *, const char *, grammar *, int, + char *, char *, perrdetail *); -PyAPI_FUNC(node *) Ta27Parser_ParseStringFlags(const char *, grammar *, int, - perrdetail *, int); -PyAPI_FUNC(node *) Ta27Parser_ParseFileFlags(FILE *, const char *, grammar *, - int, char *, char *, - perrdetail *, int); -PyAPI_FUNC(node *) Ta27Parser_ParseFileFlagsEx(FILE *, const char *, grammar *, - int, char *, char *, - perrdetail *, int *); +node *Ta27Parser_ParseStringFlags(const char *, grammar *, int, + perrdetail *, int); +node *Ta27Parser_ParseFileFlags(FILE *, const char *, grammar *, + int, char *, char *, + perrdetail *, int); +node *Ta27Parser_ParseFileFlagsEx(FILE *, const char *, grammar *, + int, char *, char *, + perrdetail *, int *); -PyAPI_FUNC(node *) Ta27Parser_ParseStringFlagsFilename(const char *, - const char *, - grammar *, int, - perrdetail *, int); -PyAPI_FUNC(node *) Ta27Parser_ParseStringFlagsFilenameEx(const char *, +node *Ta27Parser_ParseStringFlagsFilename(const char *, + const char *, + grammar *, int, + perrdetail *, int); +node *Ta27Parser_ParseStringFlagsFilenameEx(const char *, const char *, grammar *, int, perrdetail *, int *); -PyAPI_FUNC(node *) Ta27Parser_ParseStringObject( +node *Ta27Parser_ParseStringObject( const char *s, PyObject *filename, grammar *g, diff --git a/ast27/Include/token.h b/ast27/Include/token.h index 21578e1c..8eab738c 100644 --- a/ast27/Include/token.h +++ b/ast27/Include/token.h @@ -77,10 +77,10 @@ extern "C" { #define ISEOF(x) ((x) == ENDMARKER) -PyAPI_DATA(char *) _Ta27Parser_TokenNames[]; /* Token names */ -PyAPI_FUNC(int) Ta27Token_OneChar(int); -PyAPI_FUNC(int) Ta27Token_TwoChars(int, int); -PyAPI_FUNC(int) Ta27Token_ThreeChars(int, int, int); +extern char *_Ta27Parser_TokenNames[]; /* Token names */ +int Ta27Token_OneChar(int); +int Ta27Token_TwoChars(int, int); +int Ta27Token_ThreeChars(int, int, int); #ifdef __cplusplus } diff --git a/ast27/Parser/asdl_c.py b/ast27/Parser/asdl_c.py index 145f7f70..2c1d87c2 100755 --- a/ast27/Parser/asdl_c.py +++ b/ast27/Parser/asdl_c.py @@ -667,7 +667,7 @@ def visitModule(self, mod): }; static PyTypeObject AST_type = { - PyVarObject_HEAD_INIT(&PyType_Type, 0) + PyVarObject_HEAD_INIT(NULL, 0) "_ast27.AST", sizeof(PyObject), 0, diff --git a/ast27/Parser/grammar.c b/ast27/Parser/grammar.c index e21fb871..73b4d1fd 100644 --- a/ast27/Parser/grammar.c +++ b/ast27/Parser/grammar.c @@ -13,7 +13,7 @@ #include #endif -extern int Py_DebugFlag; +PyAPI_DATA(int) Py_DebugFlag; grammar * newgrammar(int start) diff --git a/ast27/Parser/tokenizer.c b/ast27/Parser/tokenizer.c index e435d497..29e88d5a 100644 --- a/ast27/Parser/tokenizer.c +++ b/ast27/Parser/tokenizer.c @@ -20,9 +20,9 @@ #endif /* PGEN */ #if PY_MINOR_VERSION >= 4 -extern char *PyOS_Readline(FILE *, FILE *, const char *); +PyAPI_FUNC(char *) PyOS_Readline(FILE *, FILE *, const char *); #else -extern char *PyOS_Readline(FILE *, FILE *, char *); +PyAPI_FUNC(char *) PyOS_Readline(FILE *, FILE *, char *); #endif /* Return malloc'ed string including trailing \n; empty malloc'ed string for EOF; diff --git a/ast27/Python/Python-ast.c b/ast27/Python/Python-ast.c index 315a2318..0d1212ae 100644 --- a/ast27/Python/Python-ast.c +++ b/ast27/Python/Python-ast.c @@ -476,7 +476,7 @@ static PyMethodDef ast_type_methods[] = { }; static PyTypeObject AST_type = { - PyVarObject_HEAD_INIT(&PyType_Type, 0) + PyVarObject_HEAD_INIT(NULL, 0) "_ast27.AST", sizeof(PyObject), 0, diff --git a/ast27/Python/asdl.c b/ast27/Python/asdl.c new file mode 100644 index 00000000..df387b21 --- /dev/null +++ b/ast27/Python/asdl.c @@ -0,0 +1,64 @@ +#include "Python.h" +#include "asdl.h" + +asdl_seq * +_Py_asdl_seq_new(Py_ssize_t size, PyArena *arena) +{ + asdl_seq *seq = NULL; + size_t n; + + /* check size is sane */ + if (size < 0 || + (size && (((size_t)size - 1) > (PY_SIZE_MAX / sizeof(void *))))) { + PyErr_NoMemory(); + return NULL; + } + n = (size ? (sizeof(void *) * (size - 1)) : 0); + + /* check if size can be added safely */ + if (n > PY_SIZE_MAX - sizeof(asdl_seq)) { + PyErr_NoMemory(); + return NULL; + } + n += sizeof(asdl_seq); + + seq = (asdl_seq *)PyArena_Malloc(arena, n); + if (!seq) { + PyErr_NoMemory(); + return NULL; + } + memset(seq, 0, n); + seq->size = size; + return seq; +} + +asdl_int_seq * +_Py_asdl_int_seq_new(Py_ssize_t size, PyArena *arena) +{ + asdl_int_seq *seq = NULL; + size_t n; + + /* check size is sane */ + if (size < 0 || + (size && (((size_t)size - 1) > (PY_SIZE_MAX / sizeof(void *))))) { + PyErr_NoMemory(); + return NULL; + } + n = (size ? (sizeof(void *) * (size - 1)) : 0); + + /* check if size can be added safely */ + if (n > PY_SIZE_MAX - sizeof(asdl_seq)) { + PyErr_NoMemory(); + return NULL; + } + n += sizeof(asdl_seq); + + seq = (asdl_int_seq *)PyArena_Malloc(arena, n); + if (!seq) { + PyErr_NoMemory(); + return NULL; + } + memset(seq, 0, n); + seq->size = size; + return seq; +} diff --git a/ast27/Python/graminit.c b/ast27/Python/graminit.c index c61ce804..f8ff327c 100644 --- a/ast27/Python/graminit.c +++ b/ast27/Python/graminit.c @@ -2,7 +2,7 @@ #include "pgenheaders.h" #include "grammar.h" -PyAPI_DATA(grammar) _Ta27Parser_Grammar; +grammar _Ta27Parser_Grammar; static arc arcs_0_0[3] = { {2, 1}, {3, 1}, diff --git a/ast35/Include/ast.h b/ast35/Include/ast.h index d9926d48..50bba6af 100644 --- a/ast35/Include/ast.h +++ b/ast35/Include/ast.h @@ -4,13 +4,13 @@ extern "C" { #endif -PyAPI_FUNC(int) Ta35AST_Validate(mod_ty); -PyAPI_FUNC(mod_ty) Ta35AST_FromNode( +int Ta35AST_Validate(mod_ty); +mod_ty Ta35AST_FromNode( const node *n, PyCompilerFlags *flags, const char *filename, /* decoded from the filesystem encoding */ PyArena *arena); -PyAPI_FUNC(mod_ty) Ta35AST_FromNodeObject( +mod_ty Ta35AST_FromNodeObject( const node *n, PyCompilerFlags *flags, PyObject *filename, diff --git a/ast35/Include/node.h b/ast35/Include/node.h index 82f23c36..27dc06fd 100644 --- a/ast35/Include/node.h +++ b/ast35/Include/node.h @@ -16,12 +16,12 @@ typedef struct _node { struct _node *n_child; } node; -PyAPI_FUNC(node *) Ta35Node_New(int type); -PyAPI_FUNC(int) Ta35Node_AddChild(node *n, int type, +node *Ta35Node_New(int type); +int Ta35Node_AddChild(node *n, int type, char *str, int lineno, int col_offset); -PyAPI_FUNC(void) Ta35Node_Free(node *n); +void Ta35Node_Free(node *n); #ifndef Py_LIMITED_API -PyAPI_FUNC(Py_ssize_t) _Ta35Node_SizeOf(node *n); +Py_ssize_t _Ta35Node_SizeOf(node *n); #endif /* Node access functions */ @@ -36,7 +36,7 @@ PyAPI_FUNC(Py_ssize_t) _Ta35Node_SizeOf(node *n); /* Assert that the type of a node is what we expect */ #define REQ(n, type) assert(TYPE(n) == (type)) -PyAPI_FUNC(void) Ta35Node_ListTree(node *); +void Ta35Node_ListTree(node *); #ifdef __cplusplus } diff --git a/ast35/Include/parsetok.h b/ast35/Include/parsetok.h index cacc4921..c7bd1940 100644 --- a/ast35/Include/parsetok.h +++ b/ast35/Include/parsetok.h @@ -35,15 +35,15 @@ typedef struct { #define PyPARSE_IGNORE_COOKIE 0x0010 #define PyPARSE_BARRY_AS_BDFL 0x0020 -PyAPI_FUNC(node *) Ta35Parser_ParseString(const char *, grammar *, int, +node *Ta35Parser_ParseString(const char *, grammar *, int, perrdetail *); -PyAPI_FUNC(node *) Ta35Parser_ParseFile (FILE *, const char *, grammar *, int, +node *Ta35Parser_ParseFile (FILE *, const char *, grammar *, int, const char *, const char *, perrdetail *); -PyAPI_FUNC(node *) Ta35Parser_ParseStringFlags(const char *, grammar *, int, +node *Ta35Parser_ParseStringFlags(const char *, grammar *, int, perrdetail *, int); -PyAPI_FUNC(node *) Ta35Parser_ParseFileFlags( +node *Ta35Parser_ParseFileFlags( FILE *fp, const char *filename, /* decoded from the filesystem encoding */ const char *enc, @@ -53,7 +53,7 @@ PyAPI_FUNC(node *) Ta35Parser_ParseFileFlags( const char *ps2, perrdetail *err_ret, int flags); -PyAPI_FUNC(node *) Ta35Parser_ParseFileFlagsEx( +node *Ta35Parser_ParseFileFlagsEx( FILE *fp, const char *filename, /* decoded from the filesystem encoding */ const char *enc, @@ -63,7 +63,7 @@ PyAPI_FUNC(node *) Ta35Parser_ParseFileFlagsEx( const char *ps2, perrdetail *err_ret, int *flags); -PyAPI_FUNC(node *) Ta35Parser_ParseFileObject( +node *Ta35Parser_ParseFileObject( FILE *fp, PyObject *filename, const char *enc, @@ -74,21 +74,21 @@ PyAPI_FUNC(node *) Ta35Parser_ParseFileObject( perrdetail *err_ret, int *flags); -PyAPI_FUNC(node *) Ta35Parser_ParseStringFlagsFilename( +node *Ta35Parser_ParseStringFlagsFilename( const char *s, const char *filename, /* decoded from the filesystem encoding */ grammar *g, int start, perrdetail *err_ret, int flags); -PyAPI_FUNC(node *) Ta35Parser_ParseStringFlagsFilenameEx( +node *Ta35Parser_ParseStringFlagsFilenameEx( const char *s, const char *filename, /* decoded from the filesystem encoding */ grammar *g, int start, perrdetail *err_ret, int *flags); -PyAPI_FUNC(node *) Ta35Parser_ParseStringObject( +node *Ta35Parser_ParseStringObject( const char *s, PyObject *filename, grammar *g, diff --git a/ast35/Include/token.h b/ast35/Include/token.h index 3a797057..f4a4e32f 100644 --- a/ast35/Include/token.h +++ b/ast35/Include/token.h @@ -80,10 +80,10 @@ extern "C" { #define ISEOF(x) ((x) == ENDMARKER) -PyAPI_DATA(const char *) _Ta35Parser_TokenNames[]; /* Token names */ -PyAPI_FUNC(int) Ta35Token_OneChar(int); -PyAPI_FUNC(int) Ta35Token_TwoChars(int, int); -PyAPI_FUNC(int) Ta35Token_ThreeChars(int, int, int); +extern const char *_Ta35Parser_TokenNames[]; /* Token names */ +int Ta35Token_OneChar(int); +int Ta35Token_TwoChars(int, int); +int Ta35Token_ThreeChars(int, int, int); #ifdef __cplusplus } diff --git a/ast35/Parser/asdl_c.py b/ast35/Parser/asdl_c.py index cabc2416..eacaea0f 100755 --- a/ast35/Parser/asdl_c.py +++ b/ast35/Parser/asdl_c.py @@ -724,7 +724,7 @@ def visitModule(self, mod): }; static PyTypeObject AST_type = { - PyVarObject_HEAD_INIT(&PyType_Type, 0) + PyVarObject_HEAD_INIT(NULL, 0) "_ast35.AST", sizeof(AST_object), 0, diff --git a/ast35/Parser/grammar.c b/ast35/Parser/grammar.c index f6c91c51..046a6e78 100644 --- a/ast35/Parser/grammar.c +++ b/ast35/Parser/grammar.c @@ -9,7 +9,7 @@ #include "token.h" #include "grammar.h" -extern int Py_DebugFlag; +PyAPI_DATA(int) Py_DebugFlag; grammar * newgrammar(int start) diff --git a/ast35/Parser/tokenizer.c b/ast35/Parser/tokenizer.c index de122585..19aaa00e 100644 --- a/ast35/Parser/tokenizer.c +++ b/ast35/Parser/tokenizer.c @@ -32,9 +32,9 @@ || (c >= 128)) #if PY_MINOR_VERSION >= 4 -extern char *PyOS_Readline(FILE *, FILE *, const char *); +PyAPI_FUNC(char *) PyOS_Readline(FILE *, FILE *, const char *); #else -extern char *PyOS_Readline(FILE *, FILE *, char *); +PyAPY_FUNC(char *) PyOS_Readline(FILE *, FILE *, char *); #endif /* Return malloc'ed string including trailing \n; empty malloc'ed string for EOF; diff --git a/ast35/Python/Python-ast.c b/ast35/Python/Python-ast.c index ce80939e..d852b9c3 100644 --- a/ast35/Python/Python-ast.c +++ b/ast35/Python/Python-ast.c @@ -610,7 +610,7 @@ static PyGetSetDef ast_type_getsets[] = { }; static PyTypeObject AST_type = { - PyVarObject_HEAD_INIT(&PyType_Type, 0) + PyVarObject_HEAD_INIT(NULL, 0) "_ast35.AST", sizeof(AST_object), 0, diff --git a/ast35/Python/asdl.c b/ast35/Python/asdl.c new file mode 100644 index 00000000..df387b21 --- /dev/null +++ b/ast35/Python/asdl.c @@ -0,0 +1,64 @@ +#include "Python.h" +#include "asdl.h" + +asdl_seq * +_Py_asdl_seq_new(Py_ssize_t size, PyArena *arena) +{ + asdl_seq *seq = NULL; + size_t n; + + /* check size is sane */ + if (size < 0 || + (size && (((size_t)size - 1) > (PY_SIZE_MAX / sizeof(void *))))) { + PyErr_NoMemory(); + return NULL; + } + n = (size ? (sizeof(void *) * (size - 1)) : 0); + + /* check if size can be added safely */ + if (n > PY_SIZE_MAX - sizeof(asdl_seq)) { + PyErr_NoMemory(); + return NULL; + } + n += sizeof(asdl_seq); + + seq = (asdl_seq *)PyArena_Malloc(arena, n); + if (!seq) { + PyErr_NoMemory(); + return NULL; + } + memset(seq, 0, n); + seq->size = size; + return seq; +} + +asdl_int_seq * +_Py_asdl_int_seq_new(Py_ssize_t size, PyArena *arena) +{ + asdl_int_seq *seq = NULL; + size_t n; + + /* check size is sane */ + if (size < 0 || + (size && (((size_t)size - 1) > (PY_SIZE_MAX / sizeof(void *))))) { + PyErr_NoMemory(); + return NULL; + } + n = (size ? (sizeof(void *) * (size - 1)) : 0); + + /* check if size can be added safely */ + if (n > PY_SIZE_MAX - sizeof(asdl_seq)) { + PyErr_NoMemory(); + return NULL; + } + n += sizeof(asdl_seq); + + seq = (asdl_int_seq *)PyArena_Malloc(arena, n); + if (!seq) { + PyErr_NoMemory(); + return NULL; + } + memset(seq, 0, n); + seq->size = size; + return seq; +} diff --git a/ast35/Python/graminit.c b/ast35/Python/graminit.c index 75b05de4..9ce881a8 100644 --- a/ast35/Python/graminit.c +++ b/ast35/Python/graminit.c @@ -2,7 +2,7 @@ #include "pgenheaders.h" #include "grammar.h" -PyAPI_DATA(grammar) _Ta35Parser_Grammar; +grammar _Ta35Parser_Grammar; static arc arcs_0_0[3] = { {2, 1}, {3, 1}, diff --git a/setup.py b/setup.py index 1cd79e59..b3dc6adc 100644 --- a/setup.py +++ b/setup.py @@ -19,6 +19,7 @@ 'ast27/Parser/parser.c', 'ast27/Parser/parsetok.c', 'ast27/Parser/tokenizer.c', + 'ast27/Python/asdl.c', 'ast27/Python/ast.c', 'ast27/Python/graminit.c', 'ast27/Python/mystrtoul.c', @@ -54,6 +55,7 @@ 'ast35/Parser/parser.c', 'ast35/Parser/parsetok.c', 'ast35/Parser/tokenizer.c', + 'ast35/Python/asdl.c', 'ast35/Python/ast.c', 'ast35/Python/graminit.c', 'ast35/Python/Python-ast.c',