Skip to content

Commit 72e4f4d

Browse files
committed
Define flags to turn on parsing type comments
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.
1 parent 8b426b9 commit 72e4f4d

5 files changed

Lines changed: 11 additions & 1 deletion

File tree

Include/compile.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ PyAPI_FUNC(PyCodeObject *) PyNode_Compile(struct _node *, const char *);
2222
#define PyCF_DONT_IMPLY_DEDENT 0x0200
2323
#define PyCF_ONLY_AST 0x0400
2424
#define PyCF_IGNORE_COOKIE 0x0800
25+
#define PyCF_TYPE_COMMENTS 0x1000
2526

2627
#ifndef Py_LIMITED_API
2728
typedef struct {

Include/parsetok.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ typedef struct {
3737

3838
#define PyPARSE_IGNORE_COOKIE 0x0010
3939
#define PyPARSE_BARRY_AS_BDFL 0x0020
40+
#define PyPARSE_TYPE_COMMENTS 0x0040
4041

4142
PyAPI_FUNC(node *) PyParser_ParseString(const char *, grammar *, int,
4243
perrdetail *);

Parser/parsetok.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ PyParser_ParseStringObject(const char *s, PyObject *filename,
9191
err_ret->error = PyErr_Occurred() ? E_DECODE : E_NOMEM;
9292
return NULL;
9393
}
94+
if (*flags & PyPARSE_TYPE_COMMENTS) {
95+
tok->type_comments = 1;
96+
}
9497

9598
#ifndef PGEN
9699
Py_INCREF(err_ret->filename);
@@ -159,6 +162,9 @@ PyParser_ParseFileObject(FILE *fp, PyObject *filename,
159162
err_ret->error = E_NOMEM;
160163
return NULL;
161164
}
165+
if (*flags & PyPARSE_TYPE_COMMENTS) {
166+
tok->type_comments = 1;
167+
}
162168
#ifndef PGEN
163169
Py_INCREF(err_ret->filename);
164170
tok->filename = err_ret->filename;

Python/bltinmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,7 @@ builtin_compile_impl(PyObject *module, PyObject *source, PyObject *filename,
771771
cf.cf_flags = flags | PyCF_SOURCE_IS_UTF8;
772772

773773
if (flags &
774-
~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_DONT_IMPLY_DEDENT | PyCF_ONLY_AST))
774+
~(PyCF_MASK | PyCF_MASK_OBSOLETE | PyCF_DONT_IMPLY_DEDENT | PyCF_ONLY_AST | PyCF_TYPE_COMMENTS))
775775
{
776776
PyErr_SetString(PyExc_ValueError,
777777
"compile(): unrecognised flags");

Python/pythonrun.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ static int PARSER_FLAGS(PyCompilerFlags *flags)
158158
parser_flags |= PyPARSE_IGNORE_COOKIE;
159159
if (flags->cf_flags & CO_FUTURE_BARRY_AS_BDFL)
160160
parser_flags |= PyPARSE_BARRY_AS_BDFL;
161+
if (flags->cf_flags & PyCF_TYPE_COMMENTS)
162+
parser_flags |= PyPARSE_TYPE_COMMENTS;
161163
return parser_flags;
162164
}
163165

0 commit comments

Comments
 (0)