]> granicus.if.org Git - python/commitdiff
bpo-37520: Correct behavior for zipfile.Path.parent (GH-14638)
authorJason R. Coombs <jaraco@jaraco.com>
Sun, 7 Jul 2019 21:37:50 +0000 (17:37 -0400)
committerGitHub <noreply@github.com>
Sun, 7 Jul 2019 21:37:50 +0000 (17:37 -0400)
* bpo-37520: Correct behavior for zipfile.Path.parent

* ðŸ“œðŸ¤– Added by blurb_it.

Lib/test/test_zipfile.py
Lib/zipfile.py
Misc/NEWS.d/next/Library/2019-07-07-21-09-08.bpo-37520.Gg0KD6.rst [new file with mode: 0644]

index 19b550f80187ea450d0f12844f636ba2932707f8..0c8ffcdbf14afe251a7b069ac9d91fc5e809c53c 100644 (file)
@@ -2514,5 +2514,16 @@ class TestPath(unittest.TestCase):
             assert (root / 'a').parent.at == ''
             assert (root / 'a' / 'b').parent.at == 'a/'
 
+    def test_dir_parent(self):
+        for zipfile_abcde in self.zipfile_abcde():
+            root = zipfile.Path(zipfile_abcde)
+            assert (root / 'b').parent.at == ''
+            assert (root / 'b/').parent.at == ''
+
+    def test_missing_dir_parent(self):
+        for zipfile_abcde in self.zipfile_abcde():
+            root = zipfile.Path(zipfile_abcde)
+            assert (root / 'missing dir/').parent.at == ''
+
 if __name__ == "__main__":
     unittest.main()
index 62f2fd27d3ce6fd607061237e6d66ff5a298f7e1..3c1f1235034a9e2078da43e07bcc144816db3fdc 100644 (file)
@@ -2236,7 +2236,7 @@ class Path:
 
     @property
     def parent(self):
-        parent_at = posixpath.dirname(self.at)
+        parent_at = posixpath.dirname(self.at.rstrip('/'))
         if parent_at:
             parent_at += '/'
         return self._next(parent_at)
diff --git a/Misc/NEWS.d/next/Library/2019-07-07-21-09-08.bpo-37520.Gg0KD6.rst b/Misc/NEWS.d/next/Library/2019-07-07-21-09-08.bpo-37520.Gg0KD6.rst
new file mode 100644 (file)
index 0000000..6584d3e
--- /dev/null
@@ -0,0 +1 @@
+Correct behavior for zipfile.Path.parent when the path object identifies a subdirectory.
\ No newline at end of file