Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Lib/test/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,11 @@ def test_ast_asdl_signature(self):
expressions[0] = f"expr = {ast.expr.__subclasses__()[0].__doc__}"
self.assertCountEqual(ast.expr.__doc__.split("\n"), expressions)

def test_issue40614_feature_version(self):
ast.parse('f"{x=}"', feature_version=(3, 8))
with self.assertRaises(SyntaxError):
ast.parse('f"{x=}"', feature_version=(3, 7))


class ASTHelpers_Test(unittest.TestCase):
maxDiff = None
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:func:`ast.parse` will not parse self documenting expressions in f-strings when passed ``feature_version`` is less than ``(3, 8)``.
5 changes: 5 additions & 0 deletions Parser/pegen/parse_string.c
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,11 @@ fstring_find_expr(Parser *p, const char **str, const char *end, int raw, int rec
/* Check for =, which puts the text value of the expression in
expr_text. */
if (**str == '=') {
if (p->feature_version < 8) {
RAISE_SYNTAX_ERROR("f-string: self documenting expressions are "
"only supported in Python 3.8 and greater");
goto error;
}
*str += 1;

/* Skip over ASCII whitespace. No need to test for end of string
Expand Down
6 changes: 6 additions & 0 deletions Python/ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -5069,6 +5069,12 @@ fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
/* Check for =, which puts the text value of the expression in
expr_text. */
if (**str == '=') {
if (c->c_feature_version < 8) {
ast_error(c, n,
"f-string: self documenting expressions are "
"only supported in Python 3.8 and greater");
goto error;
}
*str += 1;

/* Skip over ASCII whitespace. No need to test for end of string
Expand Down