diff -r cf70f030a744 Lib/test/test_zipfile.py --- a/Lib/test/test_zipfile.py Wed Jun 18 23:07:46 2014 -0400 +++ b/Lib/test/test_zipfile.py Mon Jun 23 21:32:29 2014 +0300 @@ -17,13 +17,19 @@ from random import randint, random from unittest import skipUnless -from test.test_support import TESTFN, TESTFN_UNICODE, TESTFN_ENCODING, \ - run_unittest, findfile, unlink, rmtree, check_warnings -try: - TESTFN_UNICODE.encode(TESTFN_ENCODING) -except (UnicodeError, TypeError): - # Either the file system encoding is None, or the file name - # cannot be encoded in the file system encoding. +from test.test_support import TESTFN, run_unittest, findfile, unlink, rmtree, \ + check_warnings, have_unicode, requires_unicode + +TESTFN_UNICODE = None +if have_unicode: + from test.test_support import TESTFN_UNICODE, TESTFN_ENCODING + try: + TESTFN_UNICODE.encode(TESTFN_ENCODING) + except (UnicodeError, TypeError): + # Either the file system encoding is None, or the file name + # cannot be encoded in the file system encoding. + TESTFN_UNICODE = None +else: TESTFN_UNICODE = None TESTFN2 = TESTFN + "2" @@ -496,7 +502,7 @@ ]) for arcname, fixedname in hacknames: - content = b'foobar' + arcname.encode() + content = b'foobar' + str(arcname) with zipfile.ZipFile(TESTFN2, 'w', zipfile.ZIP_STORED) as zipfp: zinfo = zipfile.ZipInfo() # preserve backslashes @@ -809,6 +815,7 @@ b'\x01\x003\x00\x00\x003\x00\x00\x00\x00\x00'), } + @requires_unicode def test_unicode_filenames(self): with zipfile.ZipFile(TESTFN, "w") as zf: zf.writestr(u"foo.txt", "Test for unicode filename") diff -r cf70f030a744 Lib/zipfile.py --- a/Lib/zipfile.py Wed Jun 18 23:07:46 2014 -0400 +++ b/Lib/zipfile.py Mon Jun 23 21:32:29 2014 +0300 @@ -14,6 +14,14 @@ zlib = None crc32 = binascii.crc32 +try: + _unicode = unicode +except NameError: + # If Python is built without Unicode support, the unicode type + # will not exist. Fake one. + class _unicode(object): + pass + __all__ = ["BadZipfile", "error", "ZIP_STORED", "ZIP_DEFLATED", "is_zipfile", "ZipInfo", "ZipFile", "PyZipFile", "LargeZipFile" ] @@ -366,7 +374,7 @@ return header + filename + extra def _encodeFilenameFlags(self): - if isinstance(self.filename, unicode): + if isinstance(self.filename, _unicode): try: return self.filename.encode('ascii'), self.flag_bits except UnicodeEncodeError: @@ -1057,7 +1065,7 @@ if os.path.sep == '\\': # filter illegal characters on Windows illegal = ':<>|"?*' - if isinstance(arcname, unicode): + if isinstance(arcname, _unicode): table = {ord(c): ord('_') for c in illegal} else: table = string.maketrans(illegal, '_' * len(illegal))