Skip to content

bpo-40614: Respect feature version for f-string debug expressions #20196

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 7 commits into from
May 27, 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
5 changes: 5 additions & 0 deletions Lib/test/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,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 @@ -931,6 +931,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");
Comment on lines +935 to +936
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to ignore this. (Note that if you don't the message should be changed in the old parser as well)

Suggested change
RAISE_SYNTAX_ERROR("f-string: self documenting expressions are "
"only supported in Python 3.8 and greater");
RAISE_SYNTAX_ERROR("Self-documenting expressions in f-strings are "
"only supported in Python 3.8 and greater");

Copy link
Contributor Author

@hauntsaninja hauntsaninja May 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! All the other f-string SyntaxErrors start with "f-string: ", so I think it's good to stay consistent.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh okay, didn't remember correctly then.

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