Skip to content

Commit c116c94

Browse files
hauntsaninjalysnikolaoupablogsal
authored
bpo-40614: Respect feature version for f-string debug expressions (pythonGH-20196)
Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com> Co-authored-by: Pablo Galindo <pablogsal@gmail.com>
1 parent db5aed9 commit c116c94

File tree

4 files changed

+17
-0
lines changed

4 files changed

+17
-0
lines changed

Lib/test/test_ast.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,11 @@ def test_ast_asdl_signature(self):
663663
expressions[0] = f"expr = {ast.expr.__subclasses__()[0].__doc__}"
664664
self.assertCountEqual(ast.expr.__doc__.split("\n"), expressions)
665665

666+
def test_issue40614_feature_version(self):
667+
ast.parse('f"{x=}"', feature_version=(3, 8))
668+
with self.assertRaises(SyntaxError):
669+
ast.parse('f"{x=}"', feature_version=(3, 7))
670+
666671

667672
class ASTHelpers_Test(unittest.TestCase):
668673
maxDiff = None
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)``.

Parser/pegen/parse_string.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,11 @@ fstring_find_expr(Parser *p, const char **str, const char *end, int raw, int rec
928928
/* Check for =, which puts the text value of the expression in
929929
expr_text. */
930930
if (**str == '=') {
931+
if (p->feature_version < 8) {
932+
RAISE_SYNTAX_ERROR("f-string: self documenting expressions are "
933+
"only supported in Python 3.8 and greater");
934+
goto error;
935+
}
931936
*str += 1;
932937

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

Python/ast.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5069,6 +5069,12 @@ fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
50695069
/* Check for =, which puts the text value of the expression in
50705070
expr_text. */
50715071
if (**str == '=') {
5072+
if (c->c_feature_version < 8) {
5073+
ast_error(c, n,
5074+
"f-string: self documenting expressions are "
5075+
"only supported in Python 3.8 and greater");
5076+
goto error;
5077+
}
50725078
*str += 1;
50735079

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

0 commit comments

Comments
 (0)