]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.8] bpo-39889: Fix unparse.py for subscript. (GH-18824). (GH-18826)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sat, 7 Mar 2020 17:13:31 +0000 (09:13 -0800)
committerGitHub <noreply@github.com>
Sat, 7 Mar 2020 17:13:31 +0000 (09:13 -0800)
(cherry picked from commit c4928fc1a853f3f84e2b4ec1253d0349137745e5)
(cherry picked from commit 92b72788ecf2ee5dfac780c7dfb5ee5350fc641d)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Lib/test/test_tools/test_unparse.py
Misc/NEWS.d/next/Tools-Demos/2020-03-07-18-01-30.bpo-39889.l1czT6.rst [new file with mode: 0644]
Tools/parser/unparse.py

index f3386f5e31a2df7c5209b60e41e8cb95aa62a9c8..12a41c756cddecf59e4740158b415dedcf9acc0e 100644 (file)
@@ -260,6 +260,20 @@ class UnparseTestCase(ASTTestCase):
         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 (file)
index 0000000..1202cb5
--- /dev/null
@@ -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]``).
index 7e1cc4ea5db95017a290f29cac578febce8d22ae..721ac3c6483d95d20c77c8a271f537034be6583b 100644 (file)
@@ -562,7 +562,17 @@ class Unparser:
     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):
@@ -587,7 +597,12 @@ class Unparser:
             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):