From 5a40e7a98e5a63ae00bff6fb8db423d4b2b4af60 Mon Sep 17 00:00:00 2001 From: hauntsaninja <> Date: Mon, 18 May 2020 15:31:56 -0700 Subject: [PATCH 1/7] bpo-40614: Respect feature version for f-string debug expressions --- Parser/pegen/parse_string.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Parser/pegen/parse_string.c b/Parser/pegen/parse_string.c index ca4b733c153b57..ed990b56a23c56 100644 --- a/Parser/pegen/parse_string.c +++ b/Parser/pegen/parse_string.c @@ -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"); + goto error; + } *str += 1; /* Skip over ASCII whitespace. No need to test for end of string From 6013c666aa61fb8dbed32daa7dd9b6e07eaf7f9d Mon Sep 17 00:00:00 2001 From: hauntsaninja <> Date: Mon, 18 May 2020 15:37:05 -0700 Subject: [PATCH 2/7] bpo-40614: add regression test --- Lib/test/test_ast.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py index e55d10badc37e1..59668dc027812e 100644 --- a/Lib/test/test_ast.py +++ b/Lib/test/test_ast.py @@ -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) as e: + ast.parse('f"{x=}"', feature_version=(3, 7)) + class ASTHelpers_Test(unittest.TestCase): maxDiff = None From f963e7d97e722d77b805df1b68e5ceb03dfeedcd Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Mon, 18 May 2020 22:41:03 +0000 Subject: [PATCH 3/7] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NEWS.d/next/Library/2020-05-18-22-41-02.bpo-40614.8j3kmq.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2020-05-18-22-41-02.bpo-40614.8j3kmq.rst 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 00000000000000..4d1ef5d1ed6780 --- /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` less than `(3, 8)`. \ No newline at end of file From a67153593b99c9132f3ca48c51ede65fecb7d65e Mon Sep 17 00:00:00 2001 From: hauntsaninja <> Date: Mon, 18 May 2020 15:42:42 -0700 Subject: [PATCH 4/7] News entry: fix RST markup --- .../next/Library/2020-05-18-22-41-02.bpo-40614.8j3kmq.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 4d1ef5d1ed6780..be378bebcec5e1 100644 --- 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 @@ -1 +1 @@ -:func:`ast.parse` will not parse self documenting expressions in f-strings when passed `feature_version` less than `(3, 8)`. \ No newline at end of file +:func:`ast.parse` will not parse self documenting expressions in f-strings when passed ``feature_version`` less than ``(3, 8)``. From 1b295cb0be6e590a7e4244c82e0ff501dcd35e0c Mon Sep 17 00:00:00 2001 From: hauntsaninja <> Date: Mon, 18 May 2020 16:46:21 -0700 Subject: [PATCH 5/7] bpo-40614: Fix old parser --- Python/ast.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Python/ast.c b/Python/ast.c index 2d20ca62aa8378..c524b8e34e8731 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 From 68111be16edffbc3f3ef728de74a1a477fbfc3a7 Mon Sep 17 00:00:00 2001 From: Shantanu Date: Mon, 18 May 2020 17:23:36 -0700 Subject: [PATCH 6/7] Update Lib/test/test_ast.py Co-authored-by: Lysandros Nikolaou --- Lib/test/test_ast.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py index 59668dc027812e..fa73d5b1c9e390 100644 --- a/Lib/test/test_ast.py +++ b/Lib/test/test_ast.py @@ -656,7 +656,7 @@ def test_ast_asdl_signature(self): def test_issue40614_feature_version(self): ast.parse('f"{x=}"', feature_version=(3, 8)) - with self.assertRaises(SyntaxError) as e: + with self.assertRaises(SyntaxError): ast.parse('f"{x=}"', feature_version=(3, 7)) From e6a76a1efcc64efaa936244be7142d058e7b87d0 Mon Sep 17 00:00:00 2001 From: Shantanu Date: Mon, 18 May 2020 17:24:03 -0700 Subject: [PATCH 7/7] Update Misc/NEWS.d/next/Library/2020-05-18-22-41-02.bpo-40614.8j3kmq.rst Co-authored-by: Lysandros Nikolaou --- .../next/Library/2020-05-18-22-41-02.bpo-40614.8j3kmq.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index be378bebcec5e1..238b98c14a3269 100644 --- 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 @@ -1 +1 @@ -:func:`ast.parse` will not parse self documenting expressions in f-strings when passed ``feature_version`` less than ``(3, 8)``. +:func:`ast.parse` will not parse self documenting expressions in f-strings when passed ``feature_version`` is less than ``(3, 8)``.