Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Static Public Attributes
test.test_zipimport.UncompressedZipImportTestCase Class Reference
Inheritance diagram for test.test_zipimport.UncompressedZipImportTestCase:
Inheritance graph
[legend]
Collaboration diagram for test.test_zipimport.UncompressedZipImportTestCase:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def setUp
def doTest
def testAFakeZlib
def testPy
def testPyc
def testBoth
def testEmptyPy
def testBadMagic
def testBadMagic2
def testBadMTime
def testPackage
def testDeepPackage
def testZipImporterMethods
def testZipImporterMethodsInSubDirectory
def testGetData
def testImporterAttr
def testImport_WithStuff
def assertModuleSource
def testGetSource
def testGetCompiledSource
def runDoctest
def doDoctestFile
def testDoctestFile
def doDoctestSuite
def testDoctestSuite
def doTraceback
def testTraceback
def testUnencodable
def tearDown

Public Attributes

 path
 meta_path
 path_hooks
 modules_before

Static Public Attributes

 compression = ZIP_STORED

Detailed Description

Definition at line 55 of file test_zipimport.py.


Member Function Documentation

Definition at line 314 of file test_zipimport.py.

00314 
00315     def assertModuleSource(self, module):
00316         self.assertEqual(inspect.getsource(module), test_src)

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 332 of file test_zipimport.py.

00332 
00333     def doDoctestFile(self, module):
00334         log = []
00335         old_master, doctest.master = doctest.master, None
00336         try:
00337             doctest.testfile(
00338                 'xyz.txt', package=module, module_relative=True,
00339                 globs=locals()
00340             )
00341         finally:
00342             doctest.master = old_master
00343         self.assertEqual(log,[True])

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 347 of file test_zipimport.py.

00347 
00348     def doDoctestSuite(self, module):
00349         log = []
00350         doctest.DocFileTest(
00351             'xyz.txt', package=module, module_relative=True,
00352             globs=locals()
00353         ).run()
00354         self.assertEqual(log,[True])

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_zipimport.UncompressedZipImportTestCase.doTest (   self,
  expected_ext,
  files,
  modules,
  kw 
)

Definition at line 66 of file test_zipimport.py.

00066 
00067     def doTest(self, expected_ext, files, *modules, **kw):
00068         z = ZipFile(TEMP_ZIP, "w")
00069         try:
00070             for name, (mtime, data) in files.items():
00071                 zinfo = ZipInfo(name, time.localtime(mtime))
00072                 zinfo.compress_type = self.compression
00073                 z.writestr(zinfo, data)
00074             z.close()
00075 
00076             stuff = kw.get("stuff", None)
00077             if stuff is not None:
00078                 # Prepend 'stuff' to the start of the zipfile
00079                 with open(TEMP_ZIP, "rb") as f:
00080                     data = f.read()
00081                 with open(TEMP_ZIP, "wb") as f:
00082                     f.write(stuff)
00083                     f.write(data)
00084 
00085             sys.path.insert(0, TEMP_ZIP)
00086 
00087             mod = __import__(".".join(modules), globals(), locals(),
00088                              ["__dummy__"])
00089 
00090             call = kw.get('call')
00091             if call is not None:
00092                 call(mod)
00093 
00094             if expected_ext:
00095                 file = mod.get_file()
00096                 self.assertEqual(file, os.path.join(TEMP_ZIP,
00097                                  *modules) + expected_ext)
00098         finally:
00099             z.close()
00100             os.remove(TEMP_ZIP)

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 358 of file test_zipimport.py.

00358 
00359     def doTraceback(self, module):
00360         try:
00361             module.do_raise()
00362         except:
00363             tb = sys.exc_info()[2].tb_next
00364 
00365             f,lno,n,line = extract_tb(tb, 1)[0]
00366             self.assertEqual(line, raise_src.strip())
00367 
00368             f,lno,n,line = extract_stack(tb.tb_frame, 1)[0]
00369             self.assertEqual(line, raise_src.strip())
00370 
00371             s = io.StringIO()
00372             print_tb(tb, 1, s)
00373             self.assertTrue(s.getvalue().endswith(raise_src))
00374         else:
00375             raise AssertionError("This ought to be impossible")

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 327 of file test_zipimport.py.

00327 
00328     def runDoctest(self, callback):
00329         files = {TESTMOD + ".py": (NOW, test_src),
00330                  "xyz.txt": (NOW, ">>> log.append(True)\n")}
00331         self.doTest(".py", files, TESTMOD, call=callback)

Here is the call graph for this function:

Here is the caller graph for this function:

Reimplemented from test.test_importhooks.ImportHooksBaseTestCase.

Definition at line 59 of file test_zipimport.py.

00059 
00060     def setUp(self):
00061         # We're reusing the zip archive path, so we must clear the
00062         # cached directory info and linecache
00063         linecache.clearcache()
00064         zipimport._zip_directory_cache.clear()
00065         ImportHooksBaseTestCase.setUp(self)

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 148 of file test_importhooks.py.

00148 
00149     def tearDown(self):
00150         sys.path[:] = self.path
00151         sys.meta_path[:] = self.meta_path
00152         sys.path_hooks[:] = self.path_hooks
00153         sys.path_importer_cache.clear()
00154         support.modules_cleanup(*self.modules_before)
00155 

Here is the caller graph for this function:

Definition at line 101 of file test_zipimport.py.

00101 
00102     def testAFakeZlib(self):
00103         #
00104         # This could cause a stack overflow before: importing zlib.py
00105         # from a compressed archive would cause zlib to be imported
00106         # which would find zlib.py in the archive, which would... etc.
00107         #
00108         # This test *must* be executed first: it must be the first one
00109         # to trigger zipimport to import zlib (zipimport caches the
00110         # zlib.decompress function object, after which the problem being
00111         # tested here wouldn't be a problem anymore...
00112         # (Hence the 'A' in the test method name: to make it the first
00113         # item in a list sorted by name, like unittest.makeSuite() does.)
00114         #
00115         # This test fails on platforms on which the zlib module is
00116         # statically linked, but the problem it tests for can't
00117         # occur in that case (builtin modules are always found first),
00118         # so we'll simply skip it then. Bug #765456.
00119         #
00120         if "zlib" in sys.builtin_module_names:
00121             return
00122         if "zlib" in sys.modules:
00123             del sys.modules["zlib"]
00124         files = {"zlib.py": (NOW, test_src)}
00125         try:
00126             self.doTest(".py", files, "zlib")
00127         except ImportError:
00128             if self.compression != ZIP_DEFLATED:
00129                 self.fail("expected test to not raise ImportError")
00130         else:
00131             if self.compression != ZIP_STORED:
00132                 self.fail("expected test to raise ImportError")

Here is the call graph for this function:

Definition at line 150 of file test_zipimport.py.

00150 
00151     def testBadMagic(self):
00152         # make pyc magic word invalid, forcing loading from .py
00153         badmagic_pyc = bytearray(test_pyc)
00154         badmagic_pyc[0] ^= 0x04  # flip an arbitrary bit
00155         files = {TESTMOD + ".py": (NOW, test_src),
00156                  TESTMOD + pyc_ext: (NOW, badmagic_pyc)}
00157         self.doTest(".py", files, TESTMOD)

Here is the call graph for this function:

Definition at line 158 of file test_zipimport.py.

00158 
00159     def testBadMagic2(self):
00160         # make pyc magic word invalid, causing an ImportError
00161         badmagic_pyc = bytearray(test_pyc)
00162         badmagic_pyc[0] ^= 0x04  # flip an arbitrary bit
00163         files = {TESTMOD + pyc_ext: (NOW, badmagic_pyc)}
00164         try:
00165             self.doTest(".py", files, TESTMOD)
00166         except ImportError:
00167             pass
00168         else:
00169             self.fail("expected ImportError; import from bad pyc")

Here is the call graph for this function:

Definition at line 170 of file test_zipimport.py.

00170 
00171     def testBadMTime(self):
00172         badtime_pyc = bytearray(test_pyc)
00173         # flip the second bit -- not the first as that one isn't stored in the
00174         # .py's mtime in the zip archive.
00175         badtime_pyc[7] ^= 0x02
00176         files = {TESTMOD + ".py": (NOW, test_src),
00177                  TESTMOD + pyc_ext: (NOW, badtime_pyc)}
00178         self.doTest(".py", files, TESTMOD)

Here is the call graph for this function:

Definition at line 141 of file test_zipimport.py.

00141 
00142     def testBoth(self):
00143         files = {TESTMOD + ".py": (NOW, test_src),
00144                  TESTMOD + pyc_ext: (NOW, test_pyc)}
00145         self.doTest(pyc_ext, files, TESTMOD)

Here is the call graph for this function:

Definition at line 185 of file test_zipimport.py.

00185 
00186     def testDeepPackage(self):
00187         packdir = TESTPACK + os.sep
00188         packdir2 = packdir + TESTPACK2 + os.sep
00189         files = {packdir + "__init__" + pyc_ext: (NOW, test_pyc),
00190                  packdir2 + "__init__" + pyc_ext: (NOW, test_pyc),
00191                  packdir2 + TESTMOD + pyc_ext: (NOW, test_pyc)}
00192         self.doTest(pyc_ext, files, TESTPACK, TESTPACK2, TESTMOD)

Here is the call graph for this function:

Definition at line 344 of file test_zipimport.py.

00344 
00345     def testDoctestFile(self):
00346         self.runDoctest(self.doDoctestFile)

Here is the call graph for this function:

Definition at line 355 of file test_zipimport.py.

00355 
00356     def testDoctestSuite(self):
00357         self.runDoctest(self.doDoctestSuite)

Here is the call graph for this function:

Definition at line 146 of file test_zipimport.py.

00146 
00147     def testEmptyPy(self):
00148         files = {TESTMOD + ".py": (NOW, "")}
00149         self.doTest(None, files, TESTMOD)

Here is the call graph for this function:

Definition at line 321 of file test_zipimport.py.

00321 
00322     def testGetCompiledSource(self):
00323         pyc = make_pyc(compile(test_src, "<???>", "exec"), NOW)
00324         files = {TESTMOD + ".py": (NOW, test_src),
00325                  TESTMOD + pyc_ext: (NOW, pyc)}
00326         self.doTest(pyc_ext, files, TESTMOD, call=self.assertModuleSource)

Here is the call graph for this function:

Definition at line 281 of file test_zipimport.py.

00281 
00282     def testGetData(self):
00283         z = ZipFile(TEMP_ZIP, "w")
00284         z.compression = self.compression
00285         try:
00286             name = "testdata.dat"
00287             data = bytes(x for x in range(256))
00288             z.writestr(name, data)
00289             z.close()
00290             zi = zipimport.zipimporter(TEMP_ZIP)
00291             self.assertEqual(data, zi.get_data(name))
00292             self.assertIn('zipimporter object', repr(zi))
00293         finally:
00294             z.close()
00295             os.remove(TEMP_ZIP)

Here is the call graph for this function:

Definition at line 317 of file test_zipimport.py.

00317 
00318     def testGetSource(self):
00319         files = {TESTMOD + ".py": (NOW, test_src)}
00320         self.doTest(".py", files, TESTMOD, call=self.assertModuleSource)

Here is the call graph for this function:

Definition at line 307 of file test_zipimport.py.

00307 
00308     def testImport_WithStuff(self):
00309         # try importing from a zipfile which contains additional
00310         # stuff at the beginning of the file
00311         files = {TESTMOD + ".py": (NOW, test_src)}
00312         self.doTest(".py", files, TESTMOD,
00313                     stuff=b"Some Stuff"*31)

Here is the call graph for this function:

Definition at line 296 of file test_zipimport.py.

00296 
00297     def testImporterAttr(self):
00298         src = """if 1:  # indent hack
00299         def get_file():
00300             return __file__
00301         if __loader__.get_data("some.data") != b"some data":
00302             raise AssertionError("bad data")\n"""
00303         pyc = make_pyc(compile(src, "<???>", "exec"), NOW)
00304         files = {TESTMOD + pyc_ext: (NOW, pyc),
00305                  "some.data": (NOW, "some data")}
00306         self.doTest(pyc_ext, files, TESTMOD)

Here is the call graph for this function:

Definition at line 179 of file test_zipimport.py.

00179 
00180     def testPackage(self):
00181         packdir = TESTPACK + os.sep
00182         files = {packdir + "__init__" + pyc_ext: (NOW, test_pyc),
00183                  packdir + TESTMOD + pyc_ext: (NOW, test_pyc)}
00184         self.doTest(pyc_ext, files, TESTPACK, TESTMOD)

Here is the call graph for this function:

Definition at line 133 of file test_zipimport.py.

00133 
00134     def testPy(self):
00135         files = {TESTMOD + ".py": (NOW, test_src)}
00136         self.doTest(".py", files, TESTMOD)

Here is the call graph for this function:

Definition at line 137 of file test_zipimport.py.

00137 
00138     def testPyc(self):
00139         files = {TESTMOD + pyc_ext: (NOW, test_pyc)}
00140         self.doTest(pyc_ext, files, TESTMOD)

Here is the call graph for this function:

Definition at line 376 of file test_zipimport.py.

00376 
00377     def testTraceback(self):
00378         files = {TESTMOD + ".py": (NOW, raise_src)}
00379         self.doTest(None, files, TESTMOD, call=self.doTraceback)

Here is the call graph for this function:

Definition at line 382 of file test_zipimport.py.

00382 
00383     def testUnencodable(self):
00384         filename = support.TESTFN_UNENCODABLE + ".zip"
00385         z = ZipFile(filename, "w")
00386         zinfo = ZipInfo(TESTMOD + ".py", time.localtime(NOW))
00387         zinfo.compress_type = self.compression
00388         z.writestr(zinfo, test_src)
00389         z.close()
00390         try:
00391             zipimport.zipimporter(filename)
00392         finally:
00393             os.remove(filename)
00394 
00395 
@unittest.skipUnless(zlib, "requires zlib")

Definition at line 193 of file test_zipimport.py.

00193 
00194     def testZipImporterMethods(self):
00195         packdir = TESTPACK + os.sep
00196         packdir2 = packdir + TESTPACK2 + os.sep
00197         files = {packdir + "__init__" + pyc_ext: (NOW, test_pyc),
00198                  packdir2 + "__init__" + pyc_ext: (NOW, test_pyc),
00199                  packdir2 + TESTMOD + pyc_ext: (NOW, test_pyc)}
00200 
00201         z = ZipFile(TEMP_ZIP, "w")
00202         try:
00203             for name, (mtime, data) in files.items():
00204                 zinfo = ZipInfo(name, time.localtime(mtime))
00205                 zinfo.compress_type = self.compression
00206                 z.writestr(zinfo, data)
00207             z.close()
00208 
00209             zi = zipimport.zipimporter(TEMP_ZIP)
00210             self.assertEqual(zi.archive, TEMP_ZIP)
00211             self.assertEqual(zi.is_package(TESTPACK), True)
00212             mod = zi.load_module(TESTPACK)
00213             self.assertEqual(zi.get_filename(TESTPACK), mod.__file__)
00214 
00215             self.assertEqual(zi.is_package(packdir + '__init__'), False)
00216             self.assertEqual(zi.is_package(packdir + TESTPACK2), True)
00217             self.assertEqual(zi.is_package(packdir2 + TESTMOD), False)
00218 
00219             mod_path = packdir2 + TESTMOD
00220             mod_name = module_path_to_dotted_name(mod_path)
00221             __import__(mod_name)
00222             mod = sys.modules[mod_name]
00223             self.assertEqual(zi.get_source(TESTPACK), None)
00224             self.assertEqual(zi.get_source(mod_path), None)
00225             self.assertEqual(zi.get_filename(mod_path), mod.__file__)
00226             # To pass in the module name instead of the path, we must use the
00227             # right importer
00228             loader = mod.__loader__
00229             self.assertEqual(loader.get_source(mod_name), None)
00230             self.assertEqual(loader.get_filename(mod_name), mod.__file__)
00231 
00232             # test prefix and archivepath members
00233             zi2 = zipimport.zipimporter(TEMP_ZIP + os.sep + TESTPACK)
00234             self.assertEqual(zi2.archive, TEMP_ZIP)
00235             self.assertEqual(zi2.prefix, TESTPACK + os.sep)
00236         finally:
00237             z.close()
00238             os.remove(TEMP_ZIP)

Here is the call graph for this function:

Definition at line 239 of file test_zipimport.py.

00239 
00240     def testZipImporterMethodsInSubDirectory(self):
00241         packdir = TESTPACK + os.sep
00242         packdir2 = packdir + TESTPACK2 + os.sep
00243         files = {packdir2 + "__init__" + pyc_ext: (NOW, test_pyc),
00244                  packdir2 + TESTMOD + pyc_ext: (NOW, test_pyc)}
00245 
00246         z = ZipFile(TEMP_ZIP, "w")
00247         try:
00248             for name, (mtime, data) in files.items():
00249                 zinfo = ZipInfo(name, time.localtime(mtime))
00250                 zinfo.compress_type = self.compression
00251                 z.writestr(zinfo, data)
00252             z.close()
00253 
00254             zi = zipimport.zipimporter(TEMP_ZIP + os.sep + packdir)
00255             self.assertEqual(zi.archive, TEMP_ZIP)
00256             self.assertEqual(zi.prefix, packdir)
00257             self.assertEqual(zi.is_package(TESTPACK2), True)
00258             mod = zi.load_module(TESTPACK2)
00259             self.assertEqual(zi.get_filename(TESTPACK2), mod.__file__)
00260 
00261             self.assertEqual(
00262                 zi.is_package(TESTPACK2 + os.sep + '__init__'), False)
00263             self.assertEqual(
00264                 zi.is_package(TESTPACK2 + os.sep + TESTMOD), False)
00265 
00266             mod_path = TESTPACK2 + os.sep + TESTMOD
00267             mod_name = module_path_to_dotted_name(mod_path)
00268             __import__(mod_name)
00269             mod = sys.modules[mod_name]
00270             self.assertEqual(zi.get_source(TESTPACK2), None)
00271             self.assertEqual(zi.get_source(mod_path), None)
00272             self.assertEqual(zi.get_filename(mod_path), mod.__file__)
00273             # To pass in the module name instead of the path, we must use the
00274             # right importer
00275             loader = mod.__loader__
00276             self.assertEqual(loader.get_source(mod_name), None)
00277             self.assertEqual(loader.get_filename(mod_name), mod.__file__)
00278         finally:
00279             z.close()
00280             os.remove(TEMP_ZIP)

Here is the call graph for this function:


Member Data Documentation

Reimplemented in test.test_zipimport.CompressedZipImportTestCase.

Definition at line 57 of file test_zipimport.py.

Definition at line 143 of file test_importhooks.py.

Definition at line 146 of file test_importhooks.py.

Definition at line 142 of file test_importhooks.py.

Definition at line 144 of file test_importhooks.py.


The documentation for this class was generated from the following file: