Changeset 391 for python/trunk/Lib/test/test_zipfile.py
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Lib/test/test_zipfile.py
r2 r391 5 5 zlib = None 6 6 7 import zipfile, os, unittest, sys, shutil, struct 7 import os 8 import io 9 import sys 10 import time 11 import shutil 12 import struct 13 import zipfile 14 import unittest 8 15 9 16 from StringIO import StringIO 10 17 from tempfile import TemporaryFile 11 18 from random import randint, random 12 13 import test.test_support as support 14 from test.test_support import TESTFN, run_unittest, findfile 19 from unittest import skipUnless 20 21 from test.test_support import TESTFN, TESTFN_UNICODE, TESTFN_ENCODING, \ 22 run_unittest, findfile, unlink 23 try: 24 TESTFN_UNICODE.encode(TESTFN_ENCODING) 25 except (UnicodeError, TypeError): 26 # Either the file system encoding is None, or the file name 27 # cannot be encoded in the file system encoding. 28 TESTFN_UNICODE = None 15 29 16 30 TESTFN2 = TESTFN + "2" … … 20 34 SMALL_TEST_DATA = [('_ziptest1', '1q2w3e4r5t'), 21 35 ('ziptest2dir/_ziptest2', 'qawsedrftg'), 22 (' /ziptest2dir/ziptest3dir/_ziptest3', 'azsxdcfvgb'),36 ('ziptest2dir/ziptest3dir/_ziptest3', 'azsxdcfvgb'), 23 37 ('ziptest2dir/ziptest3dir/ziptest4dir/_ziptest3', '6y7u8i9o0p')] 38 24 39 25 40 class TestsWithSourceFile(unittest.TestCase): 26 41 def setUp(self): 27 42 self.line_gen = ["Zipfile test line %d. random float: %f" % (i, random()) 28 43 for i in xrange(FIXEDTEST_SIZE)] 29 44 self.data = '\n'.join(self.line_gen) + '\n' 30 45 31 46 # Make a source file with some lines 32 fp = open(TESTFN, "wb") 33 fp.write(self.data) 34 fp.close() 35 36 def makeTestArchive(self, f, compression): 47 with open(TESTFN, "wb") as fp: 48 fp.write(self.data) 49 50 def make_test_archive(self, f, compression): 37 51 # Create the ZIP archive 38 zipfp = zipfile.ZipFile(f, "w", compression) 39 zipfp.write(TESTFN, "another"+os.extsep+"name") 40 zipfp.write(TESTFN, TESTFN) 41 zipfp.writestr("strfile", self.data) 42 zipfp.close() 43 44 def zipTest(self, f, compression): 45 self.makeTestArchive(f, compression) 52 with zipfile.ZipFile(f, "w", compression) as zipfp: 53 zipfp.write(TESTFN, "another.name") 54 zipfp.write(TESTFN, TESTFN) 55 zipfp.writestr("strfile", self.data) 56 57 def zip_test(self, f, compression): 58 self.make_test_archive(f, compression) 46 59 47 60 # Read the ZIP archive 48 zipfp = zipfile.ZipFile(f, "r", compression) 49 self.assertEqual(zipfp.read(TESTFN), self.data) 50 self.assertEqual(zipfp.read("another"+os.extsep+"name"), self.data) 51 self.assertEqual(zipfp.read("strfile"), self.data) 52 53 # Print the ZIP directory 54 fp = StringIO() 55 stdout = sys.stdout 56 try: 57 sys.stdout = fp 58 59 zipfp.printdir() 60 finally: 61 sys.stdout = stdout 62 63 directory = fp.getvalue() 64 lines = directory.splitlines() 65 self.assertEquals(len(lines), 4) # Number of files + header 66 67 self.assert_('File Name' in lines[0]) 68 self.assert_('Modified' in lines[0]) 69 self.assert_('Size' in lines[0]) 70 71 fn, date, time, size = lines[1].split() 72 self.assertEquals(fn, 'another.name') 73 # XXX: timestamp is not tested 74 self.assertEquals(size, str(len(self.data))) 75 76 # Check the namelist 77 names = zipfp.namelist() 78 self.assertEquals(len(names), 3) 79 self.assert_(TESTFN in names) 80 self.assert_("another"+os.extsep+"name" in names) 81 self.assert_("strfile" in names) 82 83 # Check infolist 84 infos = zipfp.infolist() 85 names = [ i.filename for i in infos ] 86 self.assertEquals(len(names), 3) 87 self.assert_(TESTFN in names) 88 self.assert_("another"+os.extsep+"name" in names) 89 self.assert_("strfile" in names) 90 for i in infos: 91 self.assertEquals(i.file_size, len(self.data)) 92 93 # check getinfo 94 for nm in (TESTFN, "another"+os.extsep+"name", "strfile"): 95 info = zipfp.getinfo(nm) 96 self.assertEquals(info.filename, nm) 97 self.assertEquals(info.file_size, len(self.data)) 98 99 # Check that testzip doesn't raise an exception 100 zipfp.testzip() 101 zipfp.close() 102 103 def testStored(self): 104 for f in (TESTFN2, TemporaryFile(), StringIO()): 105 self.zipTest(f, zipfile.ZIP_STORED) 106 107 def zipOpenTest(self, f, compression): 108 self.makeTestArchive(f, compression) 61 with zipfile.ZipFile(f, "r", compression) as zipfp: 62 self.assertEqual(zipfp.read(TESTFN), self.data) 63 self.assertEqual(zipfp.read("another.name"), self.data) 64 self.assertEqual(zipfp.read("strfile"), self.data) 65 66 # Print the ZIP directory 67 fp = StringIO() 68 stdout = sys.stdout 69 try: 70 sys.stdout = fp 71 zipfp.printdir() 72 finally: 73 sys.stdout = stdout 74 75 directory = fp.getvalue() 76 lines = directory.splitlines() 77 self.assertEqual(len(lines), 4) # Number of files + header 78 79 self.assertIn('File Name', lines[0]) 80 self.assertIn('Modified', lines[0]) 81 self.assertIn('Size', lines[0]) 82 83 fn, date, time_, size = lines[1].split() 84 self.assertEqual(fn, 'another.name') 85 self.assertTrue(time.strptime(date, '%Y-%m-%d')) 86 self.assertTrue(time.strptime(time_, '%H:%M:%S')) 87 self.assertEqual(size, str(len(self.data))) 88 89 # Check the namelist 90 names = zipfp.namelist() 91 self.assertEqual(len(names), 3) 92 self.assertIn(TESTFN, names) 93 self.assertIn("another.name", names) 94 self.assertIn("strfile", names) 95 96 # Check infolist 97 infos = zipfp.infolist() 98 names = [i.filename for i in infos] 99 self.assertEqual(len(names), 3) 100 self.assertIn(TESTFN, names) 101 self.assertIn("another.name", names) 102 self.assertIn("strfile", names) 103 for i in infos: 104 self.assertEqual(i.file_size, len(self.data)) 105 106 # check getinfo 107 for nm in (TESTFN, "another.name", "strfile"): 108 info = zipfp.getinfo(nm) 109 self.assertEqual(info.filename, nm) 110 self.assertEqual(info.file_size, len(self.data)) 111 112 # Check that testzip doesn't raise an exception 113 zipfp.testzip() 114 115 def test_stored(self): 116 for f in (TESTFN2, TemporaryFile(), StringIO()): 117 self.zip_test(f, zipfile.ZIP_STORED) 118 119 def zip_open_test(self, f, compression): 120 self.make_test_archive(f, compression) 109 121 110 122 # Read the ZIP archive 111 zipfp = zipfile.ZipFile(f, "r", compression) 112 zipdata1 = [] 113 zipopen1 = zipfp.open(TESTFN) 114 while 1: 115 read_data = zipopen1.read(256) 116 if not read_data: 117 break 118 zipdata1.append(read_data) 119 120 zipdata2 = [] 121 zipopen2 = zipfp.open("another"+os.extsep+"name") 122 while 1: 123 read_data = zipopen2.read(256) 124 if not read_data: 125 break 126 zipdata2.append(read_data) 127 128 self.assertEqual(''.join(zipdata1), self.data) 129 self.assertEqual(''.join(zipdata2), self.data) 130 zipfp.close() 131 132 def testOpenStored(self): 133 for f in (TESTFN2, TemporaryFile(), StringIO()): 134 self.zipOpenTest(f, zipfile.ZIP_STORED) 135 136 def testOpenViaZipInfo(self): 123 with zipfile.ZipFile(f, "r", compression) as zipfp: 124 zipdata1 = [] 125 with zipfp.open(TESTFN) as zipopen1: 126 while True: 127 read_data = zipopen1.read(256) 128 if not read_data: 129 break 130 zipdata1.append(read_data) 131 132 zipdata2 = [] 133 with zipfp.open("another.name") as zipopen2: 134 while True: 135 read_data = zipopen2.read(256) 136 if not read_data: 137 break 138 zipdata2.append(read_data) 139 140 self.assertEqual(''.join(zipdata1), self.data) 141 self.assertEqual(''.join(zipdata2), self.data) 142 143 def test_open_stored(self): 144 for f in (TESTFN2, TemporaryFile(), StringIO()): 145 self.zip_open_test(f, zipfile.ZIP_STORED) 146 147 def test_open_via_zip_info(self): 137 148 # Create the ZIP archive 138 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) 139 zipfp.writestr("name", "foo") 140 zipfp.writestr("name", "bar") 141 zipfp.close() 142 143 zipfp = zipfile.ZipFile(TESTFN2, "r") 144 infos = zipfp.infolist() 145 data = "" 146 for info in infos: 147 data += zipfp.open(info).read() 148 self.assert_(data == "foobar" or data == "barfoo") 149 data = "" 150 for info in infos: 151 data += zipfp.read(info) 152 self.assert_(data == "foobar" or data == "barfoo") 153 zipfp.close() 154 155 def zipRandomOpenTest(self, f, compression): 156 self.makeTestArchive(f, compression) 149 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: 150 zipfp.writestr("name", "foo") 151 zipfp.writestr("name", "bar") 152 153 with zipfile.ZipFile(TESTFN2, "r") as zipfp: 154 infos = zipfp.infolist() 155 data = "" 156 for info in infos: 157 with zipfp.open(info) as f: 158 data += f.read() 159 self.assertTrue(data == "foobar" or data == "barfoo") 160 data = "" 161 for info in infos: 162 data += zipfp.read(info) 163 self.assertTrue(data == "foobar" or data == "barfoo") 164 165 def zip_random_open_test(self, f, compression): 166 self.make_test_archive(f, compression) 157 167 158 168 # Read the ZIP archive 159 zipfp = zipfile.ZipFile(f, "r", compression) 160 zipdata1 = [] 161 zipopen1 = zipfp.open(TESTFN) 162 while 1: 163 read_data = zipopen1.read(randint(1, 1024)) 164 if not read_data: 165 break 166 zipdata1.append(read_data) 167 168 self.assertEqual(''.join(zipdata1), self.data) 169 zipfp.close() 170 171 def testRandomOpenStored(self): 172 for f in (TESTFN2, TemporaryFile(), StringIO()): 173 self.zipRandomOpenTest(f, zipfile.ZIP_STORED) 174 175 def zipReadlineTest(self, f, compression): 176 self.makeTestArchive(f, compression) 169 with zipfile.ZipFile(f, "r", compression) as zipfp: 170 zipdata1 = [] 171 with zipfp.open(TESTFN) as zipopen1: 172 while True: 173 read_data = zipopen1.read(randint(1, 1024)) 174 if not read_data: 175 break 176 zipdata1.append(read_data) 177 178 self.assertEqual(''.join(zipdata1), self.data) 179 180 def test_random_open_stored(self): 181 for f in (TESTFN2, TemporaryFile(), StringIO()): 182 self.zip_random_open_test(f, zipfile.ZIP_STORED) 183 184 def test_univeral_readaheads(self): 185 f = StringIO() 186 187 data = 'a\r\n' * 16 * 1024 188 with zipfile.ZipFile(f, 'w', zipfile.ZIP_STORED) as zipfp: 189 zipfp.writestr(TESTFN, data) 190 191 data2 = '' 192 with zipfile.ZipFile(f, 'r') as zipfp: 193 with zipfp.open(TESTFN, 'rU') as zipopen: 194 for line in zipopen: 195 data2 += line 196 197 self.assertEqual(data, data2.replace('\n', '\r\n')) 198 199 def zip_readline_read_test(self, f, compression): 200 self.make_test_archive(f, compression) 177 201 178 202 # Read the ZIP archive 179 zipfp = zipfile.ZipFile(f, "r") 180 zipopen = zipfp.open(TESTFN) 181 for line in self.line_gen: 182 linedata = zipopen.readline() 183 self.assertEqual(linedata, line + '\n') 184 185 zipfp.close() 186 187 def zipReadlinesTest(self, f, compression): 188 self.makeTestArchive(f, compression) 203 with zipfile.ZipFile(f, "r") as zipfp: 204 with zipfp.open(TESTFN) as zipopen: 205 data = '' 206 while True: 207 read = zipopen.readline() 208 if not read: 209 break 210 data += read 211 212 read = zipopen.read(100) 213 if not read: 214 break 215 data += read 216 217 self.assertEqual(data, self.data) 218 219 def zip_readline_test(self, f, compression): 220 self.make_test_archive(f, compression) 189 221 190 222 # Read the ZIP archive 191 zipfp = zipfile.ZipFile(f, "r") 192 ziplines = zipfp.open(TESTFN).readlines() 193 for line, zipline in zip(self.line_gen, ziplines): 194 self.assertEqual(zipline, line + '\n') 195 196 zipfp.close() 197 198 def zipIterlinesTest(self, f, compression): 199 self.makeTestArchive(f, compression) 223 with zipfile.ZipFile(f, "r") as zipfp: 224 with zipfp.open(TESTFN) as zipopen: 225 for line in self.line_gen: 226 linedata = zipopen.readline() 227 self.assertEqual(linedata, line + '\n') 228 229 def zip_readlines_test(self, f, compression): 230 self.make_test_archive(f, compression) 200 231 201 232 # Read the ZIP archive 202 zipfp = zipfile.ZipFile(f, "r") 203 for line, zipline in zip(self.line_gen, zipfp.open(TESTFN)): 204 self.assertEqual(zipline, line + '\n') 205 206 zipfp.close() 207 208 def testReadlineStored(self): 209 for f in (TESTFN2, TemporaryFile(), StringIO()): 210 self.zipReadlineTest(f, zipfile.ZIP_STORED) 211 212 def testReadlinesStored(self): 213 for f in (TESTFN2, TemporaryFile(), StringIO()): 214 self.zipReadlinesTest(f, zipfile.ZIP_STORED) 215 216 def testIterlinesStored(self): 217 for f in (TESTFN2, TemporaryFile(), StringIO()): 218 self.zipIterlinesTest(f, zipfile.ZIP_STORED) 219 220 if zlib: 221 def testDeflated(self): 222 for f in (TESTFN2, TemporaryFile(), StringIO()): 223 self.zipTest(f, zipfile.ZIP_DEFLATED) 224 225 def testOpenDeflated(self): 226 for f in (TESTFN2, TemporaryFile(), StringIO()): 227 self.zipOpenTest(f, zipfile.ZIP_DEFLATED) 228 229 def testRandomOpenDeflated(self): 230 for f in (TESTFN2, TemporaryFile(), StringIO()): 231 self.zipRandomOpenTest(f, zipfile.ZIP_DEFLATED) 232 233 def testReadlineDeflated(self): 234 for f in (TESTFN2, TemporaryFile(), StringIO()): 235 self.zipReadlineTest(f, zipfile.ZIP_DEFLATED) 236 237 def testReadlinesDeflated(self): 238 for f in (TESTFN2, TemporaryFile(), StringIO()): 239 self.zipReadlinesTest(f, zipfile.ZIP_DEFLATED) 240 241 def testIterlinesDeflated(self): 242 for f in (TESTFN2, TemporaryFile(), StringIO()): 243 self.zipIterlinesTest(f, zipfile.ZIP_DEFLATED) 244 245 def testLowCompression(self): 246 # Checks for cases where compressed data is larger than original 247 # Create the ZIP archive 248 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED) 233 with zipfile.ZipFile(f, "r") as zipfp: 234 with zipfp.open(TESTFN) as zo: 235 ziplines = zo.readlines() 236 for line, zipline in zip(self.line_gen, ziplines): 237 self.assertEqual(zipline, line + '\n') 238 239 def zip_iterlines_test(self, f, compression): 240 self.make_test_archive(f, compression) 241 242 # Read the ZIP archive 243 with zipfile.ZipFile(f, "r") as zipfp: 244 for line, zipline in zip(self.line_gen, zipfp.open(TESTFN)): 245 self.assertEqual(zipline, line + '\n') 246 247 def test_readline_read_stored(self): 248 # Issue #7610: calls to readline() interleaved with calls to read(). 249 for f in (TESTFN2, TemporaryFile(), StringIO()): 250 self.zip_readline_read_test(f, zipfile.ZIP_STORED) 251 252 def test_readline_stored(self): 253 for f in (TESTFN2, TemporaryFile(), StringIO()): 254 self.zip_readline_test(f, zipfile.ZIP_STORED) 255 256 def test_readlines_stored(self): 257 for f in (TESTFN2, TemporaryFile(), StringIO()): 258 self.zip_readlines_test(f, zipfile.ZIP_STORED) 259 260 def test_iterlines_stored(self): 261 for f in (TESTFN2, TemporaryFile(), StringIO()): 262 self.zip_iterlines_test(f, zipfile.ZIP_STORED) 263 264 @skipUnless(zlib, "requires zlib") 265 def test_deflated(self): 266 for f in (TESTFN2, TemporaryFile(), StringIO()): 267 self.zip_test(f, zipfile.ZIP_DEFLATED) 268 269 @skipUnless(zlib, "requires zlib") 270 def test_open_deflated(self): 271 for f in (TESTFN2, TemporaryFile(), StringIO()): 272 self.zip_open_test(f, zipfile.ZIP_DEFLATED) 273 274 @skipUnless(zlib, "requires zlib") 275 def test_random_open_deflated(self): 276 for f in (TESTFN2, TemporaryFile(), StringIO()): 277 self.zip_random_open_test(f, zipfile.ZIP_DEFLATED) 278 279 @skipUnless(zlib, "requires zlib") 280 def test_readline_read_deflated(self): 281 # Issue #7610: calls to readline() interleaved with calls to read(). 282 for f in (TESTFN2, TemporaryFile(), StringIO()): 283 self.zip_readline_read_test(f, zipfile.ZIP_DEFLATED) 284 285 @skipUnless(zlib, "requires zlib") 286 def test_readline_deflated(self): 287 for f in (TESTFN2, TemporaryFile(), StringIO()): 288 self.zip_readline_test(f, zipfile.ZIP_DEFLATED) 289 290 @skipUnless(zlib, "requires zlib") 291 def test_readlines_deflated(self): 292 for f in (TESTFN2, TemporaryFile(), StringIO()): 293 self.zip_readlines_test(f, zipfile.ZIP_DEFLATED) 294 295 @skipUnless(zlib, "requires zlib") 296 def test_iterlines_deflated(self): 297 for f in (TESTFN2, TemporaryFile(), StringIO()): 298 self.zip_iterlines_test(f, zipfile.ZIP_DEFLATED) 299 300 @skipUnless(zlib, "requires zlib") 301 def test_low_compression(self): 302 """Check for cases where compressed data is larger than original.""" 303 # Create the ZIP archive 304 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED) as zipfp: 249 305 zipfp.writestr("strfile", '12') 250 zipfp.close() 251 252 # Get an open object for strfile 253 zipfp = zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_DEFLATED) 254 openobj = zipfp.open("strfile") 255 self.assertEqual(openobj.read(1), '1') 256 self.assertEqual(openobj.read(1), '2') 257 258 def testAbsoluteArcnames(self): 259 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) 260 zipfp.write(TESTFN, "/absolute") 261 zipfp.close() 262 263 zipfp = zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) 264 self.assertEqual(zipfp.namelist(), ["absolute"]) 265 zipfp.close() 266 267 def testAppendToZipFile(self): 268 # Test appending to an existing zipfile 269 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) 270 zipfp.write(TESTFN, TESTFN) 271 zipfp.close() 272 zipfp = zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) 273 zipfp.writestr("strfile", self.data) 274 self.assertEqual(zipfp.namelist(), [TESTFN, "strfile"]) 275 zipfp.close() 276 277 def testAppendToNonZipFile(self): 278 # Test appending to an existing file that is not a zipfile 306 307 # Get an open object for strfile 308 with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_DEFLATED) as zipfp: 309 with zipfp.open("strfile") as openobj: 310 self.assertEqual(openobj.read(1), '1') 311 self.assertEqual(openobj.read(1), '2') 312 313 def test_absolute_arcnames(self): 314 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: 315 zipfp.write(TESTFN, "/absolute") 316 317 with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) as zipfp: 318 self.assertEqual(zipfp.namelist(), ["absolute"]) 319 320 def test_append_to_zip_file(self): 321 """Test appending to an existing zipfile.""" 322 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: 323 zipfp.write(TESTFN, TESTFN) 324 325 with zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) as zipfp: 326 zipfp.writestr("strfile", self.data) 327 self.assertEqual(zipfp.namelist(), [TESTFN, "strfile"]) 328 329 def test_append_to_non_zip_file(self): 330 """Test appending to an existing file that is not a zipfile.""" 279 331 # NOTE: this test fails if len(d) < 22 because of the first 280 332 # line "fpin.seek(-22, 2)" in _EndRecData 281 d = 'I am not a ZipFile!'*10 282 f = file(TESTFN2, 'wb') 283 f.write(d) 284 f.close() 285 zipfp = zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) 286 zipfp.write(TESTFN, TESTFN) 287 zipfp.close() 288 289 f = file(TESTFN2, 'rb') 290 f.seek(len(d)) 291 zipfp = zipfile.ZipFile(f, "r") 292 self.assertEqual(zipfp.namelist(), [TESTFN]) 293 zipfp.close() 294 f.close() 295 296 def test_WriteDefaultName(self): 297 # Check that calling ZipFile.write without arcname specified produces the expected result 298 zipfp = zipfile.ZipFile(TESTFN2, "w") 299 zipfp.write(TESTFN) 300 self.assertEqual(zipfp.read(TESTFN), file(TESTFN).read()) 301 zipfp.close() 302 303 def test_PerFileCompression(self): 304 # Check that files within a Zip archive can have different compression options 305 zipfp = zipfile.ZipFile(TESTFN2, "w") 306 zipfp.write(TESTFN, 'storeme', zipfile.ZIP_STORED) 307 zipfp.write(TESTFN, 'deflateme', zipfile.ZIP_DEFLATED) 308 sinfo = zipfp.getinfo('storeme') 309 dinfo = zipfp.getinfo('deflateme') 310 self.assertEqual(sinfo.compress_type, zipfile.ZIP_STORED) 311 self.assertEqual(dinfo.compress_type, zipfile.ZIP_DEFLATED) 312 zipfp.close() 313 314 def test_WriteToReadonly(self): 315 # Check that trying to call write() on a readonly ZipFile object 316 # raises a RuntimeError 317 zipf = zipfile.ZipFile(TESTFN2, mode="w") 318 zipf.writestr("somefile.txt", "bogus") 319 zipf.close() 320 zipf = zipfile.ZipFile(TESTFN2, mode="r") 321 self.assertRaises(RuntimeError, zipf.write, TESTFN) 322 zipf.close() 323 324 def testExtract(self): 325 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) 326 for fpath, fdata in SMALL_TEST_DATA: 327 zipfp.writestr(fpath, fdata) 328 zipfp.close() 329 330 zipfp = zipfile.ZipFile(TESTFN2, "r") 331 for fpath, fdata in SMALL_TEST_DATA: 332 writtenfile = zipfp.extract(fpath) 333 334 # make sure it was written to the right place 335 if os.path.isabs(fpath): 336 correctfile = os.path.join(os.getcwd(), fpath[1:]) 337 else: 333 data = 'I am not a ZipFile!'*10 334 with open(TESTFN2, 'wb') as f: 335 f.write(data) 336 337 with zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) as zipfp: 338 zipfp.write(TESTFN, TESTFN) 339 340 with open(TESTFN2, 'rb') as f: 341 f.seek(len(data)) 342 with zipfile.ZipFile(f, "r") as zipfp: 343 self.assertEqual(zipfp.namelist(), [TESTFN]) 344 345 def test_ignores_newline_at_end(self): 346 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: 347 zipfp.write(TESTFN, TESTFN) 348 with open(TESTFN2, 'a') as f: 349 f.write("\r\n\00\00\00") 350 with zipfile.ZipFile(TESTFN2, "r") as zipfp: 351 self.assertIsInstance(zipfp, zipfile.ZipFile) 352 353 def test_ignores_stuff_appended_past_comments(self): 354 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: 355 zipfp.comment = b"this is a comment" 356 zipfp.write(TESTFN, TESTFN) 357 with open(TESTFN2, 'a') as f: 358 f.write("abcdef\r\n") 359 with zipfile.ZipFile(TESTFN2, "r") as zipfp: 360 self.assertIsInstance(zipfp, zipfile.ZipFile) 361 self.assertEqual(zipfp.comment, b"this is a comment") 362 363 def test_write_default_name(self): 364 """Check that calling ZipFile.write without arcname specified 365 produces the expected result.""" 366 with zipfile.ZipFile(TESTFN2, "w") as zipfp: 367 zipfp.write(TESTFN) 368 self.assertEqual(zipfp.read(TESTFN), open(TESTFN).read()) 369 370 @skipUnless(zlib, "requires zlib") 371 def test_per_file_compression(self): 372 """Check that files within a Zip archive can have different 373 compression options.""" 374 with zipfile.ZipFile(TESTFN2, "w") as zipfp: 375 zipfp.write(TESTFN, 'storeme', zipfile.ZIP_STORED) 376 zipfp.write(TESTFN, 'deflateme', zipfile.ZIP_DEFLATED) 377 sinfo = zipfp.getinfo('storeme') 378 dinfo = zipfp.getinfo('deflateme') 379 self.assertEqual(sinfo.compress_type, zipfile.ZIP_STORED) 380 self.assertEqual(dinfo.compress_type, zipfile.ZIP_DEFLATED) 381 382 def test_write_to_readonly(self): 383 """Check that trying to call write() on a readonly ZipFile object 384 raises a RuntimeError.""" 385 with zipfile.ZipFile(TESTFN2, mode="w") as zipfp: 386 zipfp.writestr("somefile.txt", "bogus") 387 388 with zipfile.ZipFile(TESTFN2, mode="r") as zipfp: 389 self.assertRaises(RuntimeError, zipfp.write, TESTFN) 390 391 def test_extract(self): 392 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: 393 for fpath, fdata in SMALL_TEST_DATA: 394 zipfp.writestr(fpath, fdata) 395 396 with zipfile.ZipFile(TESTFN2, "r") as zipfp: 397 for fpath, fdata in SMALL_TEST_DATA: 398 writtenfile = zipfp.extract(fpath) 399 400 # make sure it was written to the right place 338 401 correctfile = os.path.join(os.getcwd(), fpath) 339 correctfile = os.path.normpath(correctfile) 340 341 self.assertEqual(writtenfile, correctfile) 342 343 # make sure correct data is in correct file 344 self.assertEqual(fdata, file(writtenfile, "rb").read()) 345 346 os.remove(writtenfile) 347 348 zipfp.close() 402 correctfile = os.path.normpath(correctfile) 403 404 self.assertEqual(writtenfile, correctfile) 405 406 # make sure correct data is in correct file 407 self.assertEqual(fdata, open(writtenfile, "rb").read()) 408 os.remove(writtenfile) 349 409 350 410 # remove the test file subdirectories 351 411 shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir')) 352 412 353 def testExtractAll(self): 354 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) 355 for fpath, fdata in SMALL_TEST_DATA: 356 zipfp.writestr(fpath, fdata) 357 zipfp.close() 358 359 zipfp = zipfile.ZipFile(TESTFN2, "r") 360 zipfp.extractall() 361 for fpath, fdata in SMALL_TEST_DATA: 362 if os.path.isabs(fpath): 363 outfile = os.path.join(os.getcwd(), fpath[1:]) 364 else: 413 def test_extract_all(self): 414 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: 415 for fpath, fdata in SMALL_TEST_DATA: 416 zipfp.writestr(fpath, fdata) 417 418 with zipfile.ZipFile(TESTFN2, "r") as zipfp: 419 zipfp.extractall() 420 for fpath, fdata in SMALL_TEST_DATA: 365 421 outfile = os.path.join(os.getcwd(), fpath) 366 422 367 self.assertEqual(fdata, file(outfile, "rb").read()) 368 369 os.remove(outfile) 370 371 zipfp.close() 423 self.assertEqual(fdata, open(outfile, "rb").read()) 424 os.remove(outfile) 372 425 373 426 # remove the test file subdirectories 374 427 shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir')) 428 429 def check_file(self, filename, content): 430 self.assertTrue(os.path.isfile(filename)) 431 with open(filename, 'rb') as f: 432 self.assertEqual(f.read(), content) 433 434 @skipUnless(TESTFN_UNICODE, "No Unicode filesystem semantics on this platform.") 435 def test_extract_unicode_filenames(self): 436 fnames = [u'foo.txt', os.path.basename(TESTFN_UNICODE)] 437 content = 'Test for unicode filename' 438 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp: 439 for fname in fnames: 440 zipfp.writestr(fname, content) 441 442 with zipfile.ZipFile(TESTFN2, "r") as zipfp: 443 for fname in fnames: 444 writtenfile = zipfp.extract(fname) 445 446 # make sure it was written to the right place 447 correctfile = os.path.join(os.getcwd(), fname) 448 correctfile = os.path.normpath(correctfile) 449 self.assertEqual(writtenfile, correctfile) 450 451 self.check_file(writtenfile, content) 452 os.remove(writtenfile) 453 454 def test_extract_hackers_arcnames(self): 455 hacknames = [ 456 ('../foo/bar', 'foo/bar'), 457 ('foo/../bar', 'foo/bar'), 458 ('foo/../../bar', 'foo/bar'), 459 ('foo/bar/..', 'foo/bar'), 460 ('./../foo/bar', 'foo/bar'), 461 ('/foo/bar', 'foo/bar'), 462 ('/foo/../bar', 'foo/bar'), 463 ('/foo/../../bar', 'foo/bar'), 464 ] 465 if os.path.sep == '\\': 466 hacknames.extend([ 467 (r'..\foo\bar', 'foo/bar'), 468 (r'..\/foo\/bar', 'foo/bar'), 469 (r'foo/\..\/bar', 'foo/bar'), 470 (r'foo\/../\bar', 'foo/bar'), 471 (r'C:foo/bar', 'foo/bar'), 472 (r'C:/foo/bar', 'foo/bar'), 473 (r'C://foo/bar', 'foo/bar'), 474 (r'C:\foo\bar', 'foo/bar'), 475 (r'//conky/mountpoint/foo/bar', 'conky/mountpoint/foo/bar'), 476 (r'\\conky\mountpoint\foo\bar', 'conky/mountpoint/foo/bar'), 477 (r'///conky/mountpoint/foo/bar', 'conky/mountpoint/foo/bar'), 478 (r'\\\conky\mountpoint\foo\bar', 'conky/mountpoint/foo/bar'), 479 (r'//conky//mountpoint/foo/bar', 'conky/mountpoint/foo/bar'), 480 (r'\\conky\\mountpoint\foo\bar', 'conky/mountpoint/foo/bar'), 481 (r'//?/C:/foo/bar', '_/C_/foo/bar'), 482 (r'\\?\C:\foo\bar', '_/C_/foo/bar'), 483 (r'C:/../C:/foo/bar', 'C_/foo/bar'), 484 (r'a:b\c<d>e|f"g?h*i', 'b/c_d_e_f_g_h_i'), 485 ('../../foo../../ba..r', 'foo/ba..r'), 486 ]) 487 else: # Unix 488 hacknames.extend([ 489 ('//foo/bar', 'foo/bar'), 490 ('../../foo../../ba..r', 'foo../ba..r'), 491 (r'foo/..\bar', r'foo/..\bar'), 492 ]) 493 494 for arcname, fixedname in hacknames: 495 content = b'foobar' + arcname.encode() 496 with zipfile.ZipFile(TESTFN2, 'w', zipfile.ZIP_STORED) as zipfp: 497 zinfo = zipfile.ZipInfo() 498 # preserve backslashes 499 zinfo.filename = arcname 500 zinfo.external_attr = 0o600 << 16 501 zipfp.writestr(zinfo, content) 502 503 arcname = arcname.replace(os.sep, "/") 504 targetpath = os.path.join('target', 'subdir', 'subsub') 505 correctfile = os.path.join(targetpath, *fixedname.split('/')) 506 507 with zipfile.ZipFile(TESTFN2, 'r') as zipfp: 508 writtenfile = zipfp.extract(arcname, targetpath) 509 self.assertEqual(writtenfile, correctfile, 510 msg="extract %r" % arcname) 511 self.check_file(correctfile, content) 512 shutil.rmtree('target') 513 514 with zipfile.ZipFile(TESTFN2, 'r') as zipfp: 515 zipfp.extractall(targetpath) 516 self.check_file(correctfile, content) 517 shutil.rmtree('target') 518 519 correctfile = os.path.join(os.getcwd(), *fixedname.split('/')) 520 521 with zipfile.ZipFile(TESTFN2, 'r') as zipfp: 522 writtenfile = zipfp.extract(arcname) 523 self.assertEqual(writtenfile, correctfile, 524 msg="extract %r" % arcname) 525 self.check_file(correctfile, content) 526 shutil.rmtree(fixedname.split('/')[0]) 527 528 with zipfile.ZipFile(TESTFN2, 'r') as zipfp: 529 zipfp.extractall() 530 self.check_file(correctfile, content) 531 shutil.rmtree(fixedname.split('/')[0]) 532 533 os.remove(TESTFN2) 534 535 def test_writestr_compression(self): 536 zipfp = zipfile.ZipFile(TESTFN2, "w") 537 zipfp.writestr("a.txt", "hello world", compress_type=zipfile.ZIP_STORED) 538 if zlib: 539 zipfp.writestr("b.txt", "hello world", compress_type=zipfile.ZIP_DEFLATED) 540 541 info = zipfp.getinfo('a.txt') 542 self.assertEqual(info.compress_type, zipfile.ZIP_STORED) 543 544 if zlib: 545 info = zipfp.getinfo('b.txt') 546 self.assertEqual(info.compress_type, zipfile.ZIP_DEFLATED) 547 375 548 376 549 def zip_test_writestr_permissions(self, f, compression): … … 378 551 # when it is passed a name rather than a ZipInfo instance. 379 552 380 self.make TestArchive(f, compression)381 zipfp = zipfile.ZipFile(f, "r")382 zinfo = zipfp.getinfo('strfile')383 self.assertEqual(zinfo.external_attr, 0600 << 16)553 self.make_test_archive(f, compression) 554 with zipfile.ZipFile(f, "r") as zipfp: 555 zinfo = zipfp.getinfo('strfile') 556 self.assertEqual(zinfo.external_attr, 0600 << 16) 384 557 385 558 def test_writestr_permissions(self): … … 387 560 self.zip_test_writestr_permissions(f, zipfile.ZIP_STORED) 388 561 562 def test_close(self): 563 """Check that the zipfile is closed after the 'with' block.""" 564 with zipfile.ZipFile(TESTFN2, "w") as zipfp: 565 for fpath, fdata in SMALL_TEST_DATA: 566 zipfp.writestr(fpath, fdata) 567 self.assertTrue(zipfp.fp is not None, 'zipfp is not open') 568 self.assertTrue(zipfp.fp is None, 'zipfp is not closed') 569 570 with zipfile.ZipFile(TESTFN2, "r") as zipfp: 571 self.assertTrue(zipfp.fp is not None, 'zipfp is not open') 572 self.assertTrue(zipfp.fp is None, 'zipfp is not closed') 573 574 def test_close_on_exception(self): 575 """Check that the zipfile is closed if an exception is raised in the 576 'with' block.""" 577 with zipfile.ZipFile(TESTFN2, "w") as zipfp: 578 for fpath, fdata in SMALL_TEST_DATA: 579 zipfp.writestr(fpath, fdata) 580 581 try: 582 with zipfile.ZipFile(TESTFN2, "r") as zipfp2: 583 raise zipfile.BadZipfile() 584 except zipfile.BadZipfile: 585 self.assertTrue(zipfp2.fp is None, 'zipfp is not closed') 586 587 def test_add_file_before_1980(self): 588 # Set atime and mtime to 1970-01-01 589 os.utime(TESTFN, (0, 0)) 590 with zipfile.ZipFile(TESTFN2, "w") as zipfp: 591 self.assertRaises(ValueError, zipfp.write, TESTFN) 592 389 593 def tearDown(self): 390 os.remove(TESTFN) 391 os.remove(TESTFN2) 594 unlink(TESTFN) 595 unlink(TESTFN2) 596 392 597 393 598 class TestZip64InSmallFiles(unittest.TestCase): … … 399 604 zipfile.ZIP64_LIMIT = 5 400 605 401 line_gen = ("Test of zipfile line %d." % i for i in range(0, FIXEDTEST_SIZE)) 606 line_gen = ("Test of zipfile line %d." % i 607 for i in range(0, FIXEDTEST_SIZE)) 402 608 self.data = '\n'.join(line_gen) 403 609 404 610 # Make a source file with some lines 405 fp = open(TESTFN, "wb") 406 fp.write(self.data) 407 fp.close() 408 409 def largeFileExceptionTest(self, f, compression): 410 zipfp = zipfile.ZipFile(f, "w", compression) 411 self.assertRaises(zipfile.LargeZipFile, 412 zipfp.write, TESTFN, "another"+os.extsep+"name") 413 zipfp.close() 414 415 def largeFileExceptionTest2(self, f, compression): 416 zipfp = zipfile.ZipFile(f, "w", compression) 417 self.assertRaises(zipfile.LargeZipFile, 418 zipfp.writestr, "another"+os.extsep+"name", self.data) 419 zipfp.close() 420 421 def testLargeFileException(self): 422 for f in (TESTFN2, TemporaryFile(), StringIO()): 423 self.largeFileExceptionTest(f, zipfile.ZIP_STORED) 424 self.largeFileExceptionTest2(f, zipfile.ZIP_STORED) 425 426 def zipTest(self, f, compression): 611 with open(TESTFN, "wb") as fp: 612 fp.write(self.data) 613 614 def large_file_exception_test(self, f, compression): 615 with zipfile.ZipFile(f, "w", compression) as zipfp: 616 self.assertRaises(zipfile.LargeZipFile, 617 zipfp.write, TESTFN, "another.name") 618 619 def large_file_exception_test2(self, f, compression): 620 with zipfile.ZipFile(f, "w", compression) as zipfp: 621 self.assertRaises(zipfile.LargeZipFile, 622 zipfp.writestr, "another.name", self.data) 623 624 def test_large_file_exception(self): 625 for f in (TESTFN2, TemporaryFile(), StringIO()): 626 self.large_file_exception_test(f, zipfile.ZIP_STORED) 627 self.large_file_exception_test2(f, zipfile.ZIP_STORED) 628 629 def zip_test(self, f, compression): 427 630 # Create the ZIP archive 428 zipfp = zipfile.ZipFile(f, "w", compression, allowZip64=True) 429 zipfp.write(TESTFN, "another"+os.extsep+"name") 430 zipfp.write(TESTFN, TESTFN) 431 zipfp.writestr("strfile", self.data) 432 zipfp.close() 631 with zipfile.ZipFile(f, "w", compression, allowZip64=True) as zipfp: 632 zipfp.write(TESTFN, "another.name") 633 zipfp.write(TESTFN, TESTFN) 634 zipfp.writestr("strfile", self.data) 433 635 434 636 # Read the ZIP archive 435 zipfp = zipfile.ZipFile(f, "r", compression) 436 self.assertEqual(zipfp.read(TESTFN), self.data) 437 self.assertEqual(zipfp.read("another"+os.extsep+"name"), self.data) 438 self.assertEqual(zipfp.read("strfile"), self.data) 439 440 # Print the ZIP directory 441 fp = StringIO() 442 stdout = sys.stdout 443 try: 444 sys.stdout = fp 445 446 zipfp.printdir() 447 finally: 448 sys.stdout = stdout 449 450 directory = fp.getvalue() 451 lines = directory.splitlines() 452 self.assertEquals(len(lines), 4) # Number of files + header 453 454 self.assert_('File Name' in lines[0]) 455 self.assert_('Modified' in lines[0]) 456 self.assert_('Size' in lines[0]) 457 458 fn, date, time, size = lines[1].split() 459 self.assertEquals(fn, 'another.name') 460 # XXX: timestamp is not tested 461 self.assertEquals(size, str(len(self.data))) 462 463 # Check the namelist 464 names = zipfp.namelist() 465 self.assertEquals(len(names), 3) 466 self.assert_(TESTFN in names) 467 self.assert_("another"+os.extsep+"name" in names) 468 self.assert_("strfile" in names) 469 470 # Check infolist 471 infos = zipfp.infolist() 472 names = [ i.filename for i in infos ] 473 self.assertEquals(len(names), 3) 474 self.assert_(TESTFN in names) 475 self.assert_("another"+os.extsep+"name" in names) 476 self.assert_("strfile" in names) 477 for i in infos: 478 self.assertEquals(i.file_size, len(self.data)) 479 480 # check getinfo 481 for nm in (TESTFN, "another"+os.extsep+"name", "strfile"): 482 info = zipfp.getinfo(nm) 483 self.assertEquals(info.filename, nm) 484 self.assertEquals(info.file_size, len(self.data)) 485 486 # Check that testzip doesn't raise an exception 487 zipfp.testzip() 488 489 490 zipfp.close() 491 492 def testStored(self): 493 for f in (TESTFN2, TemporaryFile(), StringIO()): 494 self.zipTest(f, zipfile.ZIP_STORED) 495 496 497 if zlib: 498 def testDeflated(self): 499 for f in (TESTFN2, TemporaryFile(), StringIO()): 500 self.zipTest(f, zipfile.ZIP_DEFLATED) 501 502 def testAbsoluteArcnames(self): 503 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED, allowZip64=True) 504 zipfp.write(TESTFN, "/absolute") 505 zipfp.close() 506 507 zipfp = zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) 508 self.assertEqual(zipfp.namelist(), ["absolute"]) 509 zipfp.close() 637 with zipfile.ZipFile(f, "r", compression) as zipfp: 638 self.assertEqual(zipfp.read(TESTFN), self.data) 639 self.assertEqual(zipfp.read("another.name"), self.data) 640 self.assertEqual(zipfp.read("strfile"), self.data) 641 642 # Print the ZIP directory 643 fp = StringIO() 644 stdout = sys.stdout 645 try: 646 sys.stdout = fp 647 zipfp.printdir() 648 finally: 649 sys.stdout = stdout 650 651 directory = fp.getvalue() 652 lines = directory.splitlines() 653 self.assertEqual(len(lines), 4) # Number of files + header 654 655 self.assertIn('File Name', lines[0]) 656 self.assertIn('Modified', lines[0]) 657 self.assertIn('Size', lines[0]) 658 659 fn, date, time_, size = lines[1].split() 660 self.assertEqual(fn, 'another.name') 661 self.assertTrue(time.strptime(date, '%Y-%m-%d')) 662 self.assertTrue(time.strptime(time_, '%H:%M:%S')) 663 self.assertEqual(size, str(len(self.data))) 664 665 # Check the namelist 666 names = zipfp.namelist() 667 self.assertEqual(len(names), 3) 668 self.assertIn(TESTFN, names) 669 self.assertIn("another.name", names) 670 self.assertIn("strfile", names) 671 672 # Check infolist 673 infos = zipfp.infolist() 674 names = [i.filename for i in infos] 675 self.assertEqual(len(names), 3) 676 self.assertIn(TESTFN, names) 677 self.assertIn("another.name", names) 678 self.assertIn("strfile", names) 679 for i in infos: 680 self.assertEqual(i.file_size, len(self.data)) 681 682 # check getinfo 683 for nm in (TESTFN, "another.name", "strfile"): 684 info = zipfp.getinfo(nm) 685 self.assertEqual(info.filename, nm) 686 self.assertEqual(info.file_size, len(self.data)) 687 688 # Check that testzip doesn't raise an exception 689 zipfp.testzip() 690 691 def test_stored(self): 692 for f in (TESTFN2, TemporaryFile(), StringIO()): 693 self.zip_test(f, zipfile.ZIP_STORED) 694 695 @skipUnless(zlib, "requires zlib") 696 def test_deflated(self): 697 for f in (TESTFN2, TemporaryFile(), StringIO()): 698 self.zip_test(f, zipfile.ZIP_DEFLATED) 699 700 def test_absolute_arcnames(self): 701 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED, 702 allowZip64=True) as zipfp: 703 zipfp.write(TESTFN, "/absolute") 704 705 with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) as zipfp: 706 self.assertEqual(zipfp.namelist(), ["absolute"]) 510 707 511 708 def tearDown(self): 512 709 zipfile.ZIP64_LIMIT = self._limit 513 os.remove(TESTFN) 514 os.remove(TESTFN2) 710 unlink(TESTFN) 711 unlink(TESTFN2) 712 515 713 516 714 class PyZipFileTests(unittest.TestCase): 517 def testWritePyfile(self): 518 zipfp = zipfile.PyZipFile(TemporaryFile(), "w") 519 fn = __file__ 520 if fn.endswith('.pyc') or fn.endswith('.pyo'): 521 fn = fn[:-1] 522 523 zipfp.writepy(fn) 524 525 bn = os.path.basename(fn) 526 self.assert_(bn not in zipfp.namelist()) 527 self.assert_(bn + 'o' in zipfp.namelist() or bn + 'c' in zipfp.namelist()) 528 zipfp.close() 529 530 531 zipfp = zipfile.PyZipFile(TemporaryFile(), "w") 532 fn = __file__ 533 if fn.endswith('.pyc') or fn.endswith('.pyo'): 534 fn = fn[:-1] 535 536 zipfp.writepy(fn, "testpackage") 537 538 bn = "%s/%s"%("testpackage", os.path.basename(fn)) 539 self.assert_(bn not in zipfp.namelist()) 540 self.assert_(bn + 'o' in zipfp.namelist() or bn + 'c' in zipfp.namelist()) 541 zipfp.close() 542 543 def testWritePythonPackage(self): 715 def test_write_pyfile(self): 716 with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp: 717 fn = __file__ 718 if fn.endswith('.pyc') or fn.endswith('.pyo'): 719 fn = fn[:-1] 720 721 zipfp.writepy(fn) 722 723 bn = os.path.basename(fn) 724 self.assertNotIn(bn, zipfp.namelist()) 725 self.assertTrue(bn + 'o' in zipfp.namelist() or 726 bn + 'c' in zipfp.namelist()) 727 728 with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp: 729 fn = __file__ 730 if fn.endswith(('.pyc', '.pyo')): 731 fn = fn[:-1] 732 733 zipfp.writepy(fn, "testpackage") 734 735 bn = "%s/%s" % ("testpackage", os.path.basename(fn)) 736 self.assertNotIn(bn, zipfp.namelist()) 737 self.assertTrue(bn + 'o' in zipfp.namelist() or 738 bn + 'c' in zipfp.namelist()) 739 740 def test_write_python_package(self): 544 741 import email 545 742 packagedir = os.path.dirname(email.__file__) 546 743 547 zipfp = zipfile.PyZipFile(TemporaryFile(), "w") 548 zipfp.writepy(packagedir) 549 550 # Check for a couple of modules at different levels of the hieararchy 551 names = zipfp.namelist() 552 self.assert_('email/__init__.pyo' in names or 'email/__init__.pyc' in names) 553 self.assert_('email/mime/text.pyo' in names or 'email/mime/text.pyc' in names) 554 555 def testWritePythonDirectory(self): 744 with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp: 745 zipfp.writepy(packagedir) 746 747 # Check for a couple of modules at different levels of the 748 # hierarchy 749 names = zipfp.namelist() 750 self.assertTrue('email/__init__.pyo' in names or 751 'email/__init__.pyc' in names) 752 self.assertTrue('email/mime/text.pyo' in names or 753 'email/mime/text.pyc' in names) 754 755 def test_write_python_directory(self): 556 756 os.mkdir(TESTFN2) 557 757 try: 558 fp = open(os.path.join(TESTFN2, "mod1.py"), "w") 559 fp.write("print 42\n") 560 fp.close() 561 562 fp = open(os.path.join(TESTFN2, "mod2.py"), "w") 563 fp.write("print 42 * 42\n") 564 fp.close() 565 566 fp = open(os.path.join(TESTFN2, "mod2.txt"), "w") 567 fp.write("bla bla bla\n") 568 fp.close() 758 with open(os.path.join(TESTFN2, "mod1.py"), "w") as fp: 759 fp.write("print(42)\n") 760 761 with open(os.path.join(TESTFN2, "mod2.py"), "w") as fp: 762 fp.write("print(42 * 42)\n") 763 764 with open(os.path.join(TESTFN2, "mod2.txt"), "w") as fp: 765 fp.write("bla bla bla\n") 569 766 570 767 zipfp = zipfile.PyZipFile(TemporaryFile(), "w") … … 572 769 573 770 names = zipfp.namelist() 574 self.assert _('mod1.pyc' in names or 'mod1.pyo' in names)575 self.assert _('mod2.pyc' in names or 'mod2.pyo' in names)576 self.assert _('mod2.txt' not innames)771 self.assertTrue('mod1.pyc' in names or 'mod1.pyo' in names) 772 self.assertTrue('mod2.pyc' in names or 'mod2.pyo' in names) 773 self.assertNotIn('mod2.txt', names) 577 774 578 775 finally: 579 776 shutil.rmtree(TESTFN2) 580 777 581 def test WriteNonPyfile(self):582 zipfp = zipfile.PyZipFile(TemporaryFile(), "w")583 file(TESTFN, 'w').write('most definitely not a python file')584 self.assertRaises(RuntimeError, zipfp.writepy, TESTFN)585 os.remove(TESTFN)778 def test_write_non_pyfile(self): 779 with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp: 780 open(TESTFN, 'w').write('most definitely not a python file') 781 self.assertRaises(RuntimeError, zipfp.writepy, TESTFN) 782 os.remove(TESTFN) 586 783 587 784 588 785 class OtherTests(unittest.TestCase): 589 def testUnicodeFilenames(self): 590 zf = zipfile.ZipFile(TESTFN, "w") 591 zf.writestr(u"foo.txt", "Test for unicode filename") 592 zf.writestr(u"\xf6.txt", "Test for unicode filename") 593 self.assertTrue(isinstance(zf.infolist()[0].filename, unicode)) 594 zf.close() 595 zf = zipfile.ZipFile(TESTFN, "r") 596 self.assertEqual(zf.filelist[0].filename, "foo.txt") 597 self.assertEqual(zf.filelist[1].filename, u"\xf6.txt") 598 zf.close() 599 600 def testCreateNonExistentFileForAppend(self): 786 zips_with_bad_crc = { 787 zipfile.ZIP_STORED: ( 788 b'PK\003\004\024\0\0\0\0\0 \213\212;:r' 789 b'\253\377\f\0\0\0\f\0\0\0\005\0\0\000af' 790 b'ilehello,AworldP' 791 b'K\001\002\024\003\024\0\0\0\0\0 \213\212;:' 792 b'r\253\377\f\0\0\0\f\0\0\0\005\0\0\0\0' 793 b'\0\0\0\0\0\0\0\200\001\0\0\0\000afi' 794 b'lePK\005\006\0\0\0\0\001\0\001\0003\000' 795 b'\0\0/\0\0\0\0\0'), 796 zipfile.ZIP_DEFLATED: ( 797 b'PK\x03\x04\x14\x00\x00\x00\x08\x00n}\x0c=FA' 798 b'KE\x10\x00\x00\x00n\x00\x00\x00\x05\x00\x00\x00af' 799 b'ile\xcbH\xcd\xc9\xc9W(\xcf/\xcaI\xc9\xa0' 800 b'=\x13\x00PK\x01\x02\x14\x03\x14\x00\x00\x00\x08\x00n' 801 b'}\x0c=FAKE\x10\x00\x00\x00n\x00\x00\x00\x05' 802 b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x01\x00\x00\x00' 803 b'\x00afilePK\x05\x06\x00\x00\x00\x00\x01\x00' 804 b'\x01\x003\x00\x00\x003\x00\x00\x00\x00\x00'), 805 } 806 807 def test_unicode_filenames(self): 808 with zipfile.ZipFile(TESTFN, "w") as zf: 809 zf.writestr(u"foo.txt", "Test for unicode filename") 810 zf.writestr(u"\xf6.txt", "Test for unicode filename") 811 self.assertIsInstance(zf.infolist()[0].filename, unicode) 812 813 with zipfile.ZipFile(TESTFN, "r") as zf: 814 self.assertEqual(zf.filelist[0].filename, "foo.txt") 815 self.assertEqual(zf.filelist[1].filename, u"\xf6.txt") 816 817 def test_create_non_existent_file_for_append(self): 601 818 if os.path.exists(TESTFN): 602 819 os.unlink(TESTFN) … … 606 823 607 824 try: 608 zf = zipfile.ZipFile(TESTFN, 'a') 609 zf.writestr(filename, content) 610 zf.close() 611 except IOError, (errno, errmsg): 825 with zipfile.ZipFile(TESTFN, 'a') as zf: 826 zf.writestr(filename, content) 827 except IOError: 612 828 self.fail('Could not append data to a non-existent zip file.') 613 829 614 self.assert_(os.path.exists(TESTFN)) 615 616 zf = zipfile.ZipFile(TESTFN, 'r') 617 self.assertEqual(zf.read(filename), content) 618 zf.close() 619 620 def testCloseErroneousFile(self): 830 self.assertTrue(os.path.exists(TESTFN)) 831 832 with zipfile.ZipFile(TESTFN, 'r') as zf: 833 self.assertEqual(zf.read(filename), content) 834 835 def test_close_erroneous_file(self): 621 836 # This test checks that the ZipFile constructor closes the file object 622 # it opens if there's an error in the file. If it doesn't, the traceback 623 # holds a reference to the ZipFile object and, indirectly, the file object. 837 # it opens if there's an error in the file. If it doesn't, the 838 # traceback holds a reference to the ZipFile object and, indirectly, 839 # the file object. 624 840 # On Windows, this causes the os.unlink() call to fail because the 625 841 # underlying file is still open. This is SF bug #412214. 626 842 # 627 fp = open(TESTFN, "w") 628 fp.write("this is not a legal zip file\n") 629 fp.close() 843 with open(TESTFN, "w") as fp: 844 fp.write("this is not a legal zip file\n") 630 845 try: 631 846 zf = zipfile.ZipFile(TESTFN) … … 633 848 pass 634 849 635 def testIsZipErroneousFile(self): 636 # This test checks that the is_zipfile function correctly identifies 637 # a file that is not a zip file 638 fp = open(TESTFN, "w") 850 def test_is_zip_erroneous_file(self): 851 """Check that is_zipfile() correctly identifies non-zip files.""" 852 # - passing a filename 853 with open(TESTFN, "w") as fp: 854 fp.write("this is not a legal zip file\n") 855 chk = zipfile.is_zipfile(TESTFN) 856 self.assertFalse(chk) 857 # - passing a file object 858 with open(TESTFN, "rb") as fp: 859 chk = zipfile.is_zipfile(fp) 860 self.assertTrue(not chk) 861 # - passing a file-like object 862 fp = StringIO() 639 863 fp.write("this is not a legal zip file\n") 640 fp.close() 864 chk = zipfile.is_zipfile(fp) 865 self.assertTrue(not chk) 866 fp.seek(0, 0) 867 chk = zipfile.is_zipfile(fp) 868 self.assertTrue(not chk) 869 870 def test_damaged_zipfile(self): 871 """Check that zipfiles with missing bytes at the end raise BadZipFile.""" 872 # - Create a valid zip file 873 fp = io.BytesIO() 874 with zipfile.ZipFile(fp, mode="w") as zipf: 875 zipf.writestr("foo.txt", b"O, for a Muse of Fire!") 876 zipfiledata = fp.getvalue() 877 878 # - Now create copies of it missing the last N bytes and make sure 879 # a BadZipFile exception is raised when we try to open it 880 for N in range(len(zipfiledata)): 881 fp = io.BytesIO(zipfiledata[:N]) 882 self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, fp) 883 884 def test_is_zip_valid_file(self): 885 """Check that is_zipfile() correctly identifies zip files.""" 886 # - passing a filename 887 with zipfile.ZipFile(TESTFN, mode="w") as zipf: 888 zipf.writestr("foo.txt", "O, for a Muse of Fire!") 641 889 chk = zipfile.is_zipfile(TESTFN) 642 self.assert_(chk is False) 643 644 def testIsZipValidFile(self): 645 # This test checks that the is_zipfile function correctly identifies 646 # a file that is a zip file 647 zipf = zipfile.ZipFile(TESTFN, mode="w") 648 zipf.writestr("foo.txt", "O, for a Muse of Fire!") 649 zipf.close() 650 chk = zipfile.is_zipfile(TESTFN) 651 self.assert_(chk is True) 652 653 def testNonExistentFileRaisesIOError(self): 890 self.assertTrue(chk) 891 # - passing a file object 892 with open(TESTFN, "rb") as fp: 893 chk = zipfile.is_zipfile(fp) 894 self.assertTrue(chk) 895 fp.seek(0, 0) 896 zip_contents = fp.read() 897 # - passing a file-like object 898 fp = StringIO() 899 fp.write(zip_contents) 900 chk = zipfile.is_zipfile(fp) 901 self.assertTrue(chk) 902 fp.seek(0, 0) 903 chk = zipfile.is_zipfile(fp) 904 self.assertTrue(chk) 905 906 def test_non_existent_file_raises_IOError(self): 654 907 # make sure we don't raise an AttributeError when a partially-constructed 655 908 # ZipFile instance is finalized; this tests for regression on SF tracker … … 666 919 667 920 def test_empty_file_raises_BadZipFile(self): 668 f = open(TESTFN, 'w')669 f.close()921 with open(TESTFN, 'w') as f: 922 pass 670 923 self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN) 671 924 672 f = open(TESTFN, 'w') 673 f.write("short file") 674 f.close() 925 with open(TESTFN, 'w') as fp: 926 fp.write("short file") 675 927 self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN) 676 928 677 def test ClosedZipRaisesRuntimeError(self):678 # Verify that testzip() doesn't swallow inappropriate exceptions.929 def test_closed_zip_raises_RuntimeError(self): 930 """Verify that testzip() doesn't swallow inappropriate exceptions.""" 679 931 data = StringIO() 680 zipf = zipfile.ZipFile(data, mode="w") 681 zipf.writestr("foo.txt", "O, for a Muse of Fire!") 682 zipf.close() 683 684 # This is correct; calling .read on a closed ZipFile should throw 932 with zipfile.ZipFile(data, mode="w") as zipf: 933 zipf.writestr("foo.txt", "O, for a Muse of Fire!") 934 935 # This is correct; calling .read on a closed ZipFile should raise 685 936 # a RuntimeError, and so should calling .testzip. An earlier 686 937 # version of .testzip would swallow this exception (and any other) … … 690 941 self.assertRaises(RuntimeError, zipf.testzip) 691 942 self.assertRaises(RuntimeError, zipf.writestr, "bogus.txt", "bogus") 692 file(TESTFN, 'w').write('zipfile test data')943 open(TESTFN, 'w').write('zipfile test data') 693 944 self.assertRaises(RuntimeError, zipf.write, TESTFN) 694 945 695 def test_ BadConstructorMode(self):696 # Check that bad modes passed to ZipFile constructor are caught946 def test_bad_constructor_mode(self): 947 """Check that bad modes passed to ZipFile constructor are caught.""" 697 948 self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "q") 698 949 699 def test_ BadOpenMode(self):700 # Check that bad modes passed to ZipFile.open are caught701 zipf = zipfile.ZipFile(TESTFN, mode="w")702 zipf.writestr("foo.txt", "O, for a Muse of Fire!")703 zipf.close() 704 zipf = zipfile.ZipFile(TESTFN, mode="r")950 def test_bad_open_mode(self): 951 """Check that bad modes passed to ZipFile.open are caught.""" 952 with zipfile.ZipFile(TESTFN, mode="w") as zipf: 953 zipf.writestr("foo.txt", "O, for a Muse of Fire!") 954 955 with zipfile.ZipFile(TESTFN, mode="r") as zipf: 705 956 # read the data to make sure the file is there 706 zipf.read("foo.txt") 707 self.assertRaises(RuntimeError, zipf.open, "foo.txt", "q") 708 zipf.close() 709 710 def test_Read0(self): 711 # Check that calling read(0) on a ZipExtFile object returns an empty 712 # string and doesn't advance file pointer 713 zipf = zipfile.ZipFile(TESTFN, mode="w") 714 zipf.writestr("foo.txt", "O, for a Muse of Fire!") 715 # read the data to make sure the file is there 716 f = zipf.open("foo.txt") 717 for i in xrange(FIXEDTEST_SIZE): 718 self.assertEqual(f.read(0), '') 719 720 self.assertEqual(f.read(), "O, for a Muse of Fire!") 721 zipf.close() 722 723 def test_OpenNonexistentItem(self): 724 # Check that attempting to call open() for an item that doesn't 725 # exist in the archive raises a RuntimeError 726 zipf = zipfile.ZipFile(TESTFN, mode="w") 727 self.assertRaises(KeyError, zipf.open, "foo.txt", "r") 728 729 def test_BadCompressionMode(self): 730 # Check that bad compression methods passed to ZipFile.open are caught 957 zipf.read("foo.txt") 958 self.assertRaises(RuntimeError, zipf.open, "foo.txt", "q") 959 960 def test_read0(self): 961 """Check that calling read(0) on a ZipExtFile object returns an empty 962 string and doesn't advance file pointer.""" 963 with zipfile.ZipFile(TESTFN, mode="w") as zipf: 964 zipf.writestr("foo.txt", "O, for a Muse of Fire!") 965 # read the data to make sure the file is there 966 with zipf.open("foo.txt") as f: 967 for i in xrange(FIXEDTEST_SIZE): 968 self.assertEqual(f.read(0), '') 969 970 self.assertEqual(f.read(), "O, for a Muse of Fire!") 971 972 def test_open_non_existent_item(self): 973 """Check that attempting to call open() for an item that doesn't 974 exist in the archive raises a RuntimeError.""" 975 with zipfile.ZipFile(TESTFN, mode="w") as zipf: 976 self.assertRaises(KeyError, zipf.open, "foo.txt", "r") 977 978 def test_bad_compression_mode(self): 979 """Check that bad compression methods passed to ZipFile.open are 980 caught.""" 731 981 self.assertRaises(RuntimeError, zipfile.ZipFile, TESTFN, "w", -1) 732 982 733 def test_NullByteInFilename(self): 734 # Check that a filename containing a null byte is properly terminated 735 zipf = zipfile.ZipFile(TESTFN, mode="w") 736 zipf.writestr("foo.txt\x00qqq", "O, for a Muse of Fire!") 737 self.assertEqual(zipf.namelist(), ['foo.txt']) 738 739 def test_StructSizes(self): 740 # check that ZIP internal structure sizes are calculated correctly 983 def test_unsupported_compression(self): 984 # data is declared as shrunk, but actually deflated 985 data = (b'PK\x03\x04.\x00\x00\x00\x01\x00\xe4C\xa1@\x00\x00\x00' 986 b'\x00\x02\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00x\x03\x00PK\x01' 987 b'\x02.\x03.\x00\x00\x00\x01\x00\xe4C\xa1@\x00\x00\x00\x00\x02\x00\x00' 988 b'\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' 989 b'\x80\x01\x00\x00\x00\x00xPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x00' 990 b'/\x00\x00\x00!\x00\x00\x00\x00\x00') 991 with zipfile.ZipFile(io.BytesIO(data), 'r') as zipf: 992 self.assertRaises(NotImplementedError, zipf.open, 'x') 993 994 def test_null_byte_in_filename(self): 995 """Check that a filename containing a null byte is properly 996 terminated.""" 997 with zipfile.ZipFile(TESTFN, mode="w") as zipf: 998 zipf.writestr("foo.txt\x00qqq", "O, for a Muse of Fire!") 999 self.assertEqual(zipf.namelist(), ['foo.txt']) 1000 1001 def test_struct_sizes(self): 1002 """Check that ZIP internal structure sizes are calculated correctly.""" 741 1003 self.assertEqual(zipfile.sizeEndCentDir, 22) 742 1004 self.assertEqual(zipfile.sizeCentralDir, 46) … … 744 1006 self.assertEqual(zipfile.sizeEndCentDir64Locator, 20) 745 1007 746 def test Comments(self):747 # This test checks that comments on the archive are handled properly1008 def test_comments(self): 1009 """Check that comments on the archive are handled properly.""" 748 1010 749 1011 # check default comment is empty 750 zipf = zipfile.ZipFile(TESTFN, mode="w") 751 self.assertEqual(zipf.comment, '') 752 zipf.writestr("foo.txt", "O, for a Muse of Fire!") 753 zipf.close() 754 zipfr = zipfile.ZipFile(TESTFN, mode="r") 755 self.assertEqual(zipfr.comment, '') 756 zipfr.close() 1012 with zipfile.ZipFile(TESTFN, mode="w") as zipf: 1013 self.assertEqual(zipf.comment, '') 1014 zipf.writestr("foo.txt", "O, for a Muse of Fire!") 1015 1016 with zipfile.ZipFile(TESTFN, mode="r") as zipf: 1017 self.assertEqual(zipf.comment, '') 757 1018 758 1019 # check a simple short comment 759 1020 comment = 'Bravely taking to his feet, he beat a very brave retreat.' 760 zipf = zipfile.ZipFile(TESTFN, mode="w") 761 zipf.comment = comment 762 zipf.writestr("foo.txt", "O, for a Muse of Fire!") 763 zipf.close() 764 zipfr = zipfile.ZipFile(TESTFN, mode="r") 765 self.assertEqual(zipfr.comment, comment) 766 zipfr.close() 1021 with zipfile.ZipFile(TESTFN, mode="w") as zipf: 1022 zipf.comment = comment 1023 zipf.writestr("foo.txt", "O, for a Muse of Fire!") 1024 with zipfile.ZipFile(TESTFN, mode="r") as zipf: 1025 self.assertEqual(zipf.comment, comment) 767 1026 768 1027 # check a comment of max length 769 1028 comment2 = ''.join(['%d' % (i**3 % 10) for i in xrange((1 << 16)-1)]) 770 zipf = zipfile.ZipFile(TESTFN, mode="w") 771 zipf.comment = comment2 772 zipf.writestr("foo.txt", "O, for a Muse of Fire!") 773 zipf.close() 774 zipfr = zipfile.ZipFile(TESTFN, mode="r") 775 self.assertEqual(zipfr.comment, comment2) 776 zipfr.close() 1029 with zipfile.ZipFile(TESTFN, mode="w") as zipf: 1030 zipf.comment = comment2 1031 zipf.writestr("foo.txt", "O, for a Muse of Fire!") 1032 1033 with zipfile.ZipFile(TESTFN, mode="r") as zipf: 1034 self.assertEqual(zipf.comment, comment2) 777 1035 778 1036 # check a comment that is too long is truncated 779 zipf = zipfile.ZipFile(TESTFN, mode="w") 780 zipf.comment = comment2 + 'oops' 781 zipf.writestr("foo.txt", "O, for a Muse of Fire!") 782 zipf.close() 783 zipfr = zipfile.ZipFile(TESTFN, mode="r") 784 self.assertEqual(zipfr.comment, comment2) 785 zipfr.close() 1037 with zipfile.ZipFile(TESTFN, mode="w") as zipf: 1038 zipf.comment = comment2 + 'oops' 1039 zipf.writestr("foo.txt", "O, for a Muse of Fire!") 1040 with zipfile.ZipFile(TESTFN, mode="r") as zipf: 1041 self.assertEqual(zipf.comment, comment2) 1042 1043 def test_change_comment_in_empty_archive(self): 1044 with zipfile.ZipFile(TESTFN, "a", zipfile.ZIP_STORED) as zipf: 1045 self.assertFalse(zipf.filelist) 1046 zipf.comment = b"this is a comment" 1047 with zipfile.ZipFile(TESTFN, "r") as zipf: 1048 self.assertEqual(zipf.comment, b"this is a comment") 1049 1050 def test_change_comment_in_nonempty_archive(self): 1051 with zipfile.ZipFile(TESTFN, "w", zipfile.ZIP_STORED) as zipf: 1052 zipf.writestr("foo.txt", "O, for a Muse of Fire!") 1053 with zipfile.ZipFile(TESTFN, "a", zipfile.ZIP_STORED) as zipf: 1054 self.assertTrue(zipf.filelist) 1055 zipf.comment = b"this is a comment" 1056 with zipfile.ZipFile(TESTFN, "r") as zipf: 1057 self.assertEqual(zipf.comment, b"this is a comment") 1058 1059 def check_testzip_with_bad_crc(self, compression): 1060 """Tests that files with bad CRCs return their name from testzip.""" 1061 zipdata = self.zips_with_bad_crc[compression] 1062 1063 with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf: 1064 # testzip returns the name of the first corrupt file, or None 1065 self.assertEqual('afile', zipf.testzip()) 1066 1067 def test_testzip_with_bad_crc_stored(self): 1068 self.check_testzip_with_bad_crc(zipfile.ZIP_STORED) 1069 1070 @skipUnless(zlib, "requires zlib") 1071 def test_testzip_with_bad_crc_deflated(self): 1072 self.check_testzip_with_bad_crc(zipfile.ZIP_DEFLATED) 1073 1074 def check_read_with_bad_crc(self, compression): 1075 """Tests that files with bad CRCs raise a BadZipfile exception when read.""" 1076 zipdata = self.zips_with_bad_crc[compression] 1077 1078 # Using ZipFile.read() 1079 with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf: 1080 self.assertRaises(zipfile.BadZipfile, zipf.read, 'afile') 1081 1082 # Using ZipExtFile.read() 1083 with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf: 1084 with zipf.open('afile', 'r') as corrupt_file: 1085 self.assertRaises(zipfile.BadZipfile, corrupt_file.read) 1086 1087 # Same with small reads (in order to exercise the buffering logic) 1088 with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf: 1089 with zipf.open('afile', 'r') as corrupt_file: 1090 corrupt_file.MIN_READ_SIZE = 2 1091 with self.assertRaises(zipfile.BadZipfile): 1092 while corrupt_file.read(2): 1093 pass 1094 1095 def test_read_with_bad_crc_stored(self): 1096 self.check_read_with_bad_crc(zipfile.ZIP_STORED) 1097 1098 @skipUnless(zlib, "requires zlib") 1099 def test_read_with_bad_crc_deflated(self): 1100 self.check_read_with_bad_crc(zipfile.ZIP_DEFLATED) 1101 1102 def check_read_return_size(self, compression): 1103 # Issue #9837: ZipExtFile.read() shouldn't return more bytes 1104 # than requested. 1105 for test_size in (1, 4095, 4096, 4097, 16384): 1106 file_size = test_size + 1 1107 junk = b''.join(struct.pack('B', randint(0, 255)) 1108 for x in range(file_size)) 1109 with zipfile.ZipFile(io.BytesIO(), "w", compression) as zipf: 1110 zipf.writestr('foo', junk) 1111 with zipf.open('foo', 'r') as fp: 1112 buf = fp.read(test_size) 1113 self.assertEqual(len(buf), test_size) 1114 1115 def test_read_return_size_stored(self): 1116 self.check_read_return_size(zipfile.ZIP_STORED) 1117 1118 @skipUnless(zlib, "requires zlib") 1119 def test_read_return_size_deflated(self): 1120 self.check_read_return_size(zipfile.ZIP_DEFLATED) 1121 1122 def test_empty_zipfile(self): 1123 # Check that creating a file in 'w' or 'a' mode and closing without 1124 # adding any files to the archives creates a valid empty ZIP file 1125 with zipfile.ZipFile(TESTFN, mode="w") as zipf: 1126 pass 1127 try: 1128 zipf = zipfile.ZipFile(TESTFN, mode="r") 1129 except zipfile.BadZipfile: 1130 self.fail("Unable to create empty ZIP file in 'w' mode") 1131 1132 with zipfile.ZipFile(TESTFN, mode="a") as zipf: 1133 pass 1134 try: 1135 zipf = zipfile.ZipFile(TESTFN, mode="r") 1136 except: 1137 self.fail("Unable to create empty ZIP file in 'a' mode") 1138 1139 def test_open_empty_file(self): 1140 # Issue 1710703: Check that opening a file with less than 22 bytes 1141 # raises a BadZipfile exception (rather than the previously unhelpful 1142 # IOError) 1143 with open(TESTFN, 'w') as f: 1144 pass 1145 self.assertRaises(zipfile.BadZipfile, zipfile.ZipFile, TESTFN, 'r') 1146 1147 def test_create_zipinfo_before_1980(self): 1148 self.assertRaises(ValueError, 1149 zipfile.ZipInfo, 'seventies', (1979, 1, 1, 0, 0, 0)) 786 1150 787 1151 def tearDown(self): 788 support.unlink(TESTFN) 789 support.unlink(TESTFN2) 1152 unlink(TESTFN) 1153 unlink(TESTFN2) 1154 790 1155 791 1156 class DecryptionTests(unittest.TestCase): 792 # This test checksthat ZIP decryption works. Since the library does not793 #support encryption at the moment, we use a pre-generated encrypted794 # ZIP file1157 """Check that ZIP decryption works. Since the library does not 1158 support encryption at the moment, we use a pre-generated encrypted 1159 ZIP file.""" 795 1160 796 1161 data = ( … … 816 1181 817 1182 def setUp(self): 818 fp = open(TESTFN, "wb") 819 fp.write(self.data) 820 fp.close() 1183 with open(TESTFN, "wb") as fp: 1184 fp.write(self.data) 821 1185 self.zip = zipfile.ZipFile(TESTFN, "r") 822 fp = open(TESTFN2, "wb") 823 fp.write(self.data2) 824 fp.close() 1186 with open(TESTFN2, "wb") as fp: 1187 fp.write(self.data2) 825 1188 self.zip2 = zipfile.ZipFile(TESTFN2, "r") 826 1189 … … 831 1194 os.unlink(TESTFN2) 832 1195 833 def test NoPassword(self):1196 def test_no_password(self): 834 1197 # Reading the encrypted file without password 835 1198 # must generate a RunTime exception … … 837 1200 self.assertRaises(RuntimeError, self.zip2.read, "zero") 838 1201 839 def test BadPassword(self):1202 def test_bad_password(self): 840 1203 self.zip.setpassword("perl") 841 1204 self.assertRaises(RuntimeError, self.zip.read, "test.txt") … … 843 1206 self.assertRaises(RuntimeError, self.zip2.read, "zero") 844 1207 845 def testGoodPassword(self): 1208 @skipUnless(zlib, "requires zlib") 1209 def test_good_password(self): 846 1210 self.zip.setpassword("python") 847 self.assertEqual s(self.zip.read("test.txt"), self.plain)1211 self.assertEqual(self.zip.read("test.txt"), self.plain) 848 1212 self.zip2.setpassword("12345") 849 self.assertEqual s(self.zip2.read("zero"), self.plain2)1213 self.assertEqual(self.zip2.read("zero"), self.plain2) 850 1214 851 1215 … … 853 1217 def setUp(self): 854 1218 datacount = randint(16, 64)*1024 + randint(1, 1024) 855 self.data = ''.join((struct.pack('<f', random()*randint(-1000, 1000)) for i in xrange(datacount))) 1219 self.data = ''.join(struct.pack('<f', random()*randint(-1000, 1000)) 1220 for i in xrange(datacount)) 856 1221 857 1222 # Make a source file with some lines 858 fp = open(TESTFN, "wb") 859 fp.write(self.data) 860 fp.close() 1223 with open(TESTFN, "wb") as fp: 1224 fp.write(self.data) 861 1225 862 1226 def tearDown(self): 863 support.unlink(TESTFN)864 support.unlink(TESTFN2)865 866 def make TestArchive(self, f, compression):1227 unlink(TESTFN) 1228 unlink(TESTFN2) 1229 1230 def make_test_archive(self, f, compression): 867 1231 # Create the ZIP archive 868 zipfp = zipfile.ZipFile(f, "w", compression) 869 zipfp.write(TESTFN, "another"+os.extsep+"name") 870 zipfp.write(TESTFN, TESTFN) 871 zipfp.close() 872 873 def zipTest(self, f, compression): 874 self.makeTestArchive(f, compression) 1232 with zipfile.ZipFile(f, "w", compression) as zipfp: 1233 zipfp.write(TESTFN, "another.name") 1234 zipfp.write(TESTFN, TESTFN) 1235 1236 def zip_test(self, f, compression): 1237 self.make_test_archive(f, compression) 875 1238 876 1239 # Read the ZIP archive 877 zipfp = zipfile.ZipFile(f, "r", compression) 878 testdata = zipfp.read(TESTFN) 879 self.assertEqual(len(testdata), len(self.data)) 880 self.assertEqual(testdata, self.data) 881 self.assertEqual(zipfp.read("another"+os.extsep+"name"), self.data) 882 zipfp.close() 883 884 def testStored(self): 885 for f in (TESTFN2, TemporaryFile(), StringIO()): 886 self.zipTest(f, zipfile.ZIP_STORED) 887 888 def zipOpenTest(self, f, compression): 889 self.makeTestArchive(f, compression) 1240 with zipfile.ZipFile(f, "r", compression) as zipfp: 1241 testdata = zipfp.read(TESTFN) 1242 self.assertEqual(len(testdata), len(self.data)) 1243 self.assertEqual(testdata, self.data) 1244 self.assertEqual(zipfp.read("another.name"), self.data) 1245 1246 def test_stored(self): 1247 for f in (TESTFN2, TemporaryFile(), StringIO()): 1248 self.zip_test(f, zipfile.ZIP_STORED) 1249 1250 @skipUnless(zlib, "requires zlib") 1251 def test_deflated(self): 1252 for f in (TESTFN2, TemporaryFile(), io.BytesIO()): 1253 self.zip_test(f, zipfile.ZIP_DEFLATED) 1254 1255 def zip_open_test(self, f, compression): 1256 self.make_test_archive(f, compression) 890 1257 891 1258 # Read the ZIP archive 892 zipfp = zipfile.ZipFile(f, "r", compression) 893 zipdata1 = [] 894 zipopen1 = zipfp.open(TESTFN) 895 while 1: 896 read_data = zipopen1.read(256) 897 if not read_data: 898 break 899 zipdata1.append(read_data) 900 901 zipdata2 = [] 902 zipopen2 = zipfp.open("another"+os.extsep+"name") 903 while 1: 904 read_data = zipopen2.read(256) 905 if not read_data: 906 break 907 zipdata2.append(read_data) 908 909 testdata1 = ''.join(zipdata1) 910 self.assertEqual(len(testdata1), len(self.data)) 911 self.assertEqual(testdata1, self.data) 912 913 testdata2 = ''.join(zipdata2) 914 self.assertEqual(len(testdata1), len(self.data)) 915 self.assertEqual(testdata1, self.data) 916 zipfp.close() 917 918 def testOpenStored(self): 919 for f in (TESTFN2, TemporaryFile(), StringIO()): 920 self.zipOpenTest(f, zipfile.ZIP_STORED) 921 922 def zipRandomOpenTest(self, f, compression): 923 self.makeTestArchive(f, compression) 1259 with zipfile.ZipFile(f, "r", compression) as zipfp: 1260 zipdata1 = [] 1261 with zipfp.open(TESTFN) as zipopen1: 1262 while True: 1263 read_data = zipopen1.read(256) 1264 if not read_data: 1265 break 1266 zipdata1.append(read_data) 1267 1268 zipdata2 = [] 1269 with zipfp.open("another.name") as zipopen2: 1270 while True: 1271 read_data = zipopen2.read(256) 1272 if not read_data: 1273 break 1274 zipdata2.append(read_data) 1275 1276 testdata1 = ''.join(zipdata1) 1277 self.assertEqual(len(testdata1), len(self.data)) 1278 self.assertEqual(testdata1, self.data) 1279 1280 testdata2 = ''.join(zipdata2) 1281 self.assertEqual(len(testdata2), len(self.data)) 1282 self.assertEqual(testdata2, self.data) 1283 1284 def test_open_stored(self): 1285 for f in (TESTFN2, TemporaryFile(), StringIO()): 1286 self.zip_open_test(f, zipfile.ZIP_STORED) 1287 1288 @skipUnless(zlib, "requires zlib") 1289 def test_open_deflated(self): 1290 for f in (TESTFN2, TemporaryFile(), io.BytesIO()): 1291 self.zip_open_test(f, zipfile.ZIP_DEFLATED) 1292 1293 def zip_random_open_test(self, f, compression): 1294 self.make_test_archive(f, compression) 924 1295 925 1296 # Read the ZIP archive 926 zipfp = zipfile.ZipFile(f, "r", compression) 927 zipdata1 = [] 928 zipopen1 = zipfp.open(TESTFN) 929 while 1: 930 read_data = zipopen1.read(randint(1, 1024)) 931 if not read_data: 932 break 933 zipdata1.append(read_data) 934 935 testdata = ''.join(zipdata1) 936 self.assertEqual(len(testdata), len(self.data)) 937 self.assertEqual(testdata, self.data) 938 zipfp.close() 939 940 def testRandomOpenStored(self): 941 for f in (TESTFN2, TemporaryFile(), StringIO()): 942 self.zipRandomOpenTest(f, zipfile.ZIP_STORED) 943 1297 with zipfile.ZipFile(f, "r", compression) as zipfp: 1298 zipdata1 = [] 1299 with zipfp.open(TESTFN) as zipopen1: 1300 while True: 1301 read_data = zipopen1.read(randint(1, 1024)) 1302 if not read_data: 1303 break 1304 zipdata1.append(read_data) 1305 1306 testdata = ''.join(zipdata1) 1307 self.assertEqual(len(testdata), len(self.data)) 1308 self.assertEqual(testdata, self.data) 1309 1310 def test_random_open_stored(self): 1311 for f in (TESTFN2, TemporaryFile(), StringIO()): 1312 self.zip_random_open_test(f, zipfile.ZIP_STORED) 1313 1314 @skipUnless(zlib, "requires zlib") 1315 def test_random_open_deflated(self): 1316 for f in (TESTFN2, TemporaryFile(), io.BytesIO()): 1317 self.zip_random_open_test(f, zipfile.ZIP_DEFLATED) 1318 1319 1320 @skipUnless(zlib, "requires zlib") 944 1321 class TestsWithMultipleOpens(unittest.TestCase): 945 1322 def setUp(self): 946 1323 # Create the ZIP archive 947 zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED) 948 zipfp.writestr('ones', '1'*FIXEDTEST_SIZE) 949 zipfp.writestr('twos', '2'*FIXEDTEST_SIZE) 950 zipfp.close() 951 952 def testSameFile(self): 1324 with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_DEFLATED) as zipfp: 1325 zipfp.writestr('ones', '1'*FIXEDTEST_SIZE) 1326 zipfp.writestr('twos', '2'*FIXEDTEST_SIZE) 1327 1328 def test_same_file(self): 953 1329 # Verify that (when the ZipFile is in control of creating file objects) 954 1330 # multiple open() calls can be made without interfering with each other. 955 zipf = zipfile.ZipFile(TESTFN2, mode="r") 956 zopen1 = zipf.open('ones') 957 zopen2 = zipf.open('ones') 958 data1 = zopen1.read(500) 959 data2 = zopen2.read(500) 960 data1 += zopen1.read(500) 961 data2 += zopen2.read(500) 962 self.assertEqual(data1, data2) 963 zipf.close() 964 965 def testDifferentFile(self): 1331 with zipfile.ZipFile(TESTFN2, mode="r") as zipf: 1332 zopen1 = zipf.open('ones') 1333 zopen2 = zipf.open('ones') 1334 data1 = zopen1.read(500) 1335 data2 = zopen2.read(500) 1336 data1 += zopen1.read(500) 1337 data2 += zopen2.read(500) 1338 self.assertEqual(data1, data2) 1339 1340 def test_different_file(self): 966 1341 # Verify that (when the ZipFile is in control of creating file objects) 967 1342 # multiple open() calls can be made without interfering with each other. 968 zipf = zipfile.ZipFile(TESTFN2, mode="r") 969 zopen1 = zipf.open('ones') 970 zopen2 = zipf.open('twos') 971 data1 = zopen1.read(500) 972 data2 = zopen2.read(500) 973 data1 += zopen1.read(500) 974 data2 += zopen2.read(500) 975 self.assertEqual(data1, '1'*FIXEDTEST_SIZE) 976 self.assertEqual(data2, '2'*FIXEDTEST_SIZE) 977 zipf.close() 978 979 def testInterleaved(self): 1343 with zipfile.ZipFile(TESTFN2, mode="r") as zipf: 1344 with zipf.open('ones') as zopen1, zipf.open('twos') as zopen2: 1345 data1 = zopen1.read(500) 1346 data2 = zopen2.read(500) 1347 data1 += zopen1.read(500) 1348 data2 += zopen2.read(500) 1349 self.assertEqual(data1, '1'*FIXEDTEST_SIZE) 1350 self.assertEqual(data2, '2'*FIXEDTEST_SIZE) 1351 1352 def test_interleaved(self): 980 1353 # Verify that (when the ZipFile is in control of creating file objects) 981 1354 # multiple open() calls can be made without interfering with each other. 982 zipf = zipfile.ZipFile(TESTFN2, mode="r") 983 zopen1 = zipf.open('ones') 984 data1 = zopen1.read(500) 985 zopen2 = zipf.open('twos') 986 data2 = zopen2.read(500) 987 data1 += zopen1.read(500) 988 data2 += zopen2.read(500) 989 self.assertEqual(data1, '1'*FIXEDTEST_SIZE) 990 self.assertEqual(data2, '2'*FIXEDTEST_SIZE) 991 zipf.close() 1355 with zipfile.ZipFile(TESTFN2, mode="r") as zipf: 1356 with zipf.open('ones') as zopen1, zipf.open('twos') as zopen2: 1357 data1 = zopen1.read(500) 1358 data2 = zopen2.read(500) 1359 data1 += zopen1.read(500) 1360 data2 += zopen2.read(500) 1361 self.assertEqual(data1, '1'*FIXEDTEST_SIZE) 1362 self.assertEqual(data2, '2'*FIXEDTEST_SIZE) 992 1363 993 1364 def tearDown(self): 994 os.remove(TESTFN2) 1365 unlink(TESTFN2) 1366 995 1367 996 1368 class TestWithDirectory(unittest.TestCase): … … 998 1370 os.mkdir(TESTFN2) 999 1371 1000 def test ExtractDir(self):1001 zipf = zipfile.ZipFile(findfile("zipdir.zip"))1002 zipf.extractall(TESTFN2)1372 def test_extract_dir(self): 1373 with zipfile.ZipFile(findfile("zipdir.zip")) as zipf: 1374 zipf.extractall(TESTFN2) 1003 1375 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a"))) 1004 1376 self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a", "b"))) … … 1008 1380 # Extraction should succeed if directories already exist 1009 1381 os.mkdir(os.path.join(TESTFN2, "a")) 1010 self.test ExtractDir()1011 1012 def test StoreDir(self):1382 self.test_extract_dir() 1383 1384 def test_store_dir(self): 1013 1385 os.mkdir(os.path.join(TESTFN2, "x")) 1014 1386 zipf = zipfile.ZipFile(TESTFN, "w") … … 1019 1391 shutil.rmtree(TESTFN2) 1020 1392 if os.path.exists(TESTFN): 1021 os.remove(TESTFN)1393 unlink(TESTFN) 1022 1394 1023 1395 1024 1396 class UniversalNewlineTests(unittest.TestCase): 1025 1397 def setUp(self): 1026 self.line_gen = ["Test of zipfile line %d." % i for i in xrange(FIXEDTEST_SIZE)] 1398 self.line_gen = ["Test of zipfile line %d." % i 1399 for i in xrange(FIXEDTEST_SIZE)] 1027 1400 self.seps = ('\r', '\r\n', '\n') 1028 1401 self.arcdata, self.arcfiles = {}, {} … … 1032 1405 open(self.arcfiles[s], "wb").write(self.arcdata[s]) 1033 1406 1034 def make TestArchive(self, f, compression):1407 def make_test_archive(self, f, compression): 1035 1408 # Create the ZIP archive 1036 zipfp = zipfile.ZipFile(f, "w", compression) 1037 for fn in self.arcfiles.values(): 1038 zipfp.write(fn, fn) 1039 zipfp.close() 1040 1041 def readTest(self, f, compression): 1042 self.makeTestArchive(f, compression) 1409 with zipfile.ZipFile(f, "w", compression) as zipfp: 1410 for fn in self.arcfiles.values(): 1411 zipfp.write(fn, fn) 1412 1413 def read_test(self, f, compression): 1414 self.make_test_archive(f, compression) 1415 1416 # Read the ZIP archive 1417 with zipfile.ZipFile(f, "r") as zipfp: 1418 for sep, fn in self.arcfiles.items(): 1419 with zipfp.open(fn, "rU") as fp: 1420 zipdata = fp.read() 1421 self.assertEqual(self.arcdata[sep], zipdata) 1422 1423 def readline_read_test(self, f, compression): 1424 self.make_test_archive(f, compression) 1043 1425 1044 1426 # Read the ZIP archive 1045 1427 zipfp = zipfile.ZipFile(f, "r") 1046 1428 for sep, fn in self.arcfiles.items(): 1047 zipdata = zipfp.open(fn, "rU").read() 1048 self.assertEqual(self.arcdata[sep], zipdata) 1429 with zipfp.open(fn, "rU") as zipopen: 1430 data = '' 1431 while True: 1432 read = zipopen.readline() 1433 if not read: 1434 break 1435 data += read 1436 1437 read = zipopen.read(5) 1438 if not read: 1439 break 1440 data += read 1441 1442 self.assertEqual(data, self.arcdata['\n']) 1049 1443 1050 1444 zipfp.close() 1051 1445 1052 def readline Test(self, f, compression):1053 self.make TestArchive(f, compression)1446 def readline_test(self, f, compression): 1447 self.make_test_archive(f, compression) 1054 1448 1055 1449 # Read the ZIP archive 1056 zipfp = zipfile.ZipFile(f, "r") 1057 for sep, fn in self.arcfiles.items(): 1058 zipopen = zipfp.open(fn, "rU") 1059 for line in self.line_gen: 1060 linedata = zipopen.readline() 1061 self.assertEqual(linedata, line + '\n') 1062 1063 zipfp.close() 1064 1065 def readlinesTest(self, f, compression): 1066 self.makeTestArchive(f, compression) 1450 with zipfile.ZipFile(f, "r") as zipfp: 1451 for sep, fn in self.arcfiles.items(): 1452 with zipfp.open(fn, "rU") as zipopen: 1453 for line in self.line_gen: 1454 linedata = zipopen.readline() 1455 self.assertEqual(linedata, line + '\n') 1456 1457 def readlines_test(self, f, compression): 1458 self.make_test_archive(f, compression) 1067 1459 1068 1460 # Read the ZIP archive 1069 zipfp = zipfile.ZipFile(f, "r") 1070 for sep, fn in self.arcfiles.items(): 1071 ziplines = zipfp.open(fn, "rU").readlines() 1072 for line, zipline in zip(self.line_gen, ziplines): 1073 self.assertEqual(zipline, line + '\n') 1074 1075 zipfp.close() 1076 1077 def iterlinesTest(self, f, compression): 1078 self.makeTestArchive(f, compression) 1461 with zipfile.ZipFile(f, "r") as zipfp: 1462 for sep, fn in self.arcfiles.items(): 1463 with zipfp.open(fn, "rU") as fp: 1464 ziplines = fp.readlines() 1465 for line, zipline in zip(self.line_gen, ziplines): 1466 self.assertEqual(zipline, line + '\n') 1467 1468 def iterlines_test(self, f, compression): 1469 self.make_test_archive(f, compression) 1079 1470 1080 1471 # Read the ZIP archive 1081 zipfp = zipfile.ZipFile(f, "r") 1082 for sep, fn in self.arcfiles.items(): 1083 for line, zipline in zip(self.line_gen, zipfp.open(fn, "rU")): 1084 self.assertEqual(zipline, line + '\n') 1085 1086 zipfp.close() 1087 1088 def testReadStored(self): 1089 for f in (TESTFN2, TemporaryFile(), StringIO()): 1090 self.readTest(f, zipfile.ZIP_STORED) 1091 1092 def testReadlineStored(self): 1093 for f in (TESTFN2, TemporaryFile(), StringIO()): 1094 self.readlineTest(f, zipfile.ZIP_STORED) 1095 1096 def testReadlinesStored(self): 1097 for f in (TESTFN2, TemporaryFile(), StringIO()): 1098 self.readlinesTest(f, zipfile.ZIP_STORED) 1099 1100 def testIterlinesStored(self): 1101 for f in (TESTFN2, TemporaryFile(), StringIO()): 1102 self.iterlinesTest(f, zipfile.ZIP_STORED) 1103 1104 if zlib: 1105 def testReadDeflated(self): 1106 for f in (TESTFN2, TemporaryFile(), StringIO()): 1107 self.readTest(f, zipfile.ZIP_DEFLATED) 1108 1109 def testReadlineDeflated(self): 1110 for f in (TESTFN2, TemporaryFile(), StringIO()): 1111 self.readlineTest(f, zipfile.ZIP_DEFLATED) 1112 1113 def testReadlinesDeflated(self): 1114 for f in (TESTFN2, TemporaryFile(), StringIO()): 1115 self.readlinesTest(f, zipfile.ZIP_DEFLATED) 1116 1117 def testIterlinesDeflated(self): 1118 for f in (TESTFN2, TemporaryFile(), StringIO()): 1119 self.iterlinesTest(f, zipfile.ZIP_DEFLATED) 1472 with zipfile.ZipFile(f, "r") as zipfp: 1473 for sep, fn in self.arcfiles.items(): 1474 for line, zipline in zip(self.line_gen, zipfp.open(fn, "rU")): 1475 self.assertEqual(zipline, line + '\n') 1476 1477 def test_read_stored(self): 1478 for f in (TESTFN2, TemporaryFile(), StringIO()): 1479 self.read_test(f, zipfile.ZIP_STORED) 1480 1481 def test_readline_read_stored(self): 1482 # Issue #7610: calls to readline() interleaved with calls to read(). 1483 for f in (TESTFN2, TemporaryFile(), StringIO()): 1484 self.readline_read_test(f, zipfile.ZIP_STORED) 1485 1486 def test_readline_stored(self): 1487 for f in (TESTFN2, TemporaryFile(), StringIO()): 1488 self.readline_test(f, zipfile.ZIP_STORED) 1489 1490 def test_readlines_stored(self): 1491 for f in (TESTFN2, TemporaryFile(), StringIO()): 1492 self.readlines_test(f, zipfile.ZIP_STORED) 1493 1494 def test_iterlines_stored(self): 1495 for f in (TESTFN2, TemporaryFile(), StringIO()): 1496 self.iterlines_test(f, zipfile.ZIP_STORED) 1497 1498 @skipUnless(zlib, "requires zlib") 1499 def test_read_deflated(self): 1500 for f in (TESTFN2, TemporaryFile(), StringIO()): 1501 self.read_test(f, zipfile.ZIP_DEFLATED) 1502 1503 @skipUnless(zlib, "requires zlib") 1504 def test_readline_read_deflated(self): 1505 # Issue #7610: calls to readline() interleaved with calls to read(). 1506 for f in (TESTFN2, TemporaryFile(), StringIO()): 1507 self.readline_read_test(f, zipfile.ZIP_DEFLATED) 1508 1509 @skipUnless(zlib, "requires zlib") 1510 def test_readline_deflated(self): 1511 for f in (TESTFN2, TemporaryFile(), StringIO()): 1512 self.readline_test(f, zipfile.ZIP_DEFLATED) 1513 1514 @skipUnless(zlib, "requires zlib") 1515 def test_readlines_deflated(self): 1516 for f in (TESTFN2, TemporaryFile(), StringIO()): 1517 self.readlines_test(f, zipfile.ZIP_DEFLATED) 1518 1519 @skipUnless(zlib, "requires zlib") 1520 def test_iterlines_deflated(self): 1521 for f in (TESTFN2, TemporaryFile(), StringIO()): 1522 self.iterlines_test(f, zipfile.ZIP_DEFLATED) 1120 1523 1121 1524 def tearDown(self): 1122 1525 for sep, fn in self.arcfiles.items(): 1123 1526 os.remove(fn) 1124 support.unlink(TESTFN)1125 support.unlink(TESTFN2)1527 unlink(TESTFN) 1528 unlink(TESTFN2) 1126 1529 1127 1530 … … 1129 1532 run_unittest(TestsWithSourceFile, TestZip64InSmallFiles, OtherTests, 1130 1533 PyZipFileTests, DecryptionTests, TestsWithMultipleOpens, 1131 TestWithDirectory, 1132 UniversalNewlineTests,TestsWithRandomBinaryFiles)1534 TestWithDirectory, UniversalNewlineTests, 1535 TestsWithRandomBinaryFiles) 1133 1536 1134 1537 if __name__ == "__main__":
Note:
See TracChangeset
for help on using the changeset viewer.