[Python-checkins] bpo-33348: parse expressions after * and ** in lib2to3 (GH-6586)

Miss Skeleton (bot) webhook-mailer at python.org
Thu Oct 24 02:19:14 EDT 2019


https://github.com/python/cpython/commit/96b06aefe23521b61e4e9cdd44f5d30b00c7eb95
commit: 96b06aefe23521b61e4e9cdd44f5d30b00c7eb95
branch: master
author: Zsolt Dollenstein <zsol.zsol at gmail.com>
committer: Miss Skeleton (bot) <31488909+miss-islington at users.noreply.github.com>
date: 2019-10-23T23:19:07-07:00
summary:

bpo-33348: parse expressions after * and ** in lib2to3 (GH-6586)



These are valid even in python 2.7


https://bugs.python.org/issue33348



Automerge-Triggered-By: @gpshead

files:
A Misc/NEWS.d/next/Library/2018-04-24-13-18-48.bpo-33348.XaJDei.rst
M Lib/lib2to3/Grammar.txt
M Lib/lib2to3/fixes/fix_apply.py
M Lib/lib2to3/fixes/fix_intern.py
M Lib/lib2to3/fixes/fix_reload.py
M Lib/lib2to3/tests/test_parser.py

diff --git a/Lib/lib2to3/Grammar.txt b/Lib/lib2to3/Grammar.txt
index a7ddad3cf322c..68b73868b5828 100644
--- a/Lib/lib2to3/Grammar.txt
+++ b/Lib/lib2to3/Grammar.txt
@@ -138,8 +138,8 @@ arglist: argument (',' argument)* [',']
 # that precede iterable unpackings are blocked; etc.
 argument: ( test [comp_for] |
             test '=' test |
-	    '**' expr |
-	    star_expr )
+            '**' test |
+	        '*' test )
 
 comp_iter: comp_for | comp_if
 comp_for: [ASYNC] 'for' exprlist 'in' testlist_safe [comp_iter]
diff --git a/Lib/lib2to3/fixes/fix_apply.py b/Lib/lib2to3/fixes/fix_apply.py
index 826ec8c9b62ab..6408582c42647 100644
--- a/Lib/lib2to3/fixes/fix_apply.py
+++ b/Lib/lib2to3/fixes/fix_apply.py
@@ -37,10 +37,8 @@ def transform(self, node, results):
         # I feel like we should be able to express this logic in the
         # PATTERN above but I don't know how to do it so...
         if args:
-            if args.type == self.syms.star_expr:
-                return  # Make no change.
             if (args.type == self.syms.argument and
-                args.children[0].value == '**'):
+                args.children[0].value in {'**', '*'}):
                 return  # Make no change.
         if kwds and (kwds.type == self.syms.argument and
                      kwds.children[0].value == '**'):
diff --git a/Lib/lib2to3/fixes/fix_intern.py b/Lib/lib2to3/fixes/fix_intern.py
index a852330908bd9..d752843092aac 100644
--- a/Lib/lib2to3/fixes/fix_intern.py
+++ b/Lib/lib2to3/fixes/fix_intern.py
@@ -30,10 +30,8 @@ def transform(self, node, results):
             # PATTERN above but I don't know how to do it so...
             obj = results['obj']
             if obj:
-                if obj.type == self.syms.star_expr:
-                    return  # Make no change.
                 if (obj.type == self.syms.argument and
-                    obj.children[0].value == '**'):
+                    obj.children[0].value in {'**', '*'}):
                     return  # Make no change.
         names = ('sys', 'intern')
         new = ImportAndCall(node, results, names)
diff --git a/Lib/lib2to3/fixes/fix_reload.py b/Lib/lib2to3/fixes/fix_reload.py
index 6c7fbbd3be3fb..b30841131c51f 100644
--- a/Lib/lib2to3/fixes/fix_reload.py
+++ b/Lib/lib2to3/fixes/fix_reload.py
@@ -27,10 +27,8 @@ def transform(self, node, results):
             # PATTERN above but I don't know how to do it so...
             obj = results['obj']
             if obj:
-                if obj.type == self.syms.star_expr:
-                    return  # Make no change.
                 if (obj.type == self.syms.argument and
-                    obj.children[0].value == '**'):
+                    obj.children[0].value in {'**', '*'}):
                     return  # Make no change.
         names = ('importlib', 'reload')
         new = ImportAndCall(node, results, names)
diff --git a/Lib/lib2to3/tests/test_parser.py b/Lib/lib2to3/tests/test_parser.py
index f22d399b3403f..a0c31e8f5300d 100644
--- a/Lib/lib2to3/tests/test_parser.py
+++ b/Lib/lib2to3/tests/test_parser.py
@@ -253,6 +253,13 @@ def test_double_star_dict_literal(self):
     def test_double_star_dict_literal_after_keywords(self):
         self.validate("""func(spam='fried', **{'eggs':'scrambled'})""")
 
+    def test_double_star_expression(self):
+        self.validate("""func(**{'a':2} or {})""")
+        self.validate("""func(**() or {})""")
+
+    def test_star_expression(self):
+        self.validate("""func(*[] or [2])""")
+
     def test_list_display(self):
         self.validate("""[*{2}, 3, *[4]]""")
 
diff --git a/Misc/NEWS.d/next/Library/2018-04-24-13-18-48.bpo-33348.XaJDei.rst b/Misc/NEWS.d/next/Library/2018-04-24-13-18-48.bpo-33348.XaJDei.rst
new file mode 100644
index 0000000000000..f95a73fb57ed4
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2018-04-24-13-18-48.bpo-33348.XaJDei.rst
@@ -0,0 +1,2 @@
+lib2to3 now recognizes expressions after ``*`` and `**` like in ``f(*[] or
+[])``.



More information about the Python-checkins mailing list