diff options
author | Lysandros Nikolaou <lisandrosnik@gmail.com> | 2020-05-01 03:27:52 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-01 03:27:52 (GMT) |
commit | 3e0a6f37dfdd595be737baae00ec0e036a912615 (patch) | |
tree | 42c810af16c6e84aa38aa31c30aebd1d0a9671f3 /Parser/pegen/parse_string.c | |
parent | eb0d359b4b0e14552998e7af771a088b4fd01745 (diff) | |
download | cpython-3e0a6f37dfdd595be737baae00ec0e036a912615.zip cpython-3e0a6f37dfdd595be737baae00ec0e036a912615.tar.gz cpython-3e0a6f37dfdd595be737baae00ec0e036a912615.tar.bz2 |
bpo-40334: Add support for feature_version in new PEG parser (GH-19827)
`ast.parse` and `compile` support a `feature_version` parameter that
tells the parser to parse the input string, as if it were written in
an older Python version.
The `feature_version` is propagated to the tokenizer, which uses it
to handle the three different stages of support for `async` and
`await`. Additionally, it disallows the following at parser level:
- The '@' operator in < 3.5
- Async functions in < 3.5
- Async comprehensions in < 3.6
- Underscores in numeric literals in < 3.6
- Await expression in < 3.5
- Variable annotations in < 3.6
- Async for-loops in < 3.5
- Async with-statements in < 3.5
- F-strings in < 3.6
Closes we-like-parsers/cpython#124.
Diffstat (limited to 'Parser/pegen/parse_string.c')
-rw-r--r-- | Parser/pegen/parse_string.c | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/Parser/pegen/parse_string.c b/Parser/pegen/parse_string.c index 834239e..d96303d 100644 --- a/Parser/pegen/parse_string.c +++ b/Parser/pegen/parse_string.c @@ -179,6 +179,13 @@ _PyPegen_parsestr(Parser *p, const char *s, int *bytesmode, int *rawmode, PyObje } } + /* fstrings are only allowed in Python 3.6 and greater */ + if (fmode && p->feature_version < 6) { + p->error_indicator = 1; + RAISE_SYNTAX_ERROR("Format strings are only supported in Python 3.6 and greater"); + return -1; + } + if (fmode && *bytesmode) { PyErr_BadInternalCall(); return -1; @@ -595,7 +602,8 @@ fstring_compile_expr(Parser *p, const char *expr_start, const char *expr_end, return NULL; } - Parser *p2 = _PyPegen_Parser_New(tok, Py_fstring_input, p->flags, NULL, p->arena); + Parser *p2 = _PyPegen_Parser_New(tok, Py_fstring_input, p->flags, p->feature_version, + NULL, p->arena); p2->starting_lineno = p->starting_lineno + p->tok->first_lineno - 1; p2->starting_col_offset = p->tok->first_lineno == p->tok->lineno ? p->starting_col_offset + t->col_offset : 0; |