]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Define flags to turn on parsing type comments
authorGuido van Rossum <guido@python.org>
Sat, 19 Jan 2019 20:04:08 +0000 (12:04 -0800)
committerGuido van Rossum <guido@python.org>
Tue, 22 Jan 2019 17:18:28 +0000 (09:18 -0800)
Demo: `compile(sample, "<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
Include/parsetok.h
Parser/parsetok.c
Python/bltinmodule.c
Python/pythonrun.c

index 2dacfff37f8c2a502d03c7be9b72f1dc54a2afdd..36477c01e2ba6324c98cecc8a6c2159763b78e6f 100644 (file)
@@ -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 {
index 1217c46d0ed7b98dd2ecff05a26dc85afa830433..e95dd31fb79a29c73e78883914929230c95cc3a4 100644 (file)
@@ -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 *);
index 40db8a2b82905f341242856a84ae8f96d34468df..042fff71a02fb8447d95c976821e5063c5061701 100644 (file)
@@ -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;
index 332142fc6ffc2ac37810eb9ce37e6b0f85d9d555..930adb496e14cd7aaccffdba175992e8b75c4867 100644 (file)
@@ -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");
index 9b6371d9c0da64d50650090b9a73c533b608da17..c7a622c83d37c6042103501010db0b6ad912f4e1 100644 (file)
@@ -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;
 }