Skip to content

Commit 027f95c

Browse files
bpo-33744: Fix test_uu. (GH-7350)
Separate tests leaked files or were depended on files leaked in other tests.
1 parent a801cf1 commit 027f95c

File tree

1 file changed

+26
-78
lines changed

1 file changed

+26
-78
lines changed

Lib/test/test_uu.py

Lines changed: 26 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import unittest
77
from test import support
88

9-
import sys, os
9+
import sys
1010
import uu
1111
import io
1212

@@ -162,113 +162,61 @@ def test_decode(self):
162162

163163
class UUFileTest(unittest.TestCase):
164164

165-
def _kill(self, f):
166-
# close and remove file
167-
if f is None:
168-
return
169-
try:
170-
f.close()
171-
except (SystemExit, KeyboardInterrupt):
172-
raise
173-
except:
174-
pass
175-
try:
176-
os.unlink(f.name)
177-
except (SystemExit, KeyboardInterrupt):
178-
raise
179-
except:
180-
pass
181-
182165
def setUp(self):
183166
self.tmpin = support.TESTFN + "i"
184167
self.tmpout = support.TESTFN + "o"
185-
186-
def tearDown(self):
187-
del self.tmpin
188-
del self.tmpout
168+
self.addCleanup(support.unlink, self.tmpin)
169+
self.addCleanup(support.unlink, self.tmpout)
189170

190171
def test_encode(self):
191-
fin = fout = None
192-
try:
193-
support.unlink(self.tmpin)
194-
fin = open(self.tmpin, 'wb')
172+
with open(self.tmpin, 'wb') as fin:
195173
fin.write(plaintext)
196-
fin.close()
197174

198-
fin = open(self.tmpin, 'rb')
199-
fout = open(self.tmpout, 'wb')
200-
uu.encode(fin, fout, self.tmpin, mode=0o644)
201-
fin.close()
202-
fout.close()
175+
with open(self.tmpin, 'rb') as fin:
176+
with open(self.tmpout, 'wb') as fout:
177+
uu.encode(fin, fout, self.tmpin, mode=0o644)
203178

204-
fout = open(self.tmpout, 'rb')
179+
with open(self.tmpout, 'rb') as fout:
205180
s = fout.read()
206-
fout.close()
207-
self.assertEqual(s, encodedtextwrapped(0o644, self.tmpin))
181+
self.assertEqual(s, encodedtextwrapped(0o644, self.tmpin))
208182

209-
# in_file and out_file as filenames
210-
uu.encode(self.tmpin, self.tmpout, self.tmpin, mode=0o644)
211-
fout = open(self.tmpout, 'rb')
183+
# in_file and out_file as filenames
184+
uu.encode(self.tmpin, self.tmpout, self.tmpin, mode=0o644)
185+
with open(self.tmpout, 'rb') as fout:
212186
s = fout.read()
213-
fout.close()
214-
self.assertEqual(s, encodedtextwrapped(0o644, self.tmpin))
215-
216-
finally:
217-
self._kill(fin)
218-
self._kill(fout)
187+
self.assertEqual(s, encodedtextwrapped(0o644, self.tmpin))
219188

220189
def test_decode(self):
221-
f = None
222-
try:
223-
support.unlink(self.tmpin)
224-
f = open(self.tmpin, 'wb')
190+
with open(self.tmpin, 'wb') as f:
225191
f.write(encodedtextwrapped(0o644, self.tmpout))
226-
f.close()
227192

228-
f = open(self.tmpin, 'rb')
193+
with open(self.tmpin, 'rb') as f:
229194
uu.decode(f)
230-
f.close()
231195

232-
f = open(self.tmpout, 'rb')
196+
with open(self.tmpout, 'rb') as f:
233197
s = f.read()
234-
f.close()
235-
self.assertEqual(s, plaintext)
236-
# XXX is there an xp way to verify the mode?
237-
finally:
238-
self._kill(f)
198+
self.assertEqual(s, plaintext)
199+
# XXX is there an xp way to verify the mode?
239200

240201
def test_decode_filename(self):
241-
f = None
242-
try:
243-
support.unlink(self.tmpin)
244-
f = open(self.tmpin, 'wb')
202+
with open(self.tmpin, 'wb') as f:
245203
f.write(encodedtextwrapped(0o644, self.tmpout))
246-
f.close()
247204

248-
uu.decode(self.tmpin)
205+
uu.decode(self.tmpin)
249206

250-
f = open(self.tmpout, 'rb')
207+
with open(self.tmpout, 'rb') as f:
251208
s = f.read()
252-
f.close()
253-
self.assertEqual(s, plaintext)
254-
finally:
255-
self._kill(f)
209+
self.assertEqual(s, plaintext)
256210

257211
def test_decodetwice(self):
258212
# Verify that decode() will refuse to overwrite an existing file
259-
f = None
260-
try:
261-
f = io.BytesIO(encodedtextwrapped(0o644, self.tmpout))
262-
263-
f = open(self.tmpin, 'rb')
213+
with open(self.tmpin, 'wb') as f:
214+
f.write(encodedtextwrapped(0o644, self.tmpout))
215+
with open(self.tmpin, 'rb') as f:
264216
uu.decode(f)
265-
f.close()
266217

267-
f = open(self.tmpin, 'rb')
218+
with open(self.tmpin, 'rb') as f:
268219
self.assertRaises(uu.Error, uu.decode, f)
269-
f.close()
270-
finally:
271-
self._kill(f)
272220

273221

274222
if __name__=="__main__":

0 commit comments

Comments
 (0)