diff options
author | Pablo Galindo <Pablogsal@gmail.com> | 2020-05-27 21:01:11 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-27 21:01:11 (GMT) |
commit | 9b83829e7d476acecc86d48978acc5813ec51e65 (patch) | |
tree | 54ed63c5cb7e0d81cd1ea91bf51f935a66658bbd | |
parent | 3a2667d91e33493ccde113ddf5092afefc3c89fa (diff) | |
download | cpython-9b83829e7d476acecc86d48978acc5813ec51e65.zip cpython-9b83829e7d476acecc86d48978acc5813ec51e65.tar.gz cpython-9b83829e7d476acecc86d48978acc5813ec51e65.tar.bz2 |
[3.9] bpo-40614: Respect feature version for f-string debug expressions (GH-20196) (GH-20464)
Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com>
Co-authored-by: Pablo Galindo <pablogsal@gmail.com>
(cherry picked from commit c116c94)
Co-authored-by: Shantanu <hauntsaninja@users.noreply.github.com>
-rw-r--r-- | Lib/test/test_ast.py | 5 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2020-05-18-22-41-02.bpo-40614.8j3kmq.rst | 1 | ||||
-rw-r--r-- | Parser/pegen/parse_string.c | 5 | ||||
-rw-r--r-- | Python/ast.c | 6 |
4 files changed, 17 insertions, 0 deletions
diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py index 3e9c8b5..a3b366e 100644 --- a/Lib/test/test_ast.py +++ b/Lib/test/test_ast.py @@ -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 diff --git a/Misc/NEWS.d/next/Library/2020-05-18-22-41-02.bpo-40614.8j3kmq.rst b/Misc/NEWS.d/next/Library/2020-05-18-22-41-02.bpo-40614.8j3kmq.rst new file mode 100644 index 0000000..238b98c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-05-18-22-41-02.bpo-40614.8j3kmq.rst @@ -0,0 +1 @@ +:func:`ast.parse` will not parse self documenting expressions in f-strings when passed ``feature_version`` is less than ``(3, 8)``. diff --git a/Parser/pegen/parse_string.c b/Parser/pegen/parse_string.c index a0ec698..e24ecc5 100644 --- a/Parser/pegen/parse_string.c +++ b/Parser/pegen/parse_string.c @@ -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 diff --git a/Python/ast.c b/Python/ast.c index 2d20ca6..c524b8e 100644 --- a/Python/ast.c +++ b/Python/ast.c @@ -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 |