[Python-checkins] [3.8] bpo-39889: Fix unparse.py for subscript. (GH-18824). (GH-18826)

Serhiy Storchaka webhook-mailer at python.org
Sat Mar 7 11:55:36 EST 2020


https://github.com/python/cpython/commit/92b72788ecf2ee5dfac780c7dfb5ee5350fc641d
commit: 92b72788ecf2ee5dfac780c7dfb5ee5350fc641d
branch: 3.8
author: Serhiy Storchaka <storchaka at gmail.com>
committer: GitHub <noreply at github.com>
date: 2020-03-07T18:55:32+02:00
summary:

[3.8] bpo-39889: Fix unparse.py for subscript. (GH-18824). (GH-18826)

(cherry picked from commit c4928fc1a853f3f84e2b4ec1253d0349137745e5)

files:
A Misc/NEWS.d/next/Tools-Demos/2020-03-07-18-01-30.bpo-39889.l1czT6.rst
M Lib/test/test_tools/test_unparse.py
M Tools/parser/unparse.py

diff --git a/Lib/test/test_tools/test_unparse.py b/Lib/test/test_tools/test_unparse.py
index a958ebb51cc3d..d89cb09776756 100644
--- a/Lib/test/test_tools/test_unparse.py
+++ b/Lib/test/test_tools/test_unparse.py
@@ -265,6 +265,20 @@ def test_dict_unpacking_in_dict(self):
         self.check_roundtrip(r"""{**{'y': 2}, 'x': 1}""")
         self.check_roundtrip(r"""{**{'y': 2}, **{'x': 1}}""")
 
+    def test_subscript(self):
+        self.check_roundtrip("a[i]")
+        self.check_roundtrip("a[i,]")
+        self.check_roundtrip("a[i, j]")
+        self.check_roundtrip("a[()]")
+        self.check_roundtrip("a[i:j]")
+        self.check_roundtrip("a[:j]")
+        self.check_roundtrip("a[i:]")
+        self.check_roundtrip("a[i:j:k]")
+        self.check_roundtrip("a[:j:k]")
+        self.check_roundtrip("a[i::k]")
+        self.check_roundtrip("a[i:j,]")
+        self.check_roundtrip("a[i:j, k]")
+
 
 class DirectoryTestCase(ASTTestCase):
     """Test roundtrip behaviour on all files in Lib and Lib/test."""
diff --git a/Misc/NEWS.d/next/Tools-Demos/2020-03-07-18-01-30.bpo-39889.l1czT6.rst b/Misc/NEWS.d/next/Tools-Demos/2020-03-07-18-01-30.bpo-39889.l1czT6.rst
new file mode 100644
index 0000000000000..1202cb5fa064b
--- /dev/null
+++ b/Misc/NEWS.d/next/Tools-Demos/2020-03-07-18-01-30.bpo-39889.l1czT6.rst
@@ -0,0 +1,3 @@
+Fixed ``unparse.py`` for extended slices containing a single element (e.g.
+``a[i:j,]``). Remove redundant tuples when index with a tuple (e.g. ``a[i,
+j]``).
diff --git a/Tools/parser/unparse.py b/Tools/parser/unparse.py
index a5cc000676b02..0642b84dcfc82 100644
--- a/Tools/parser/unparse.py
+++ b/Tools/parser/unparse.py
@@ -556,7 +556,17 @@ def _Call(self, t):
     def _Subscript(self, t):
         self.dispatch(t.value)
         self.write("[")
-        self.dispatch(t.slice)
+        if (isinstance(t.slice, ast.Index)
+                and isinstance(t.slice.value, ast.Tuple)
+                and t.slice.value.elts):
+            if len(t.slice.value.elts) == 1:
+                elt = t.slice.value.elts[0]
+                self.dispatch(elt)
+                self.write(",")
+            else:
+                interleave(lambda: self.write(", "), self.dispatch, t.slice.value.elts)
+        else:
+            self.dispatch(t.slice)
         self.write("]")
 
     def _Starred(self, t):
@@ -581,7 +591,12 @@ def _Slice(self, t):
             self.dispatch(t.step)
 
     def _ExtSlice(self, t):
-        interleave(lambda: self.write(', '), self.dispatch, t.dims)
+        if len(t.dims) == 1:
+            elt = t.dims[0]
+            self.dispatch(elt)
+            self.write(",")
+        else:
+            interleave(lambda: self.write(', '), self.dispatch, t.dims)
 
     # argument
     def _arg(self, t):



More information about the Python-checkins mailing list