bpo-40614: Respect feature version for f-string debug expressions (GH-20196)

Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com>
Co-authored-by: Pablo Galindo <pablogsal@gmail.com>
This commit is contained in:
Shantanu 2020-05-27 13:30:38 -07:00 committed by GitHub
parent db5aed931f
commit c116c94ff1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 0 deletions

View File

@ -663,6 +663,11 @@ class AST_Tests(unittest.TestCase):
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

View File

@ -0,0 +1 @@
:func:`ast.parse` will not parse self documenting expressions in f-strings when passed ``feature_version`` is less than ``(3, 8)``.

View File

@ -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

View File

@ -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