]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Export PyCF_TYPE_COMMENTS and add an interface for it to ast.parse()
authorGuido van Rossum <guido@python.org>
Fri, 25 Jan 2019 20:48:31 +0000 (12:48 -0800)
committerGuido van Rossum <guido@python.org>
Fri, 25 Jan 2019 20:48:31 +0000 (12:48 -0800)
Lib/ast.py
Parser/asdl_c.py
Python/Python-ast.c

index 6c1e978b0586677868f913a96157db3188968d17..470a74b3b5ff794c373dd6c2a09277325b358b20 100644 (file)
 from _ast import *
 
 
-def parse(source, filename='<unknown>', mode='exec'):
+def parse(source, filename='<unknown>', 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):
index 6c538b751d2f0086a7ef2e3a075ba97141306d78..1526995e3f8b559620acfa46b3c7cd519139face 100644 (file)
@@ -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)
index 854ccdd123786262bba87af31578e1ad10278697..5467d192ee69c07443bd39150df550dcca6842f1 100644 (file)
@@ -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;