From: Guido van Rossum Date: Fri, 25 Jan 2019 20:48:31 +0000 (-0800) Subject: Export PyCF_TYPE_COMMENTS and add an interface for it to ast.parse() X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=46dfac5b3385684e23e24a8a70cca1a28ab7739c;p=thirdparty%2FPython%2Fcpython.git Export PyCF_TYPE_COMMENTS and add an interface for it to ast.parse() --- diff --git a/Lib/ast.py b/Lib/ast.py index 6c1e978b0586..470a74b3b5ff 100644 --- a/Lib/ast.py +++ b/Lib/ast.py @@ -27,12 +27,16 @@ from _ast import * -def parse(source, filename='', mode='exec'): +def parse(source, filename='', mode='exec', *, type_comments=False): """ Parse the source into an AST node. Equivalent to compile(source, filename, mode, PyCF_ONLY_AST). + Pass type_comments=True to get back type comments where the syntax allows. """ - return compile(source, filename, mode, PyCF_ONLY_AST) + flags = PyCF_ONLY_AST + if type_comments: + flags |= PyCF_TYPE_COMMENTS + return compile(source, filename, mode, flags) def literal_eval(node_or_string): diff --git a/Parser/asdl_c.py b/Parser/asdl_c.py index 6c538b751d2f..1526995e3f8b 100644 --- a/Parser/asdl_c.py +++ b/Parser/asdl_c.py @@ -1002,6 +1002,8 @@ class ASTModuleVisitor(PickleVisitor): self.emit('if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return NULL;', 1) self.emit('if (PyModule_AddIntMacro(m, PyCF_ONLY_AST) < 0)', 1) self.emit("return NULL;", 2) + self.emit('if (PyModule_AddIntMacro(m, PyCF_TYPE_COMMENTS) < 0)', 1) + self.emit("return NULL;", 2) for dfn in mod.dfns: self.visit(dfn) self.emit("return m;", 1) diff --git a/Python/Python-ast.c b/Python/Python-ast.c index 854ccdd12378..5467d192ee69 100644 --- a/Python/Python-ast.c +++ b/Python/Python-ast.c @@ -8710,6 +8710,8 @@ PyInit__ast(void) if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return NULL; if (PyModule_AddIntMacro(m, PyCF_ONLY_AST) < 0) return NULL; + if (PyModule_AddIntMacro(m, PyCF_TYPE_COMMENTS) < 0) + return NULL; if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return NULL; if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0) return NULL;