Skip to content

[3.8] bpo-40614: Respect feature version for f-string debug expressions (GH-20196) #20466

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 6, 2020
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
6 changes: 6 additions & 0 deletions Lib/test/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,12 @@ def test_issue39579_dotted_name_end_col_offset(self):
attr_b = tree.body[0].decorator_list[0].value
self.assertEqual(attr_b.end_col_offset, 4)

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)``.
6 changes: 6 additions & 0 deletions Python/ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -5202,6 +5202,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