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

Skip to content

Commit 8fb9b86

Browse files
committed
Fix #8886. Use context managers throughout the test.
1 parent a47bbf5 commit 8fb9b86

2 files changed

Lines changed: 113 additions & 105 deletions

File tree

Lib/test/test_zipfile.py

Lines changed: 107 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -112,20 +112,20 @@ def zip_open_test(self, f, compression):
112112
# Read the ZIP archive
113113
with zipfile.ZipFile(f, "r", compression) as zipfp:
114114
zipdata1 = []
115-
zipopen1 = zipfp.open(TESTFN)
116-
while True:
117-
read_data = zipopen1.read(256)
118-
if not read_data:
119-
break
120-
zipdata1.append(read_data)
115+
with zipfp.open(TESTFN) as zipopen1:
116+
while True:
117+
read_data = zipopen1.read(256)
118+
if not read_data:
119+
break
120+
zipdata1.append(read_data)
121121

122122
zipdata2 = []
123-
zipopen2 = zipfp.open("another.name")
124-
while True:
125-
read_data = zipopen2.read(256)
126-
if not read_data:
127-
break
128-
zipdata2.append(read_data)
123+
with zipfp.open("another.name") as zipopen2:
124+
while True:
125+
read_data = zipopen2.read(256)
126+
if not read_data:
127+
break
128+
zipdata2.append(read_data)
129129

130130
self.assertEqual(b''.join(zipdata1), self.data)
131131
self.assertEqual(b''.join(zipdata2), self.data)
@@ -146,7 +146,8 @@ def test_open_via_zip_info(self):
146146
infos = zipfp.infolist()
147147
data = b""
148148
for info in infos:
149-
data += zipfp.open(info).read()
149+
with zipfp.open(info) as zipopen:
150+
data += zipopen.read()
150151
self.assertTrue(data == b"foobar" or data == b"barfoo")
151152
data = b""
152153
for info in infos:
@@ -159,12 +160,12 @@ def zip_random_open_test(self, f, compression):
159160
# Read the ZIP archive
160161
with zipfile.ZipFile(f, "r", compression) as zipfp:
161162
zipdata1 = []
162-
zipopen1 = zipfp.open(TESTFN)
163-
while True:
164-
read_data = zipopen1.read(randint(1, 1024))
165-
if not read_data:
166-
break
167-
zipdata1.append(read_data)
163+
with zipfp.open(TESTFN) as zipopen1:
164+
while True:
165+
read_data = zipopen1.read(randint(1, 1024))
166+
if not read_data:
167+
break
168+
zipdata1.append(read_data)
168169

169170
self.assertEqual(b''.join(zipdata1), self.data)
170171
if not isinstance(f, str):
@@ -184,9 +185,9 @@ def test_univeral_readaheads(self):
184185

185186
data2 = b''
186187
zipfp = zipfile.ZipFile(f, 'r')
187-
zipopen = zipfp.open(TESTFN, 'rU')
188-
for line in zipopen:
189-
data2 += line
188+
with zipfp.open(TESTFN, 'rU') as zipopen:
189+
for line in zipopen:
190+
data2 += line
190191
zipfp.close()
191192

192193
self.assertEqual(data, data2.replace(b'\n', b'\r\n'))
@@ -196,19 +197,18 @@ def zip_readline_read_test(self, f, compression):
196197

197198
# Read the ZIP archive
198199
zipfp = zipfile.ZipFile(f, "r")
199-
zipopen = zipfp.open(TESTFN)
200-
201-
data = b''
202-
while True:
203-
read = zipopen.readline()
204-
if not read:
205-
break
206-
data += read
200+
with zipfp.open(TESTFN) as zipopen:
201+
data = b''
202+
while True:
203+
read = zipopen.readline()
204+
if not read:
205+
break
206+
data += read
207207

208-
read = zipopen.read(100)
209-
if not read:
210-
break
211-
data += read
208+
read = zipopen.read(100)
209+
if not read:
210+
break
211+
data += read
212212

213213
self.assertEqual(data, self.data)
214214
zipfp.close()
@@ -220,10 +220,10 @@ def zip_readline_test(self, f, compression):
220220

221221
# Read the ZIP archive
222222
with zipfile.ZipFile(f, "r") as zipfp:
223-
zipopen = zipfp.open(TESTFN)
224-
for line in self.line_gen:
225-
linedata = zipopen.readline()
226-
self.assertEqual(linedata, line + '\n')
223+
with zipfp.open(TESTFN) as zipopen:
224+
for line in self.line_gen:
225+
linedata = zipopen.readline()
226+
self.assertEqual(linedata, line + '\n')
227227
if not isinstance(f, str):
228228
f.close()
229229

@@ -232,7 +232,8 @@ def zip_readlines_test(self, f, compression):
232232

233233
# Read the ZIP archive
234234
with zipfile.ZipFile(f, "r") as zipfp:
235-
ziplines = zipfp.open(TESTFN).readlines()
235+
with zipfp.open(TESTFN) as zipopen:
236+
ziplines = zipopen.readlines()
236237
for line, zipline in zip(self.line_gen, ziplines):
237238
self.assertEqual(zipline, line + '\n')
238239
if not isinstance(f, str):
@@ -243,8 +244,9 @@ def zip_iterlines_test(self, f, compression):
243244

244245
# Read the ZIP archive
245246
with zipfile.ZipFile(f, "r") as zipfp:
246-
for line, zipline in zip(self.line_gen, zipfp.open(TESTFN)):
247-
self.assertEqual(zipline, line + '\n')
247+
with zipfp.open(TESTFN) as zipopen:
248+
for line, zipline in zip(self.line_gen, zipopen):
249+
self.assertEqual(zipline, line + '\n')
248250
if not isinstance(f, str):
249251
f.close()
250252

@@ -311,9 +313,9 @@ def test_low_compression(self):
311313

312314
# Get an open object for strfile
313315
with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_DEFLATED) as zipfp:
314-
openobj = zipfp.open("strfile")
315-
self.assertEqual(openobj.read(1), b'1')
316-
self.assertEqual(openobj.read(1), b'2')
316+
with zipfp.open("strfile") as openobj:
317+
self.assertEqual(openobj.read(1), b'1')
318+
self.assertEqual(openobj.read(1), b'2')
317319

318320
def test_absolute_arcnames(self):
319321
with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
@@ -352,7 +354,8 @@ def test_write_default_name(self):
352354
produces the expected result."""
353355
with zipfile.ZipFile(TESTFN2, "w") as zipfp:
354356
zipfp.write(TESTFN)
355-
self.assertEqual(zipfp.read(TESTFN), open(TESTFN, "rb").read())
357+
with open(TESTFN, "rb") as f:
358+
self.assertEqual(zipfp.read(TESTFN), f.read())
356359

357360
@skipUnless(zlib, "requires zlib")
358361
def test_per_file_compression(self):
@@ -394,7 +397,8 @@ def test_extract(self):
394397
self.assertEqual(writtenfile, correctfile)
395398

396399
# make sure correct data is in correct file
397-
self.assertEqual(fdata.encode(), open(writtenfile, "rb").read())
400+
with open(writtenfile, "rb") as f:
401+
self.assertEqual(fdata.encode(), f.read())
398402

399403
os.remove(writtenfile)
400404

@@ -414,7 +418,8 @@ def test_extract_all(self):
414418
else:
415419
outfile = os.path.join(os.getcwd(), fpath)
416420

417-
self.assertEqual(fdata.encode(), open(outfile, "rb").read())
421+
with open(outfile, "rb") as f:
422+
self.assertEqual(fdata.encode(), f.read())
418423

419424
os.remove(outfile)
420425

@@ -674,7 +679,8 @@ def test_write_python_directory(self):
674679

675680
def test_write_non_pyfile(self):
676681
with zipfile.PyZipFile(TemporaryFile(), "w") as zipfp:
677-
open(TESTFN, 'w').write('most definitely not a python file')
682+
with open(TESTFN, 'w') as f:
683+
f.write('most definitely not a python file')
678684
self.assertRaises(RuntimeError, zipfp.writepy, TESTFN)
679685
os.remove(TESTFN)
680686

@@ -825,7 +831,8 @@ def test_closed_zip_raises_RuntimeError(self):
825831
self.assertRaises(RuntimeError, zipf.open, "foo.txt")
826832
self.assertRaises(RuntimeError, zipf.testzip)
827833
self.assertRaises(RuntimeError, zipf.writestr, "bogus.txt", "bogus")
828-
open(TESTFN, 'w').write('zipfile test data')
834+
with open(TESTFN, 'w') as f:
835+
f.write('zipfile test data')
829836
self.assertRaises(RuntimeError, zipf.write, TESTFN)
830837

831838
def test_bad_constructor_mode(self):
@@ -848,11 +855,11 @@ def test_read0(self):
848855
with zipfile.ZipFile(TESTFN, mode="w") as zipf:
849856
zipf.writestr("foo.txt", "O, for a Muse of Fire!")
850857
# read the data to make sure the file is there
851-
f = zipf.open("foo.txt")
852-
for i in range(FIXEDTEST_SIZE):
853-
self.assertEqual(f.read(0), b'')
858+
with zipf.open("foo.txt") as f:
859+
for i in range(FIXEDTEST_SIZE):
860+
self.assertEqual(f.read(0), b'')
854861

855-
self.assertEqual(f.read(), b"O, for a Muse of Fire!")
862+
self.assertEqual(f.read(), b"O, for a Muse of Fire!")
856863

857864
def test_open_non_existent_item(self):
858865
"""Check that attempting to call open() for an item that doesn't
@@ -1115,20 +1122,20 @@ def zip_open_test(self, f, compression):
11151122
# Read the ZIP archive
11161123
with zipfile.ZipFile(f, "r", compression) as zipfp:
11171124
zipdata1 = []
1118-
zipopen1 = zipfp.open(TESTFN)
1119-
while True:
1120-
read_data = zipopen1.read(256)
1121-
if not read_data:
1122-
break
1123-
zipdata1.append(read_data)
1125+
with zipfp.open(TESTFN) as zipopen1:
1126+
while True:
1127+
read_data = zipopen1.read(256)
1128+
if not read_data:
1129+
break
1130+
zipdata1.append(read_data)
11241131

11251132
zipdata2 = []
1126-
zipopen2 = zipfp.open("another.name")
1127-
while True:
1128-
read_data = zipopen2.read(256)
1129-
if not read_data:
1130-
break
1131-
zipdata2.append(read_data)
1133+
with zipfp.open("another.name") as zipopen2:
1134+
while True:
1135+
read_data = zipopen2.read(256)
1136+
if not read_data:
1137+
break
1138+
zipdata2.append(read_data)
11321139

11331140
testdata1 = b''.join(zipdata1)
11341141
self.assertEqual(len(testdata1), len(self.data))
@@ -1155,12 +1162,12 @@ def zip_random_open_test(self, f, compression):
11551162
# Read the ZIP archive
11561163
with zipfile.ZipFile(f, "r", compression) as zipfp:
11571164
zipdata1 = []
1158-
zipopen1 = zipfp.open(TESTFN)
1159-
while True:
1160-
read_data = zipopen1.read(randint(1, 1024))
1161-
if not read_data:
1162-
break
1163-
zipdata1.append(read_data)
1165+
with zipfp.open(TESTFN) as zipopen1:
1166+
while True:
1167+
read_data = zipopen1.read(randint(1, 1024))
1168+
if not read_data:
1169+
break
1170+
zipdata1.append(read_data)
11641171

11651172
testdata = b''.join(zipdata1)
11661173
self.assertEqual(len(testdata), len(self.data))
@@ -1190,37 +1197,34 @@ def test_same_file(self):
11901197
# Verify that (when the ZipFile is in control of creating file objects)
11911198
# multiple open() calls can be made without interfering with each other.
11921199
with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
1193-
zopen1 = zipf.open('ones')
1194-
zopen2 = zipf.open('ones')
1195-
data1 = zopen1.read(500)
1196-
data2 = zopen2.read(500)
1197-
data1 += zopen1.read(500)
1198-
data2 += zopen2.read(500)
1200+
with zipf.open('ones') as zopen1, zipf.open('ones') as zopen2:
1201+
data1 = zopen1.read(500)
1202+
data2 = zopen2.read(500)
1203+
data1 += zopen1.read(500)
1204+
data2 += zopen2.read(500)
11991205
self.assertEqual(data1, data2)
12001206

12011207
def test_different_file(self):
12021208
# Verify that (when the ZipFile is in control of creating file objects)
12031209
# multiple open() calls can be made without interfering with each other.
12041210
with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
1205-
zopen1 = zipf.open('ones')
1206-
zopen2 = zipf.open('twos')
1207-
data1 = zopen1.read(500)
1208-
data2 = zopen2.read(500)
1209-
data1 += zopen1.read(500)
1210-
data2 += zopen2.read(500)
1211+
with zipf.open('ones') as zopen1, zipf.open('twos') as zopen2:
1212+
data1 = zopen1.read(500)
1213+
data2 = zopen2.read(500)
1214+
data1 += zopen1.read(500)
1215+
data2 += zopen2.read(500)
12111216
self.assertEqual(data1, b'1'*FIXEDTEST_SIZE)
12121217
self.assertEqual(data2, b'2'*FIXEDTEST_SIZE)
12131218

12141219
def test_interleaved(self):
12151220
# Verify that (when the ZipFile is in control of creating file objects)
12161221
# multiple open() calls can be made without interfering with each other.
12171222
with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
1218-
zopen1 = zipf.open('ones')
1219-
data1 = zopen1.read(500)
1220-
zopen2 = zipf.open('twos')
1221-
data2 = zopen2.read(500)
1222-
data1 += zopen1.read(500)
1223-
data2 += zopen2.read(500)
1223+
with zipf.open('ones') as zopen1, zipf.open('twos') as zopen2:
1224+
data1 = zopen1.read(500)
1225+
data2 = zopen2.read(500)
1226+
data1 += zopen1.read(500)
1227+
data2 += zopen2.read(500)
12241228
self.assertEqual(data1, b'1'*FIXEDTEST_SIZE)
12251229
self.assertEqual(data2, b'2'*FIXEDTEST_SIZE)
12261230

@@ -1294,25 +1298,23 @@ def readline_read_test(self, f, compression):
12941298
self.make_test_archive(f, compression)
12951299

12961300
# Read the ZIP archive
1297-
zipfp = zipfile.ZipFile(f, "r")
1298-
for sep, fn in self.arcfiles.items():
1299-
zipopen = zipfp.open(fn, "rU")
1300-
data = b''
1301-
while True:
1302-
read = zipopen.readline()
1303-
if not read:
1304-
break
1305-
data += read
1306-
1307-
read = zipopen.read(5)
1308-
if not read:
1309-
break
1310-
data += read
1301+
with zipfile.ZipFile(f, "r") as zipfp:
1302+
for sep, fn in self.arcfiles.items():
1303+
with zipfp.open(fn, "rU") as zipopen:
1304+
data = b''
1305+
while True:
1306+
read = zipopen.readline()
1307+
if not read:
1308+
break
1309+
data += read
1310+
1311+
read = zipopen.read(5)
1312+
if not read:
1313+
break
1314+
data += read
13111315

1312-
zipopen.close()
13131316
self.assertEqual(data, self.arcdata['\n'])
13141317

1315-
zipfp.close()
13161318
if not isinstance(f, str):
13171319
f.close()
13181320

Misc/NEWS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ C-API
3131

3232
- Loosen PyArg_ValidateKeywordArguments to allow dict subclasses.
3333

34+
Tests
35+
-----
36+
37+
- Issue #8886: Use context managers throughout test_zipfile. Patch by
38+
Eric Carstensen.
39+
3440

3541
What's New in Python 3.2 Alpha 4?
3642
=================================

0 commit comments

Comments
 (0)