Skip to content

Commit f7ed4d4

Browse files
hauntsaninjalysnikolaoupablogsal
authored
bpo-40614: Respect feature version for f-string debug expressions (GH-20196) (GH-20466)
Co-authored-by: Lysandros Nikolaou <[email protected]> Co-authored-by: Pablo Galindo <[email protected]> (cherry picked from commit c116c94)
1 parent c067183 commit f7ed4d4

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

Lib/test/test_ast.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,12 @@ def test_issue39579_dotted_name_end_col_offset(self):
630630
attr_b = tree.body[0].decorator_list[0].value
631631
self.assertEqual(attr_b.end_col_offset, 4)
632632

633+
def test_issue40614_feature_version(self):
634+
ast.parse('f"{x=}"', feature_version=(3, 8))
635+
with self.assertRaises(SyntaxError):
636+
ast.parse('f"{x=}"', feature_version=(3, 7))
637+
638+
633639
class ASTHelpers_Test(unittest.TestCase):
634640
maxDiff = None
635641

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:func:`ast.parse` will not parse self documenting expressions in f-strings when passed ``feature_version`` is less than ``(3, 8)``.

Python/ast.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5202,6 +5202,12 @@ fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
52025202
/* Check for =, which puts the text value of the expression in
52035203
expr_text. */
52045204
if (**str == '=') {
5205+
if (c->c_feature_version < 8) {
5206+
ast_error(c, n,
5207+
"f-string: self documenting expressions are "
5208+
"only supported in Python 3.8 and greater");
5209+
goto error;
5210+
}
52055211
*str += 1;
52065212

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

0 commit comments

Comments
 (0)