From 72e4f4d7bc516b6d7692a07b1a6c812871c4fd4a Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Sat, 19 Jan 2019 12:04:08 -0800 Subject: [PATCH] Define flags to turn on parsing type comments Demo: `compile(sample, "", "exec", PyCF_ONLY_AST|0x1000)` This will crash if type comments are present. It doesn't *work* yet -- type comments cause crashes in ast.c and type ignore comments aren't passed on to Module yet. --- Include/compile.h | 1 + Include/parsetok.h | 1 + Parser/parsetok.c | 6 ++++++ Python/bltinmodule.c | 2 +- Python/pythonrun.c | 2 ++ 5 files changed, 11 insertions(+), 1 deletion(-) diff --git a/Include/compile.h b/Include/compile.h index 2dacfff37f8c..36477c01e2ba 100644 --- a/Include/compile.h +++ b/Include/compile.h @@ -22,6 +22,7 @@ PyAPI_FUNC(PyCodeObject *) PyNode_Compile(struct _node *, const char *); #define PyCF_DONT_IMPLY_DEDENT 0x0200 #define PyCF_ONLY_AST 0x0400 #define PyCF_IGNORE_COOKIE 0x0800 +#define PyCF_TYPE_COMMENTS 0x1000 #ifndef Py_LIMITED_API typedef struct { diff --git a/Include/parsetok.h b/Include/parsetok.h index 1217c46d0ed7..e95dd31fb79a 100644 --- a/Include/parsetok.h +++ b/Include/parsetok.h @@ -37,6 +37,7 @@ typedef struct { #define PyPARSE_IGNORE_COOKIE 0x0010 #define PyPARSE_BARRY_AS_BDFL 0x0020 +#define PyPARSE_TYPE_COMMENTS 0x0040 PyAPI_FUNC(node *) PyParser_ParseString(const char *, grammar *, int, perrdetail *); diff --git a/Parser/parsetok.c b/Parser/parsetok.c index 40db8a2b8290..042fff71a02f 100644 --- a/Parser/parsetok.c +++ b/Parser/parsetok.c @@ -91,6 +91,9 @@ PyParser_ParseStringObject(const char *s, PyObject *filename, err_ret->error = PyErr_Occurred() ? E_DECODE : E_NOMEM; return NULL; } + if (*flags & PyPARSE_TYPE_COMMENTS) { + tok->type_comments = 1; + } #ifndef PGEN Py_INCREF(err_ret->filename); @@ -159,6 +162,9 @@ PyParser_ParseFileObject(FILE *fp, PyObject *filename, err_ret->error = E_NOMEM; return NULL; } + if (*flags & PyPARSE_TYPE_COMMENTS) { + tok->type_comments = 1; + } #ifndef PGEN Py_INCREF(err_ret->filename); tok->filename = err_ret->filename; diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 332142fc6ffc..930adb496e14 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -771,7 +771,7 @@ builtin_compile_impl(PyObject *module, PyObject *source, PyObject *filename, cf.cf_flags = flags | PyCF_SOURCE_IS_UTF8; if (flags & - ~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_DONT_IMPLY_DEDENT | PyCF_ONLY_AST)) + ~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_DONT_IMPLY_DEDENT | PyCF_ONLY_AST | PyCF_TYPE_COMMENTS)) { PyErr_SetString(PyExc_ValueError, "compile(): unrecognised flags"); diff --git a/Python/pythonrun.c b/Python/pythonrun.c index 9b6371d9c0da..c7a622c83d37 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -158,6 +158,8 @@ static int PARSER_FLAGS(PyCompilerFlags *flags) parser_flags |= PyPARSE_IGNORE_COOKIE; if (flags->cf_flags & CO_FUTURE_BARRY_AS_BDFL) parser_flags |= PyPARSE_BARRY_AS_BDFL; + if (flags->cf_flags & PyCF_TYPE_COMMENTS) + parser_flags |= PyPARSE_TYPE_COMMENTS; return parser_flags; } -- 2.47.3