+++ /dev/null
-import sys\r
-import os\r
-import marshal\r
-import imp\r
-import struct\r
-import time\r
-import unittest\r
-\r
-from test import test_support\r
-from test.test_importhooks import ImportHooksBaseTestCase, test_src, test_co\r
-\r
-# some tests can be ran even without zlib\r
-try:\r
- import zlib\r
-except ImportError:\r
- zlib = None\r
-\r
-from zipfile import ZipFile, ZipInfo, ZIP_STORED, ZIP_DEFLATED\r
-\r
-import zipimport\r
-import linecache\r
-import doctest\r
-import inspect\r
-import StringIO\r
-from traceback import extract_tb, extract_stack, print_tb\r
-raise_src = 'def do_raise(): raise TypeError\n'\r
-\r
-def make_pyc(co, mtime):\r
- data = marshal.dumps(co)\r
- if type(mtime) is type(0.0):\r
- # Mac mtimes need a bit of special casing\r
- if mtime < 0x7fffffff:\r
- mtime = int(mtime)\r
- else:\r
- mtime = int(-0x100000000L + long(mtime))\r
- pyc = imp.get_magic() + struct.pack("<i", int(mtime)) + data\r
- return pyc\r
-\r
-def module_path_to_dotted_name(path):\r
- return path.replace(os.sep, '.')\r
-\r
-NOW = time.time()\r
-test_pyc = make_pyc(test_co, NOW)\r
-\r
-\r
-if __debug__:\r
- pyc_ext = ".pyc"\r
-else:\r
- pyc_ext = ".pyo"\r
-\r
-\r
-TESTMOD = "ziptestmodule"\r
-TESTPACK = "ziptestpackage"\r
-TESTPACK2 = "ziptestpackage2"\r
-TEMP_ZIP = os.path.abspath("junk95142" + os.extsep + "zip")\r
-\r
-\r
-class UncompressedZipImportTestCase(ImportHooksBaseTestCase):\r
-\r
- compression = ZIP_STORED\r
-\r
- def setUp(self):\r
- # We're reusing the zip archive path, so we must clear the\r
- # cached directory info and linecache\r
- linecache.clearcache()\r
- zipimport._zip_directory_cache.clear()\r
- ImportHooksBaseTestCase.setUp(self)\r
-\r
- def doTest(self, expected_ext, files, *modules, **kw):\r
- z = ZipFile(TEMP_ZIP, "w")\r
- try:\r
- for name, (mtime, data) in files.items():\r
- zinfo = ZipInfo(name, time.localtime(mtime))\r
- zinfo.compress_type = self.compression\r
- z.writestr(zinfo, data)\r
- z.close()\r
-\r
- stuff = kw.get("stuff", None)\r
- if stuff is not None:\r
- # Prepend 'stuff' to the start of the zipfile\r
- f = open(TEMP_ZIP, "rb")\r
- data = f.read()\r
- f.close()\r
-\r
- f = open(TEMP_ZIP, "wb")\r
- f.write(stuff)\r
- f.write(data)\r
- f.close()\r
-\r
- sys.path.insert(0, TEMP_ZIP)\r
-\r
- mod = __import__(".".join(modules), globals(), locals(),\r
- ["__dummy__"])\r
-\r
- call = kw.get('call')\r
- if call is not None:\r
- call(mod)\r
-\r
- if expected_ext:\r
- file = mod.get_file()\r
- self.assertEqual(file, os.path.join(TEMP_ZIP,\r
- *modules) + expected_ext)\r
- finally:\r
- z.close()\r
- os.remove(TEMP_ZIP)\r
-\r
- def testAFakeZlib(self):\r
- #\r
- # This could cause a stack overflow before: importing zlib.py\r
- # from a compressed archive would cause zlib to be imported\r
- # which would find zlib.py in the archive, which would... etc.\r
- #\r
- # This test *must* be executed first: it must be the first one\r
- # to trigger zipimport to import zlib (zipimport caches the\r
- # zlib.decompress function object, after which the problem being\r
- # tested here wouldn't be a problem anymore...\r
- # (Hence the 'A' in the test method name: to make it the first\r
- # item in a list sorted by name, like unittest.makeSuite() does.)\r
- #\r
- # This test fails on platforms on which the zlib module is\r
- # statically linked, but the problem it tests for can't\r
- # occur in that case (builtin modules are always found first),\r
- # so we'll simply skip it then. Bug #765456.\r
- #\r
- if "zlib" in sys.builtin_module_names:\r
- return\r
- if "zlib" in sys.modules:\r
- del sys.modules["zlib"]\r
- files = {"zlib.py": (NOW, test_src)}\r
- try:\r
- self.doTest(".py", files, "zlib")\r
- except ImportError:\r
- if self.compression != ZIP_DEFLATED:\r
- self.fail("expected test to not raise ImportError")\r
- else:\r
- if self.compression != ZIP_STORED:\r
- self.fail("expected test to raise ImportError")\r
-\r
- def testPy(self):\r
- files = {TESTMOD + ".py": (NOW, test_src)}\r
- self.doTest(".py", files, TESTMOD)\r
-\r
- def testPyc(self):\r
- files = {TESTMOD + pyc_ext: (NOW, test_pyc)}\r
- self.doTest(pyc_ext, files, TESTMOD)\r
-\r
- def testBoth(self):\r
- files = {TESTMOD + ".py": (NOW, test_src),\r
- TESTMOD + pyc_ext: (NOW, test_pyc)}\r
- self.doTest(pyc_ext, files, TESTMOD)\r
-\r
- def testEmptyPy(self):\r
- files = {TESTMOD + ".py": (NOW, "")}\r
- self.doTest(None, files, TESTMOD)\r
-\r
- def testBadMagic(self):\r
- # make pyc magic word invalid, forcing loading from .py\r
- m0 = ord(test_pyc[0])\r
- m0 ^= 0x04 # flip an arbitrary bit\r
- badmagic_pyc = chr(m0) + test_pyc[1:]\r
- files = {TESTMOD + ".py": (NOW, test_src),\r
- TESTMOD + pyc_ext: (NOW, badmagic_pyc)}\r
- self.doTest(".py", files, TESTMOD)\r
-\r
- def testBadMagic2(self):\r
- # make pyc magic word invalid, causing an ImportError\r
- m0 = ord(test_pyc[0])\r
- m0 ^= 0x04 # flip an arbitrary bit\r
- badmagic_pyc = chr(m0) + test_pyc[1:]\r
- files = {TESTMOD + pyc_ext: (NOW, badmagic_pyc)}\r
- try:\r
- self.doTest(".py", files, TESTMOD)\r
- except ImportError:\r
- pass\r
- else:\r
- self.fail("expected ImportError; import from bad pyc")\r
-\r
- def testBadMTime(self):\r
- t3 = ord(test_pyc[7])\r
- t3 ^= 0x02 # flip the second bit -- not the first as that one\r
- # isn't stored in the .py's mtime in the zip archive.\r
- badtime_pyc = test_pyc[:7] + chr(t3) + test_pyc[8:]\r
- files = {TESTMOD + ".py": (NOW, test_src),\r
- TESTMOD + pyc_ext: (NOW, badtime_pyc)}\r
- self.doTest(".py", files, TESTMOD)\r
-\r
- def testPackage(self):\r
- packdir = TESTPACK + os.sep\r
- files = {packdir + "__init__" + pyc_ext: (NOW, test_pyc),\r
- packdir + TESTMOD + pyc_ext: (NOW, test_pyc)}\r
- self.doTest(pyc_ext, files, TESTPACK, TESTMOD)\r
-\r
- def testDeepPackage(self):\r
- packdir = TESTPACK + os.sep\r
- packdir2 = packdir + TESTPACK2 + os.sep\r
- files = {packdir + "__init__" + pyc_ext: (NOW, test_pyc),\r
- packdir2 + "__init__" + pyc_ext: (NOW, test_pyc),\r
- packdir2 + TESTMOD + pyc_ext: (NOW, test_pyc)}\r
- self.doTest(pyc_ext, files, TESTPACK, TESTPACK2, TESTMOD)\r
-\r
- def testZipImporterMethods(self):\r
- packdir = TESTPACK + os.sep\r
- packdir2 = packdir + TESTPACK2 + os.sep\r
- files = {packdir + "__init__" + pyc_ext: (NOW, test_pyc),\r
- packdir2 + "__init__" + pyc_ext: (NOW, test_pyc),\r
- packdir2 + TESTMOD + pyc_ext: (NOW, test_pyc)}\r
-\r
- z = ZipFile(TEMP_ZIP, "w")\r
- try:\r
- for name, (mtime, data) in files.items():\r
- zinfo = ZipInfo(name, time.localtime(mtime))\r
- zinfo.compress_type = self.compression\r
- z.writestr(zinfo, data)\r
- z.close()\r
-\r
- zi = zipimport.zipimporter(TEMP_ZIP)\r
- self.assertEqual(zi.archive, TEMP_ZIP)\r
- self.assertEqual(zi.is_package(TESTPACK), True)\r
- mod = zi.load_module(TESTPACK)\r
- self.assertEqual(zi.get_filename(TESTPACK), mod.__file__)\r
-\r
- self.assertEqual(zi.is_package(packdir + '__init__'), False)\r
- self.assertEqual(zi.is_package(packdir + TESTPACK2), True)\r
- self.assertEqual(zi.is_package(packdir2 + TESTMOD), False)\r
-\r
- mod_path = packdir2 + TESTMOD\r
- mod_name = module_path_to_dotted_name(mod_path)\r
- __import__(mod_name)\r
- mod = sys.modules[mod_name]\r
- self.assertEqual(zi.get_source(TESTPACK), None)\r
- self.assertEqual(zi.get_source(mod_path), None)\r
- self.assertEqual(zi.get_filename(mod_path), mod.__file__)\r
- # To pass in the module name instead of the path, we must use the right importer\r
- loader = mod.__loader__\r
- self.assertEqual(loader.get_source(mod_name), None)\r
- self.assertEqual(loader.get_filename(mod_name), mod.__file__)\r
-\r
- # test prefix and archivepath members\r
- zi2 = zipimport.zipimporter(TEMP_ZIP + os.sep + TESTPACK)\r
- self.assertEqual(zi2.archive, TEMP_ZIP)\r
- self.assertEqual(zi2.prefix, TESTPACK + os.sep)\r
- finally:\r
- z.close()\r
- os.remove(TEMP_ZIP)\r
-\r
- def testZipImporterMethodsInSubDirectory(self):\r
- packdir = TESTPACK + os.sep\r
- packdir2 = packdir + TESTPACK2 + os.sep\r
- files = {packdir2 + "__init__" + pyc_ext: (NOW, test_pyc),\r
- packdir2 + TESTMOD + pyc_ext: (NOW, test_pyc)}\r
-\r
- z = ZipFile(TEMP_ZIP, "w")\r
- try:\r
- for name, (mtime, data) in files.items():\r
- zinfo = ZipInfo(name, time.localtime(mtime))\r
- zinfo.compress_type = self.compression\r
- z.writestr(zinfo, data)\r
- z.close()\r
-\r
- zi = zipimport.zipimporter(TEMP_ZIP + os.sep + packdir)\r
- self.assertEqual(zi.archive, TEMP_ZIP)\r
- self.assertEqual(zi.prefix, packdir)\r
- self.assertEqual(zi.is_package(TESTPACK2), True)\r
- mod = zi.load_module(TESTPACK2)\r
- self.assertEqual(zi.get_filename(TESTPACK2), mod.__file__)\r
-\r
- self.assertEqual(zi.is_package(TESTPACK2 + os.sep + '__init__'), False)\r
- self.assertEqual(zi.is_package(TESTPACK2 + os.sep + TESTMOD), False)\r
-\r
- mod_path = TESTPACK2 + os.sep + TESTMOD\r
- mod_name = module_path_to_dotted_name(mod_path)\r
- __import__(mod_name)\r
- mod = sys.modules[mod_name]\r
- self.assertEqual(zi.get_source(TESTPACK2), None)\r
- self.assertEqual(zi.get_source(mod_path), None)\r
- self.assertEqual(zi.get_filename(mod_path), mod.__file__)\r
- # To pass in the module name instead of the path, we must use the right importer\r
- loader = mod.__loader__\r
- self.assertEqual(loader.get_source(mod_name), None)\r
- self.assertEqual(loader.get_filename(mod_name), mod.__file__)\r
- finally:\r
- z.close()\r
- os.remove(TEMP_ZIP)\r
-\r
- def testGetData(self):\r
- z = ZipFile(TEMP_ZIP, "w")\r
- z.compression = self.compression\r
- try:\r
- name = "testdata.dat"\r
- data = "".join([chr(x) for x in range(256)]) * 500\r
- z.writestr(name, data)\r
- z.close()\r
- zi = zipimport.zipimporter(TEMP_ZIP)\r
- self.assertEqual(data, zi.get_data(name))\r
- self.assertIn('zipimporter object', repr(zi))\r
- finally:\r
- z.close()\r
- os.remove(TEMP_ZIP)\r
-\r
- def testImporterAttr(self):\r
- src = """if 1: # indent hack\r
- def get_file():\r
- return __file__\r
- if __loader__.get_data("some.data") != "some data":\r
- raise AssertionError, "bad data"\n"""\r
- pyc = make_pyc(compile(src, "<???>", "exec"), NOW)\r
- files = {TESTMOD + pyc_ext: (NOW, pyc),\r
- "some.data": (NOW, "some data")}\r
- self.doTest(pyc_ext, files, TESTMOD)\r
-\r
- def testImport_WithStuff(self):\r
- # try importing from a zipfile which contains additional\r
- # stuff at the beginning of the file\r
- files = {TESTMOD + ".py": (NOW, test_src)}\r
- self.doTest(".py", files, TESTMOD,\r
- stuff="Some Stuff"*31)\r
-\r
- def assertModuleSource(self, module):\r
- self.assertEqual(inspect.getsource(module), test_src)\r
-\r
- def testGetSource(self):\r
- files = {TESTMOD + ".py": (NOW, test_src)}\r
- self.doTest(".py", files, TESTMOD, call=self.assertModuleSource)\r
-\r
- def testGetCompiledSource(self):\r
- pyc = make_pyc(compile(test_src, "<???>", "exec"), NOW)\r
- files = {TESTMOD + ".py": (NOW, test_src),\r
- TESTMOD + pyc_ext: (NOW, pyc)}\r
- self.doTest(pyc_ext, files, TESTMOD, call=self.assertModuleSource)\r
-\r
- def runDoctest(self, callback):\r
- files = {TESTMOD + ".py": (NOW, test_src),\r
- "xyz.txt": (NOW, ">>> log.append(True)\n")}\r
- self.doTest(".py", files, TESTMOD, call=callback)\r
-\r
- def doDoctestFile(self, module):\r
- log = []\r
- old_master, doctest.master = doctest.master, None\r
- try:\r
- doctest.testfile(\r
- 'xyz.txt', package=module, module_relative=True,\r
- globs=locals()\r
- )\r
- finally:\r
- doctest.master = old_master\r
- self.assertEqual(log,[True])\r
-\r
- def testDoctestFile(self):\r
- self.runDoctest(self.doDoctestFile)\r
-\r
- def doDoctestSuite(self, module):\r
- log = []\r
- doctest.DocFileTest(\r
- 'xyz.txt', package=module, module_relative=True,\r
- globs=locals()\r
- ).run()\r
- self.assertEqual(log,[True])\r
-\r
- def testDoctestSuite(self):\r
- self.runDoctest(self.doDoctestSuite)\r
-\r
- def doTraceback(self, module):\r
- try:\r
- module.do_raise()\r
- except:\r
- tb = sys.exc_info()[2].tb_next\r
-\r
- f,lno,n,line = extract_tb(tb, 1)[0]\r
- self.assertEqual(line, raise_src.strip())\r
-\r
- f,lno,n,line = extract_stack(tb.tb_frame, 1)[0]\r
- self.assertEqual(line, raise_src.strip())\r
-\r
- s = StringIO.StringIO()\r
- print_tb(tb, 1, s)\r
- self.assertTrue(s.getvalue().endswith(raise_src))\r
- else:\r
- raise AssertionError("This ought to be impossible")\r
-\r
- def testTraceback(self):\r
- files = {TESTMOD + ".py": (NOW, raise_src)}\r
- self.doTest(None, files, TESTMOD, call=self.doTraceback)\r
-\r
-\r
-@unittest.skipUnless(zlib, "requires zlib")\r
-class CompressedZipImportTestCase(UncompressedZipImportTestCase):\r
- compression = ZIP_DEFLATED\r
-\r
-\r
-class BadFileZipImportTestCase(unittest.TestCase):\r
- def assertZipFailure(self, filename):\r
- self.assertRaises(zipimport.ZipImportError,\r
- zipimport.zipimporter, filename)\r
-\r
- def testNoFile(self):\r
- self.assertZipFailure('AdfjdkFJKDFJjdklfjs')\r
-\r
- def testEmptyFilename(self):\r
- self.assertZipFailure('')\r
-\r
- def testBadArgs(self):\r
- self.assertRaises(TypeError, zipimport.zipimporter, None)\r
- self.assertRaises(TypeError, zipimport.zipimporter, TESTMOD, kwd=None)\r
-\r
- def testFilenameTooLong(self):\r
- self.assertZipFailure('A' * 33000)\r
-\r
- def testEmptyFile(self):\r
- test_support.unlink(TESTMOD)\r
- open(TESTMOD, 'w+').close()\r
- self.assertZipFailure(TESTMOD)\r
-\r
- def testFileUnreadable(self):\r
- test_support.unlink(TESTMOD)\r
- fd = os.open(TESTMOD, os.O_CREAT, 000)\r
- try:\r
- os.close(fd)\r
- self.assertZipFailure(TESTMOD)\r
- finally:\r
- # If we leave "the read-only bit" set on Windows, nothing can\r
- # delete TESTMOD, and later tests suffer bogus failures.\r
- os.chmod(TESTMOD, 0666)\r
- test_support.unlink(TESTMOD)\r
-\r
- def testNotZipFile(self):\r
- test_support.unlink(TESTMOD)\r
- fp = open(TESTMOD, 'w+')\r
- fp.write('a' * 22)\r
- fp.close()\r
- self.assertZipFailure(TESTMOD)\r
-\r
- # XXX: disabled until this works on Big-endian machines\r
- def _testBogusZipFile(self):\r
- test_support.unlink(TESTMOD)\r
- fp = open(TESTMOD, 'w+')\r
- fp.write(struct.pack('=I', 0x06054B50))\r
- fp.write('a' * 18)\r
- fp.close()\r
- z = zipimport.zipimporter(TESTMOD)\r
-\r
- try:\r
- self.assertRaises(TypeError, z.find_module, None)\r
- self.assertRaises(TypeError, z.load_module, None)\r
- self.assertRaises(TypeError, z.is_package, None)\r
- self.assertRaises(TypeError, z.get_code, None)\r
- self.assertRaises(TypeError, z.get_data, None)\r
- self.assertRaises(TypeError, z.get_source, None)\r
-\r
- error = zipimport.ZipImportError\r
- self.assertEqual(z.find_module('abc'), None)\r
-\r
- self.assertRaises(error, z.load_module, 'abc')\r
- self.assertRaises(error, z.get_code, 'abc')\r
- self.assertRaises(IOError, z.get_data, 'abc')\r
- self.assertRaises(error, z.get_source, 'abc')\r
- self.assertRaises(error, z.is_package, 'abc')\r
- finally:\r
- zipimport._zip_directory_cache.clear()\r
-\r
-\r
-def test_main():\r
- try:\r
- test_support.run_unittest(\r
- UncompressedZipImportTestCase,\r
- CompressedZipImportTestCase,\r
- BadFileZipImportTestCase,\r
- )\r
- finally:\r
- test_support.unlink(TESTMOD)\r
-\r
-if __name__ == "__main__":\r
- test_main()\r