cpython: d02507c9f973 (original) (raw)
Mercurial > cpython
changeset 83282:d02507c9f973 2.7
Issue #17656: Fix extraction of zip files with unicode member paths. [#17656]
Serhiy Storchaka storchaka@gmail.com | |
---|---|
date | Sat, 13 Apr 2013 12:28:17 +0300 |
parents | 1062c66e9bdc |
children | d5e5017309b1 |
files | Lib/test/test_zipfile.py Lib/zipfile.py Misc/NEWS |
diffstat | 3 files changed, 26 insertions(+), 2 deletions(-)[+] [-] Lib/test/test_zipfile.py 21 Lib/zipfile.py 5 Misc/NEWS 2 |
line wrap: on
line diff
--- a/Lib/test/test_zipfile.py +++ b/Lib/test/test_zipfile.py @@ -18,7 +18,7 @@ from tempfile import TemporaryFile from random import randint, random from unittest import skipUnless -from test.test_support import TESTFN, run_unittest, findfile, unlink +from test.test_support import TESTFN, TESTFN_UNICODE, run_unittest, findfile, unlink TESTFN2 = TESTFN + "2" TESTFNDIR = TESTFN + "d" @@ -424,6 +424,25 @@ class TestsWithSourceFile(unittest.TestC with open(filename, 'rb') as f: self.assertEqual(f.read(), content)
- def test_extract_unicode_filenames(self):
fnames = [u'foo.txt', os.path.basename(TESTFN_UNICODE)][](#l1.17)
content = 'Test for unicode filename'[](#l1.18)
with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:[](#l1.19)
for fname in fnames:[](#l1.20)
zipfp.writestr(fname, content)[](#l1.21)
with zipfile.ZipFile(TESTFN2, "r") as zipfp:[](#l1.23)
for fname in fnames:[](#l1.24)
writtenfile = zipfp.extract(fname)[](#l1.25)
# make sure it was written to the right place[](#l1.27)
correctfile = os.path.join(os.getcwd(), fname)[](#l1.28)
correctfile = os.path.normpath(correctfile)[](#l1.29)
self.assertEqual(writtenfile, correctfile)[](#l1.30)
self.check_file(writtenfile, content)[](#l1.32)
os.remove(writtenfile)[](#l1.33)
+ def test_extract_hackers_arcnames(self): hacknames = [ ('../foo/bar', 'foo/bar'),
--- a/Lib/zipfile.py +++ b/Lib/zipfile.py @@ -1053,7 +1053,10 @@ class ZipFile(object): if os.path.sep == '\': # filter illegal characters on Windows illegal = ':<>|"?*'
table = string.maketrans(illegal, '_' * len(illegal))[](#l2.7)
if isinstance(arcname, unicode):[](#l2.8)
table = {ord(c): ord('_') for c in illegal}[](#l2.9)
else:[](#l2.10)
table = string.maketrans(illegal, '_' * len(illegal))[](#l2.11) arcname = arcname.translate(table)[](#l2.12) # remove trailing dots[](#l2.13) arcname = (x.rstrip('.') for x in arcname.split(os.path.sep))[](#l2.14)
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -22,6 +22,8 @@ Core and Builtins Library ------- +- Issue #17656: Fix extraction of zip files with unicode member paths. +