[Python-checkins] [3.9] bpo-43272: Fix old parser test failures for backported grammar constructs (GH-24591)

pablogsal webhook-mailer at python.org
Fri Feb 19 20:36:22 EST 2021


https://github.com/python/cpython/commit/f9d1bf2de07131a8d80bc1e4914ee534bc5effa4
commit: f9d1bf2de07131a8d80bc1e4914ee534bc5effa4
branch: 3.9
author: Pablo Galindo <Pablogsal at gmail.com>
committer: pablogsal <Pablogsal at gmail.com>
date: 2021-02-20T01:36:15Z
summary:

[3.9] bpo-43272: Fix old parser test failures for backported grammar constructs (GH-24591)

files:
M Lib/test/test_fstring.py
M Lib/test/test_named_expressions.py
M Lib/test/test_unpack_ex.py

diff --git a/Lib/test/test_fstring.py b/Lib/test/test_fstring.py
index 2f08d35f26dc3..05e7102d624f3 100644
--- a/Lib/test/test_fstring.py
+++ b/Lib/test/test_fstring.py
@@ -374,10 +374,10 @@ def test_ast_line_numbers_with_parentheses(self):
         # check the call
         call = middle.value
         self.assertEqual(type(call), ast.Call)
-        self.assertEqual(call.lineno, 5)
-        self.assertEqual(call.end_lineno, 5)
-        self.assertEqual(call.col_offset, 27)
-        self.assertEqual(call.end_col_offset, 31)
+        self.assertEqual(call.lineno, 4 if use_old_parser() else 5)
+        self.assertEqual(call.end_lineno, 4 if use_old_parser() else 5)
+        self.assertEqual(call.col_offset, 13 if use_old_parser() else 27)
+        self.assertEqual(call.end_col_offset, 17 if use_old_parser() else 31)
         # check the second wat
         self.assertEqual(type(wat2), ast.Constant)
         self.assertEqual(wat2.lineno, 4)
diff --git a/Lib/test/test_named_expressions.py b/Lib/test/test_named_expressions.py
index 2adcd4b5d6466..55c003888d478 100644
--- a/Lib/test/test_named_expressions.py
+++ b/Lib/test/test_named_expressions.py
@@ -1,4 +1,5 @@
 import unittest
+from test.support import use_old_parser
 
 GLOBAL_VAR = None
 
@@ -167,6 +168,7 @@ def test_named_expression_invalid_list_comprehension_iterable_expression(self):
                 with self.assertRaisesRegex(SyntaxError, msg):
                     exec(f"lambda: {code}", {}) # Function scope
 
+    @unittest.skipIf(use_old_parser(), "Old parser does not support walruses in set comprehensions")
     def test_named_expression_invalid_rebinding_set_comprehension_iteration_variable(self):
         cases = [
             ("Local reuse", 'i', "{i := 0 for i in range(5)}"),
@@ -199,6 +201,7 @@ def test_named_expression_invalid_rebinding_set_comprehension_inner_loop(self):
                 with self.assertRaisesRegex(SyntaxError, msg):
                     exec(f"lambda: {code}", {}) # Function scope
 
+    @unittest.skipIf(use_old_parser(), "Old parser does not support walruses in set comprehensions")
     def test_named_expression_invalid_set_comprehension_iterable_expression(self):
         cases = [
             ("Top level", "{i for i in (i := range(5))}"),
diff --git a/Lib/test/test_unpack_ex.py b/Lib/test/test_unpack_ex.py
index 049e48b13fa1f..2dff57c268f07 100644
--- a/Lib/test/test_unpack_ex.py
+++ b/Lib/test/test_unpack_ex.py
@@ -1,5 +1,7 @@
 # Tests for extended unpacking, starred expressions.
 
+from test.support import use_old_parser
+
 doctests = """
 
 Unpack tuple
@@ -346,6 +348,26 @@
       ...
     SyntaxError: can't use starred expression here
 
+Some size constraints (all fail.)
+
+    >>> s = ", ".join("a%d" % i for i in range(1<<8)) + ", *rest = range(1<<8 + 1)"
+    >>> compile(s, 'test', 'exec') # doctest:+ELLIPSIS
+    Traceback (most recent call last):
+     ...
+    SyntaxError: too many expressions in star-unpacking assignment
+
+    >>> s = ", ".join("a%d" % i for i in range(1<<8 + 1)) + ", *rest = range(1<<8 + 2)"
+    >>> compile(s, 'test', 'exec') # doctest:+ELLIPSIS
+    Traceback (most recent call last):
+     ...
+    SyntaxError: too many expressions in star-unpacking assignment
+
+(there is an additional limit, on the number of expressions after the
+'*rest', but it's 1<<24 and testing it takes too much memory.)
+
+"""
+
+new_parser_doctests = """\
     >>> (*x),y = 1, 2 # doctest:+ELLIPSIS
     Traceback (most recent call last):
       ...
@@ -370,27 +392,12 @@
     Traceback (most recent call last):
       ...
     SyntaxError: can't use starred expression here
-
-Some size constraints (all fail.)
-
-    >>> s = ", ".join("a%d" % i for i in range(1<<8)) + ", *rest = range(1<<8 + 1)"
-    >>> compile(s, 'test', 'exec') # doctest:+ELLIPSIS
-    Traceback (most recent call last):
-     ...
-    SyntaxError: too many expressions in star-unpacking assignment
-
-    >>> s = ", ".join("a%d" % i for i in range(1<<8 + 1)) + ", *rest = range(1<<8 + 2)"
-    >>> compile(s, 'test', 'exec') # doctest:+ELLIPSIS
-    Traceback (most recent call last):
-     ...
-    SyntaxError: too many expressions in star-unpacking assignment
-
-(there is an additional limit, on the number of expressions after the
-'*rest', but it's 1<<24 and testing it takes too much memory.)
-
 """
 
-__test__ = {'doctests' : doctests}
+if use_old_parser():
+    __test__ = {'doctests' : doctests}
+else:
+    __test__ = {'doctests' : doctests + new_parser_doctests}
 
 def test_main(verbose=False):
     from test import support



More information about the Python-checkins mailing list