Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ec91cd6

Browse files
committed
merge backout for #20621
2 parents e686c5c + eb43736 commit ec91cd6

2 files changed

Lines changed: 67 additions & 321 deletions

File tree

Lib/test/test_zipimport.py

Lines changed: 19 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -71,27 +71,6 @@ def tearDown(self):
7171
support.modules_cleanup(*self.modules_before)
7272

7373

74-
def _write_zip_package(zipname, files,
75-
data_to_prepend=b"", compression=ZIP_STORED):
76-
z = ZipFile(zipname, "w")
77-
try:
78-
for name, (mtime, data) in files.items():
79-
zinfo = ZipInfo(name, time.localtime(mtime))
80-
zinfo.compress_type = compression
81-
z.writestr(zinfo, data)
82-
finally:
83-
z.close()
84-
85-
if data_to_prepend:
86-
# Prepend data to the start of the zipfile
87-
with open(zipname, "rb") as f:
88-
zip_data = f.read()
89-
90-
with open(zipname, "wb") as f:
91-
f.write(data_to_prepend)
92-
f.write(zip_data)
93-
94-
9574
class UncompressedZipImportTestCase(ImportHooksBaseTestCase):
9675

9776
compression = ZIP_STORED
@@ -104,9 +83,23 @@ def setUp(self):
10483
ImportHooksBaseTestCase.setUp(self)
10584

10685
def doTest(self, expected_ext, files, *modules, **kw):
107-
_write_zip_package(TEMP_ZIP, files, data_to_prepend=kw.get("stuff"),
108-
compression=self.compression)
86+
z = ZipFile(TEMP_ZIP, "w")
10987
try:
88+
for name, (mtime, data) in files.items():
89+
zinfo = ZipInfo(name, time.localtime(mtime))
90+
zinfo.compress_type = self.compression
91+
z.writestr(zinfo, data)
92+
z.close()
93+
94+
stuff = kw.get("stuff", None)
95+
if stuff is not None:
96+
# Prepend 'stuff' to the start of the zipfile
97+
with open(TEMP_ZIP, "rb") as f:
98+
data = f.read()
99+
with open(TEMP_ZIP, "wb") as f:
100+
f.write(stuff)
101+
f.write(data)
102+
110103
sys.path.insert(0, TEMP_ZIP)
111104

112105
mod = __import__(".".join(modules), globals(), locals(),
@@ -121,8 +114,7 @@ def doTest(self, expected_ext, files, *modules, **kw):
121114
self.assertEqual(file, os.path.join(TEMP_ZIP,
122115
*modules) + expected_ext)
123116
finally:
124-
while TEMP_ZIP in sys.path:
125-
sys.path.remove(TEMP_ZIP)
117+
z.close()
126118
os.remove(TEMP_ZIP)
127119

128120
def testAFakeZlib(self):
@@ -430,67 +422,10 @@ class CompressedZipImportTestCase(UncompressedZipImportTestCase):
430422
compression = ZIP_DEFLATED
431423

432424

433-
class ZipFileModifiedAfterImportTestCase(ImportHooksBaseTestCase):
434-
def setUp(self):
435-
zipimport._zip_directory_cache.clear()
436-
zipimport._zip_stat_cache.clear()
437-
ImportHooksBaseTestCase.setUp(self)
438-
439-
def tearDown(self):
440-
ImportHooksBaseTestCase.tearDown(self)
441-
if os.path.exists(TEMP_ZIP):
442-
os.remove(TEMP_ZIP)
443-
444-
def testZipFileChangesAfterFirstImport(self):
445-
"""Alter the zip file after caching its index and try an import."""
446-
packdir = TESTPACK + os.sep
447-
files = {packdir + "__init__" + pyc_ext: (NOW, test_pyc),
448-
packdir + TESTMOD + ".py": (NOW, "test_value = 38\n"),
449-
"ziptest_a.py": (NOW, "test_value = 23\n"),
450-
"ziptest_b.py": (NOW, "test_value = 42\n"),
451-
"ziptest_c.py": (NOW, "test_value = 1337\n")}
452-
zipfile_path = TEMP_ZIP
453-
_write_zip_package(zipfile_path, files)
454-
self.assertTrue(os.path.exists(zipfile_path))
455-
sys.path.insert(0, zipfile_path)
456-
457-
# Import something out of the zipfile and confirm it is correct.
458-
testmod = __import__(TESTPACK + "." + TESTMOD,
459-
globals(), locals(), ["__dummy__"])
460-
self.assertEqual(testmod.test_value, 38)
461-
# Import something else out of the zipfile and confirm it is correct.
462-
ziptest_b = __import__("ziptest_b", globals(), locals(), ["test_value"])
463-
self.assertEqual(ziptest_b.test_value, 42)
464-
465-
# Truncate and fill the zip file with non-zip garbage.
466-
with open(zipfile_path, "rb") as orig_zip_file:
467-
orig_zip_file_contents = orig_zip_file.read()
468-
with open(zipfile_path, "wb") as byebye_valid_zip_file:
469-
byebye_valid_zip_file.write(b"Tear down this wall!\n"*1987)
470-
# Now that the zipfile has been replaced, import something else from it
471-
# which should fail as the file contents are now garbage.
472-
with self.assertRaises(ImportError):
473-
ziptest_a = __import__("ziptest_a", globals(), locals(),
474-
["test_value"])
475-
self.assertEqual(ziptest_a.test_value, 23)
476-
477-
# Now lets make it a valid zipfile that has some garbage at the start.
478-
# This alters all of the offsets within the file
479-
with open(zipfile_path, "wb") as new_zip_file:
480-
new_zip_file.write(b"X"*1991) # The year Python was created.
481-
new_zip_file.write(orig_zip_file_contents)
482-
483-
# Now that the zip file has been "restored" to a valid but different
484-
# zipfile the zipimporter should *successfully* re-read the new zip
485-
# file's end of file central index and be able to import from it again.
486-
ziptest_c = __import__("ziptest_c", globals(), locals(), ["test_value"])
487-
self.assertEqual(ziptest_c.test_value, 1337)
488-
489-
490425
class BadFileZipImportTestCase(unittest.TestCase):
491426
def assertZipFailure(self, filename):
492-
with self.assertRaises(zipimport.ZipImportError):
493-
zipimport.zipimporter(filename)
427+
self.assertRaises(zipimport.ZipImportError,
428+
zipimport.zipimporter, filename)
494429

495430
def testNoFile(self):
496431
self.assertZipFailure('AdfjdkFJKDFJjdklfjs')
@@ -564,7 +499,6 @@ def test_main():
564499
UncompressedZipImportTestCase,
565500
CompressedZipImportTestCase,
566501
BadFileZipImportTestCase,
567-
ZipFileModifiedAfterImportTestCase,
568502
)
569503
finally:
570504
support.unlink(TESTMOD)

0 commit comments

Comments
 (0)