]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Fix the test for issue #6972.
authorSerhiy Storchaka <storchaka@gmail.com>
Sat, 2 Feb 2013 17:50:59 +0000 (19:50 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Sat, 2 Feb 2013 17:50:59 +0000 (19:50 +0200)
Remove trailing dots on Windows.

Lib/test/test_zipfile.py
Lib/zipfile.py

index 85cd0742b7f2dc05e497f8c7818becfc0996b0af..c00c83ef052452ef357dda382173bbc49a9bbeec 100644 (file)
@@ -456,8 +456,6 @@ class TestsWithSourceFile(unittest.TestCase):
             ('/foo/bar', 'foo/bar'),
             ('/foo/../bar', 'foo/bar'),
             ('/foo/../../bar', 'foo/bar'),
-            ('//foo/bar', 'foo/bar'),
-            ('../../foo../../ba..r', 'foo../ba..r'),
         ]
         if os.path.sep == '\\':  # Windows.
             hacknames.extend([
@@ -479,19 +477,32 @@ class TestsWithSourceFile(unittest.TestCase):
                 (r'\\?\C:\foo\bar', 'foo/bar'),
                 (r'C:/../C:/foo/bar', 'C_/foo/bar'),
                 (r'a:b\c<d>e|f"g?h*i', 'b/c_d_e_f_g_h_i'),
+                ('../../foo../../ba..r', 'foo/ba..r'),
+            ])
+        else:  # Unix
+            hacknames.extend([
+                ('//foo/bar', 'foo/bar'),
+                ('../../foo../../ba..r', 'foo../ba..r'),
+                (r'foo/..\bar', r'foo/..\bar'),
             ])
 
         for arcname, fixedname in hacknames:
             content = b'foobar' + arcname.encode()
             with zipfile.ZipFile(TESTFN2, 'w', zipfile.ZIP_STORED) as zipfp:
-                zipfp.writestr(arcname, content)
+                zinfo = zipfile.ZipInfo()
+                # preserve backslashes
+                zinfo.filename = arcname
+                zinfo.external_attr = 0o600 << 16
+                zipfp.writestr(zinfo, content)
 
+            arcname = arcname.replace(os.sep, "/")
             targetpath = os.path.join('target', 'subdir', 'subsub')
             correctfile = os.path.join(targetpath, *fixedname.split('/'))
 
             with zipfile.ZipFile(TESTFN2, 'r') as zipfp:
                 writtenfile = zipfp.extract(arcname, targetpath)
-                self.assertEqual(writtenfile, correctfile)
+                self.assertEqual(writtenfile, correctfile,
+                                 msg="extract %r" % arcname)
             self.check_file(correctfile, content)
             shutil.rmtree('target')
 
@@ -504,7 +515,8 @@ class TestsWithSourceFile(unittest.TestCase):
 
             with zipfile.ZipFile(TESTFN2, 'r') as zipfp:
                 writtenfile = zipfp.extract(arcname)
-                self.assertEqual(writtenfile, correctfile)
+                self.assertEqual(writtenfile, correctfile,
+                                 msg="extract %r" % arcname)
             self.check_file(correctfile, content)
             shutil.rmtree(fixedname.split('/')[0])
 
index 0b5d3e8f7b3b8ed8c80ebebe6c6b312963298ba2..b223b4a4c190cbe2e5d7a139a69123685b557829 100644 (file)
@@ -1071,11 +1071,14 @@ class ZipFile:
         arcname = os.path.splitdrive(arcname)[1]
         arcname = os.path.sep.join(x for x in arcname.split(os.path.sep)
                     if x not in ('', os.path.curdir, os.path.pardir))
-        # filter illegal characters on Windows
         if os.path.sep == '\\':
+            # filter illegal characters on Windows
             illegal = ':<>|"?*'
             table = str.maketrans(illegal, '_' * len(illegal))
             arcname = arcname.translate(table)
+            # remove trailing dots
+            arcname = (x.rstrip('.') for x in arcname.split(os.path.sep))
+            arcname = os.path.sep.join(x for x in arcname if x)
 
         targetpath = os.path.join(targetpath, arcname)
         targetpath = os.path.normpath(targetpath)