aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/test/test_zipimport.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test/test_zipimport.py')
-rw-r--r--Lib/test/test_zipimport.py100
1 files changed, 55 insertions, 45 deletions
diff --git a/Lib/test/test_zipimport.py b/Lib/test/test_zipimport.py
index a66738a778c..df5ff9d6a69 100644
--- a/Lib/test/test_zipimport.py
+++ b/Lib/test/test_zipimport.py
@@ -6,7 +6,7 @@ import struct
import time
import unittest
-from test import test_support
+from test import support
from test.test_importhooks import ImportHooksBaseTestCase, test_src, test_co
# some tests can be ran even without zlib
@@ -21,7 +21,7 @@ import zipimport
import linecache
import doctest
import inspect
-import StringIO
+import io
from traceback import extract_tb, extract_stack, print_tb
raise_src = 'def do_raise(): raise TypeError\n'
@@ -32,7 +32,7 @@ def make_pyc(co, mtime):
if mtime < 0x7fffffff:
mtime = int(mtime)
else:
- mtime = int(-0x100000000L + long(mtime))
+ mtime = int(-0x100000000 + int(mtime))
pyc = imp.get_magic() + struct.pack("<i", int(mtime)) + data
return pyc
@@ -43,16 +43,13 @@ NOW = time.time()
test_pyc = make_pyc(test_co, NOW)
-if __debug__:
- pyc_ext = ".pyc"
-else:
- pyc_ext = ".pyo"
-
-
TESTMOD = "ziptestmodule"
TESTPACK = "ziptestpackage"
TESTPACK2 = "ziptestpackage2"
-TEMP_ZIP = os.path.abspath("junk95142" + os.extsep + "zip")
+TEMP_ZIP = os.path.abspath("junk95142.zip")
+
+pyc_file = imp.cache_from_source(TESTMOD + '.py')
+pyc_ext = ('.pyc' if __debug__ else '.pyo')
class UncompressedZipImportTestCase(ImportHooksBaseTestCase):
@@ -78,14 +75,11 @@ class UncompressedZipImportTestCase(ImportHooksBaseTestCase):
stuff = kw.get("stuff", None)
if stuff is not None:
# Prepend 'stuff' to the start of the zipfile
- f = open(TEMP_ZIP, "rb")
- data = f.read()
- f.close()
-
- f = open(TEMP_ZIP, "wb")
- f.write(stuff)
- f.write(data)
- f.close()
+ with open(TEMP_ZIP, "rb") as f:
+ data = f.read()
+ with open(TEMP_ZIP, "wb") as f:
+ f.write(stuff)
+ f.write(data)
sys.path.insert(0, TEMP_ZIP)
@@ -155,18 +149,16 @@ class UncompressedZipImportTestCase(ImportHooksBaseTestCase):
def testBadMagic(self):
# make pyc magic word invalid, forcing loading from .py
- m0 = ord(test_pyc[0])
- m0 ^= 0x04 # flip an arbitrary bit
- badmagic_pyc = chr(m0) + test_pyc[1:]
+ badmagic_pyc = bytearray(test_pyc)
+ badmagic_pyc[0] ^= 0x04 # flip an arbitrary bit
files = {TESTMOD + ".py": (NOW, test_src),
TESTMOD + pyc_ext: (NOW, badmagic_pyc)}
self.doTest(".py", files, TESTMOD)
def testBadMagic2(self):
# make pyc magic word invalid, causing an ImportError
- m0 = ord(test_pyc[0])
- m0 ^= 0x04 # flip an arbitrary bit
- badmagic_pyc = chr(m0) + test_pyc[1:]
+ badmagic_pyc = bytearray(test_pyc)
+ badmagic_pyc[0] ^= 0x04 # flip an arbitrary bit
files = {TESTMOD + pyc_ext: (NOW, badmagic_pyc)}
try:
self.doTest(".py", files, TESTMOD)
@@ -176,10 +168,10 @@ class UncompressedZipImportTestCase(ImportHooksBaseTestCase):
self.fail("expected ImportError; import from bad pyc")
def testBadMTime(self):
- t3 = ord(test_pyc[7])
- t3 ^= 0x02 # flip the second bit -- not the first as that one
- # isn't stored in the .py's mtime in the zip archive.
- badtime_pyc = test_pyc[:7] + chr(t3) + test_pyc[8:]
+ badtime_pyc = bytearray(test_pyc)
+ # flip the second bit -- not the first as that one isn't stored in the
+ # .py's mtime in the zip archive.
+ badtime_pyc[7] ^= 0x02
files = {TESTMOD + ".py": (NOW, test_src),
TESTMOD + pyc_ext: (NOW, badtime_pyc)}
self.doTest(".py", files, TESTMOD)
@@ -230,7 +222,8 @@ class UncompressedZipImportTestCase(ImportHooksBaseTestCase):
self.assertEqual(zi.get_source(TESTPACK), None)
self.assertEqual(zi.get_source(mod_path), None)
self.assertEqual(zi.get_filename(mod_path), mod.__file__)
- # To pass in the module name instead of the path, we must use the right importer
+ # To pass in the module name instead of the path, we must use the
+ # right importer
loader = mod.__loader__
self.assertEqual(loader.get_source(mod_name), None)
self.assertEqual(loader.get_filename(mod_name), mod.__file__)
@@ -264,8 +257,10 @@ class UncompressedZipImportTestCase(ImportHooksBaseTestCase):
mod = zi.load_module(TESTPACK2)
self.assertEqual(zi.get_filename(TESTPACK2), mod.__file__)
- self.assertEqual(zi.is_package(TESTPACK2 + os.sep + '__init__'), False)
- self.assertEqual(zi.is_package(TESTPACK2 + os.sep + TESTMOD), False)
+ self.assertEqual(
+ zi.is_package(TESTPACK2 + os.sep + '__init__'), False)
+ self.assertEqual(
+ zi.is_package(TESTPACK2 + os.sep + TESTMOD), False)
mod_path = TESTPACK2 + os.sep + TESTMOD
mod_name = module_path_to_dotted_name(mod_path)
@@ -274,7 +269,8 @@ class UncompressedZipImportTestCase(ImportHooksBaseTestCase):
self.assertEqual(zi.get_source(TESTPACK2), None)
self.assertEqual(zi.get_source(mod_path), None)
self.assertEqual(zi.get_filename(mod_path), mod.__file__)
- # To pass in the module name instead of the path, we must use the right importer
+ # To pass in the module name instead of the path, we must use the
+ # right importer
loader = mod.__loader__
self.assertEqual(loader.get_source(mod_name), None)
self.assertEqual(loader.get_filename(mod_name), mod.__file__)
@@ -287,7 +283,7 @@ class UncompressedZipImportTestCase(ImportHooksBaseTestCase):
z.compression = self.compression
try:
name = "testdata.dat"
- data = "".join([chr(x) for x in range(256)]) * 500
+ data = bytes(x for x in range(256))
z.writestr(name, data)
z.close()
zi = zipimport.zipimporter(TEMP_ZIP)
@@ -301,8 +297,8 @@ class UncompressedZipImportTestCase(ImportHooksBaseTestCase):
src = """if 1: # indent hack
def get_file():
return __file__
- if __loader__.get_data("some.data") != "some data":
- raise AssertionError, "bad data"\n"""
+ if __loader__.get_data("some.data") != b"some data":
+ raise AssertionError("bad data")\n"""
pyc = make_pyc(compile(src, "<???>", "exec"), NOW)
files = {TESTMOD + pyc_ext: (NOW, pyc),
"some.data": (NOW, "some data")}
@@ -313,7 +309,7 @@ class UncompressedZipImportTestCase(ImportHooksBaseTestCase):
# stuff at the beginning of the file
files = {TESTMOD + ".py": (NOW, test_src)}
self.doTest(".py", files, TESTMOD,
- stuff="Some Stuff"*31)
+ stuff=b"Some Stuff"*31)
def assertModuleSource(self, module):
self.assertEqual(inspect.getsource(module), test_src)
@@ -371,7 +367,7 @@ class UncompressedZipImportTestCase(ImportHooksBaseTestCase):
f,lno,n,line = extract_stack(tb.tb_frame, 1)[0]
self.assertEqual(line, raise_src.strip())
- s = StringIO.StringIO()
+ s = io.StringIO()
print_tb(tb, 1, s)
self.assertTrue(s.getvalue().endswith(raise_src))
else:
@@ -381,6 +377,20 @@ class UncompressedZipImportTestCase(ImportHooksBaseTestCase):
files = {TESTMOD + ".py": (NOW, raise_src)}
self.doTest(None, files, TESTMOD, call=self.doTraceback)
+ @unittest.skipIf(support.TESTFN_UNENCODABLE is None,
+ "need an unencodable filename")
+ def testUnencodable(self):
+ filename = support.TESTFN_UNENCODABLE + ".zip"
+ z = ZipFile(filename, "w")
+ zinfo = ZipInfo(TESTMOD + ".py", time.localtime(NOW))
+ zinfo.compress_type = self.compression
+ z.writestr(zinfo, test_src)
+ z.close()
+ try:
+ zipimport.zipimporter(filename)
+ finally:
+ os.remove(filename)
+
@unittest.skipUnless(zlib, "requires zlib")
class CompressedZipImportTestCase(UncompressedZipImportTestCase):
@@ -406,12 +416,12 @@ class BadFileZipImportTestCase(unittest.TestCase):
self.assertZipFailure('A' * 33000)
def testEmptyFile(self):
- test_support.unlink(TESTMOD)
+ support.unlink(TESTMOD)
open(TESTMOD, 'w+').close()
self.assertZipFailure(TESTMOD)
def testFileUnreadable(self):
- test_support.unlink(TESTMOD)
+ support.unlink(TESTMOD)
fd = os.open(TESTMOD, os.O_CREAT, 000)
try:
os.close(fd)
@@ -419,11 +429,11 @@ class BadFileZipImportTestCase(unittest.TestCase):
finally:
# If we leave "the read-only bit" set on Windows, nothing can
# delete TESTMOD, and later tests suffer bogus failures.
- os.chmod(TESTMOD, 0666)
- test_support.unlink(TESTMOD)
+ os.chmod(TESTMOD, 0o666)
+ support.unlink(TESTMOD)
def testNotZipFile(self):
- test_support.unlink(TESTMOD)
+ support.unlink(TESTMOD)
fp = open(TESTMOD, 'w+')
fp.write('a' * 22)
fp.close()
@@ -431,7 +441,7 @@ class BadFileZipImportTestCase(unittest.TestCase):
# XXX: disabled until this works on Big-endian machines
def _testBogusZipFile(self):
- test_support.unlink(TESTMOD)
+ support.unlink(TESTMOD)
fp = open(TESTMOD, 'w+')
fp.write(struct.pack('=I', 0x06054B50))
fp.write('a' * 18)
@@ -460,13 +470,13 @@ class BadFileZipImportTestCase(unittest.TestCase):
def test_main():
try:
- test_support.run_unittest(
+ support.run_unittest(
UncompressedZipImportTestCase,
CompressedZipImportTestCase,
BadFileZipImportTestCase,
)
finally:
- test_support.unlink(TESTMOD)
+ support.unlink(TESTMOD)
if __name__ == "__main__":
test_main()