|
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 |
|
@@ -142,113 +142,61 @@ def test_decode(self):
|
142 | 142 |
|
143 | 143 | class UUFileTest(unittest.TestCase):
|
144 | 144 |
|
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 |
| - |
162 | 145 | def setUp(self):
|
163 | 146 | self.tmpin = support.TESTFN + "i"
|
164 | 147 | 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) |
169 | 150 |
|
170 | 151 | 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: |
175 | 153 | fin.write(plaintext)
|
176 |
| - fin.close() |
177 | 154 |
|
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) |
183 | 158 |
|
184 |
| - fout = open(self.tmpout, 'rb') |
| 159 | + with open(self.tmpout, 'rb') as fout: |
185 | 160 | s = fout.read()
|
186 |
| - fout.close() |
187 |
| - self.assertEqual(s, encodedtextwrapped(0o644, self.tmpin)) |
| 161 | + self.assertEqual(s, encodedtextwrapped(0o644, self.tmpin)) |
188 | 162 |
|
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: |
192 | 166 | 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)) |
199 | 168 |
|
200 | 169 | 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: |
205 | 171 | f.write(encodedtextwrapped(0o644, self.tmpout))
|
206 |
| - f.close() |
207 | 172 |
|
208 |
| - f = open(self.tmpin, 'rb') |
| 173 | + with open(self.tmpin, 'rb') as f: |
209 | 174 | uu.decode(f)
|
210 |
| - f.close() |
211 | 175 |
|
212 |
| - f = open(self.tmpout, 'rb') |
| 176 | + with open(self.tmpout, 'rb') as f: |
213 | 177 | 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? |
219 | 180 |
|
220 | 181 | 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: |
225 | 183 | f.write(encodedtextwrapped(0o644, self.tmpout))
|
226 |
| - f.close() |
227 | 184 |
|
228 |
| - uu.decode(self.tmpin) |
| 185 | + uu.decode(self.tmpin) |
229 | 186 |
|
230 |
| - f = open(self.tmpout, 'rb') |
| 187 | + with open(self.tmpout, 'rb') as f: |
231 | 188 | s = f.read()
|
232 |
| - f.close() |
233 |
| - self.assertEqual(s, plaintext) |
234 |
| - finally: |
235 |
| - self._kill(f) |
| 189 | + self.assertEqual(s, plaintext) |
236 | 190 |
|
237 | 191 | def test_decodetwice(self):
|
238 | 192 | # 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: |
244 | 196 | uu.decode(f)
|
245 |
| - f.close() |
246 | 197 |
|
247 |
| - f = open(self.tmpin, 'rb') |
| 198 | + with open(self.tmpin, 'rb') as f: |
248 | 199 | self.assertRaises(uu.Error, uu.decode, f)
|
249 |
| - f.close() |
250 |
| - finally: |
251 |
| - self._kill(f) |
252 | 200 |
|
253 | 201 | def test_main():
|
254 | 202 | support.run_unittest(UUTest,
|
|
0 commit comments