|
6 | 6 | import unittest
|
7 | 7 | from test import support
|
8 | 8 |
|
9 |
| -import sys, os |
| 9 | +import sys |
10 | 10 | import uu
|
11 | 11 | import io
|
12 | 12 |
|
@@ -162,113 +162,61 @@ def test_decode(self):
|
162 | 162 |
|
163 | 163 | class UUFileTest(unittest.TestCase):
|
164 | 164 |
|
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 |
| - |
182 | 165 | def setUp(self):
|
183 | 166 | self.tmpin = support.TESTFN + "i"
|
184 | 167 | 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) |
189 | 170 |
|
190 | 171 | 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: |
195 | 173 | fin.write(plaintext)
|
196 |
| - fin.close() |
197 | 174 |
|
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) |
203 | 178 |
|
204 |
| - fout = open(self.tmpout, 'rb') |
| 179 | + with open(self.tmpout, 'rb') as fout: |
205 | 180 | s = fout.read()
|
206 |
| - fout.close() |
207 |
| - self.assertEqual(s, encodedtextwrapped(0o644, self.tmpin)) |
| 181 | + self.assertEqual(s, encodedtextwrapped(0o644, self.tmpin)) |
208 | 182 |
|
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: |
212 | 186 | 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)) |
219 | 188 |
|
220 | 189 | 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: |
225 | 191 | f.write(encodedtextwrapped(0o644, self.tmpout))
|
226 |
| - f.close() |
227 | 192 |
|
228 |
| - f = open(self.tmpin, 'rb') |
| 193 | + with open(self.tmpin, 'rb') as f: |
229 | 194 | uu.decode(f)
|
230 |
| - f.close() |
231 | 195 |
|
232 |
| - f = open(self.tmpout, 'rb') |
| 196 | + with open(self.tmpout, 'rb') as f: |
233 | 197 | 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? |
239 | 200 |
|
240 | 201 | 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: |
245 | 203 | f.write(encodedtextwrapped(0o644, self.tmpout))
|
246 |
| - f.close() |
247 | 204 |
|
248 |
| - uu.decode(self.tmpin) |
| 205 | + uu.decode(self.tmpin) |
249 | 206 |
|
250 |
| - f = open(self.tmpout, 'rb') |
| 207 | + with open(self.tmpout, 'rb') as f: |
251 | 208 | s = f.read()
|
252 |
| - f.close() |
253 |
| - self.assertEqual(s, plaintext) |
254 |
| - finally: |
255 |
| - self._kill(f) |
| 209 | + self.assertEqual(s, plaintext) |
256 | 210 |
|
257 | 211 | def test_decodetwice(self):
|
258 | 212 | # 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: |
264 | 216 | uu.decode(f)
|
265 |
| - f.close() |
266 | 217 |
|
267 |
| - f = open(self.tmpin, 'rb') |
| 218 | + with open(self.tmpin, 'rb') as f: |
268 | 219 | self.assertRaises(uu.Error, uu.decode, f)
|
269 |
| - f.close() |
270 |
| - finally: |
271 |
| - self._kill(f) |
272 | 220 |
|
273 | 221 |
|
274 | 222 | if __name__=="__main__":
|
|
0 commit comments