diff -r c28e07377b03 Lib/test/test_zipimport.py --- a/Lib/test/test_zipimport.py Tue Jan 21 21:12:13 2014 -0500 +++ b/Lib/test/test_zipimport.py Tue Jan 21 23:37:13 2014 -0800 @@ -395,10 +395,15 @@ def setUp(self): zipimport._zip_directory_cache.clear() zipimport._zip_stat_cache.clear() + # save sys.modules so we can unimport everything done by our tests. + self._sys_modules_orig = dict(sys.modules) ImportHooksBaseTestCase.setUp(self) def tearDown(self): ImportHooksBaseTestCase.tearDown(self) + # The closest we can come to un-importing our zipped up test modules. + sys.modules.clear() + sys.modules.update(self._sys_modules_orig) if os.path.exists(TEMP_ZIP): os.remove(TEMP_ZIP) @@ -415,13 +420,19 @@ self.assertTrue(os.path.exists(zipfile_path)) sys.path.insert(0, zipfile_path) + testpack_testmod = TESTPACK + "." + TESTMOD # Import something out of the zipfile and confirm it is correct. - testmod = __import__(TESTPACK + "." + TESTMOD, + testmod = __import__(testpack_testmod, globals(), locals(), ["__dummy__"]) self.assertEqual(testmod.test_value, 38) + del testmod + del sys.modules[TESTPACK] + del sys.modules[testpack_testmod] # Import something else out of the zipfile and confirm it is correct. ziptest_b = __import__("ziptest_b", globals(), locals(), ["test_value"]) self.assertEqual(ziptest_b.test_value, 42) + del ziptest_b + del sys.modules["ziptest_b"] # Truncate and fill the zip file with non-zip garbage. with io.open(zipfile_path, "rb") as orig_zip_file: @@ -433,6 +444,14 @@ with self.assertRaises(ImportError): ziptest_a = __import__("ziptest_a", globals(), locals(), ["test_value"]) + # The code path used by the __import__ call is different than + # that used by import statements. + with self.assertRaises(ImportError): + import ziptest_a + with self.assertRaises(ImportError): + from ziptest_a import test_value + with self.assertRaises(ImportError): + exec("from {} import {}".format(TESTPACK, TESTMOD)) # Now lets make it a valid zipfile that has some garbage at the start. # This alters all of the offsets within the file @@ -440,6 +459,12 @@ new_zip_file.write(b"X"*1991) # The year Python was created. new_zip_file.write(orig_zip_file_contents) + # importing a submodule triggers a different import code path. + exec("import " + testpack_testmod) + self.assertEqual(testpack.testmod.test_value, 38) + exec("from {} import {}".format(TESTPACK, TESTMOD)) + self.assertEqual(testmod.test_value, 38) + # Now that the zip file has been "restored" to a valid but different # zipfile the zipimporter should *successfully* re-read the new zip # file's end of file central index and be able to import from it again.