Skip to content

Commit da7f8ce

Browse files
bpo-33744: Fix test_uu. (GH-7350) (GH-7353)
Separate tests leaked files or were depended on files leaked in other tests. (cherry picked from commit 027f95c) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent db30390 commit da7f8ce

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

@@ -142,113 +142,61 @@ def test_decode(self):
142142

143143
class UUFileTest(unittest.TestCase):
144144

145-
def _kill(self, f):
146-
# close and remove file
147-
if f is None:
148-
return
149-
try:
150-
f.close()
151-
except (SystemExit, KeyboardInterrupt):
152-
raise
153-
except:
154-
pass
155-
try:
156-
os.unlink(f.name)
157-
except (SystemExit, KeyboardInterrupt):
158-
raise
159-
except:
160-
pass
161-
162145
def setUp(self):
163146
self.tmpin = support.TESTFN + "i"
164147
self.tmpout = support.TESTFN + "o"
165-
166-
def tearDown(self):
167-
del self.tmpin
168-
del self.tmpout
148+
self.addCleanup(support.unlink, self.tmpin)
149+
self.addCleanup(support.unlink, self.tmpout)
169150

170151
def test_encode(self):
171-
fin = fout = None
172-
try:
173-
support.unlink(self.tmpin)
174-
fin = open(self.tmpin, 'wb')
152+
with open(self.tmpin, 'wb') as fin:
175153
fin.write(plaintext)
176-
fin.close()
177154

178-
fin = open(self.tmpin, 'rb')
179-
fout = open(self.tmpout, 'wb')
180-
uu.encode(fin, fout, self.tmpin, mode=0o644)
181-
fin.close()
182-
fout.close()
155+
with open(self.tmpin, 'rb') as fin:
156+
with open(self.tmpout, 'wb') as fout:
157+
uu.encode(fin, fout, self.tmpin, mode=0o644)
183158

184-
fout = open(self.tmpout, 'rb')
159+
with open(self.tmpout, 'rb') as fout:
185160
s = fout.read()
186-
fout.close()
187-
self.assertEqual(s, encodedtextwrapped(0o644, self.tmpin))
161+
self.assertEqual(s, encodedtextwrapped(0o644, self.tmpin))
188162

189-
# in_file and out_file as filenames
190-
uu.encode(self.tmpin, self.tmpout, self.tmpin, mode=0o644)
191-
fout = open(self.tmpout, 'rb')
163+
# in_file and out_file as filenames
164+
uu.encode(self.tmpin, self.tmpout, self.tmpin, mode=0o644)
165+
with open(self.tmpout, 'rb') as fout:
192166
s = fout.read()
193-
fout.close()
194-
self.assertEqual(s, encodedtextwrapped(0o644, self.tmpin))
195-
196-
finally:
197-
self._kill(fin)
198-
self._kill(fout)
167+
self.assertEqual(s, encodedtextwrapped(0o644, self.tmpin))
199168

200169
def test_decode(self):
201-
f = None
202-
try:
203-
support.unlink(self.tmpin)
204-
f = open(self.tmpin, 'wb')
170+
with open(self.tmpin, 'wb') as f:
205171
f.write(encodedtextwrapped(0o644, self.tmpout))
206-
f.close()
207172

208-
f = open(self.tmpin, 'rb')
173+
with open(self.tmpin, 'rb') as f:
209174
uu.decode(f)
210-
f.close()
211175

212-
f = open(self.tmpout, 'rb')
176+
with open(self.tmpout, 'rb') as f:
213177
s = f.read()
214-
f.close()
215-
self.assertEqual(s, plaintext)
216-
# XXX is there an xp way to verify the mode?
217-
finally:
218-
self._kill(f)
178+
self.assertEqual(s, plaintext)
179+
# XXX is there an xp way to verify the mode?
219180

220181
def test_decode_filename(self):
221-
f = None
222-
try:
223-
support.unlink(self.tmpin)
224-
f = open(self.tmpin, 'wb')
182+
with open(self.tmpin, 'wb') as f:
225183
f.write(encodedtextwrapped(0o644, self.tmpout))
226-
f.close()
227184

228-
uu.decode(self.tmpin)
185+
uu.decode(self.tmpin)
229186

230-
f = open(self.tmpout, 'rb')
187+
with open(self.tmpout, 'rb') as f:
231188
s = f.read()
232-
f.close()
233-
self.assertEqual(s, plaintext)
234-
finally:
235-
self._kill(f)
189+
self.assertEqual(s, plaintext)
236190

237191
def test_decodetwice(self):
238192
# Verify that decode() will refuse to overwrite an existing file
239-
f = None
240-
try:
241-
f = io.BytesIO(encodedtextwrapped(0o644, self.tmpout))
242-
243-
f = open(self.tmpin, 'rb')
193+
with open(self.tmpin, 'wb') as f:
194+
f.write(encodedtextwrapped(0o644, self.tmpout))
195+
with open(self.tmpin, 'rb') as f:
244196
uu.decode(f)
245-
f.close()
246197

247-
f = open(self.tmpin, 'rb')
198+
with open(self.tmpin, 'rb') as f:
248199
self.assertRaises(uu.Error, uu.decode, f)
249-
f.close()
250-
finally:
251-
self._kill(f)
252200

253201
def test_main():
254202
support.run_unittest(UUTest,

0 commit comments

Comments
 (0)