[Python-checkins] bpo-40904: Fix segfault in the new parser with f-string containing yield statements with no value (GH-20701)

Pablo Galindo webhook-mailer at python.org
Sun Jun 7 20:47:42 EDT 2020


https://github.com/python/cpython/commit/972ab0327675e695373fc6272d5ac24e187579ad
commit: 972ab0327675e695373fc6272d5ac24e187579ad
branch: master
author: Pablo Galindo <Pablogsal at gmail.com>
committer: GitHub <noreply at github.com>
date: 2020-06-08T01:47:37+01:00
summary:

bpo-40904: Fix segfault in the new parser with f-string containing yield statements with no value (GH-20701)

files:
A Misc/NEWS.d/next/Core and Builtins/2020-06-08-01-08-57.bpo-40904.76qQzo.rst
M Lib/test/test_fstring.py
M Parser/pegen/parse_string.c

diff --git a/Lib/test/test_fstring.py b/Lib/test/test_fstring.py
index ea4e589929e7e..9048e89689df2 100644
--- a/Lib/test/test_fstring.py
+++ b/Lib/test/test_fstring.py
@@ -725,9 +725,11 @@ def test_yield(self):
         #  a function into a generator
         def fn(y):
             f'y:{yield y*2}'
+            f'{yield}'
 
         g = fn(4)
         self.assertEqual(next(g), 8)
+        self.assertEqual(next(g), None)
 
     def test_yield_send(self):
         def fn(x):
diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-06-08-01-08-57.bpo-40904.76qQzo.rst b/Misc/NEWS.d/next/Core and Builtins/2020-06-08-01-08-57.bpo-40904.76qQzo.rst
new file mode 100644
index 0000000000000..09009b18c63a3
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2020-06-08-01-08-57.bpo-40904.76qQzo.rst	
@@ -0,0 +1,2 @@
+Fix possible segfault in the new PEG parser when parsing f-string containing
+yield statements with no value (:code:`f"{yield}"`). Patch by Pablo Galindo
diff --git a/Parser/pegen/parse_string.c b/Parser/pegen/parse_string.c
index efe82df47658b..94241e1965e9a 100644
--- a/Parser/pegen/parse_string.c
+++ b/Parser/pegen/parse_string.c
@@ -278,6 +278,9 @@ static void fstring_shift_argument(expr_ty parent, arg_ty args, int lineno, int
 
 
 static inline void shift_expr(expr_ty parent, expr_ty n, int line, int col) {
+    if (n == NULL) {
+        return;
+    }
     if (parent->lineno < n->lineno) {
         col = 0;
     }



More information about the Python-checkins mailing list