|
4 | 4 | """
|
5 | 5 |
|
6 | 6 | import unittest
|
7 |
| -from test import test_support |
| 7 | +from test import test_support as support |
8 | 8 |
|
9 |
| -import sys, os, uu, cStringIO |
| 9 | +import cStringIO |
| 10 | +import sys |
10 | 11 | import uu
|
11 | 12 |
|
12 | 13 | plaintext = "The smooth-scaled python crept over the sleeping dog\n"
|
@@ -108,114 +109,64 @@ def test_decode(self):
|
108 | 109 |
|
109 | 110 | class UUFileTest(unittest.TestCase):
|
110 | 111 |
|
111 |
| - def _kill(self, f): |
112 |
| - # close and remove file |
113 |
| - try: |
114 |
| - f.close() |
115 |
| - except (SystemExit, KeyboardInterrupt): |
116 |
| - raise |
117 |
| - except: |
118 |
| - pass |
119 |
| - try: |
120 |
| - os.unlink(f.name) |
121 |
| - except (SystemExit, KeyboardInterrupt): |
122 |
| - raise |
123 |
| - except: |
124 |
| - pass |
125 |
| - |
126 | 112 | def setUp(self):
|
127 |
| - self.tmpin = test_support.TESTFN + "i" |
128 |
| - self.tmpout = test_support.TESTFN + "o" |
129 |
| - |
130 |
| - def tearDown(self): |
131 |
| - del self.tmpin |
132 |
| - del self.tmpout |
| 113 | + self.tmpin = support.TESTFN + "i" |
| 114 | + self.tmpout = support.TESTFN + "o" |
| 115 | + self.addCleanup(support.unlink, self.tmpin) |
| 116 | + self.addCleanup(support.unlink, self.tmpout) |
133 | 117 |
|
134 | 118 | def test_encode(self):
|
135 |
| - fin = fout = None |
136 |
| - try: |
137 |
| - test_support.unlink(self.tmpin) |
138 |
| - fin = open(self.tmpin, 'wb') |
| 119 | + with open(self.tmpin, 'wb') as fin: |
139 | 120 | fin.write(plaintext)
|
140 |
| - fin.close() |
141 | 121 |
|
142 |
| - fin = open(self.tmpin, 'rb') |
143 |
| - fout = open(self.tmpout, 'w') |
144 |
| - uu.encode(fin, fout, self.tmpin, mode=0644) |
145 |
| - fin.close() |
146 |
| - fout.close() |
| 122 | + with open(self.tmpin, 'rb') as fin: |
| 123 | + with open(self.tmpout, 'w') as fout: |
| 124 | + uu.encode(fin, fout, self.tmpin, mode=0o644) |
147 | 125 |
|
148 |
| - fout = open(self.tmpout, 'r') |
| 126 | + with open(self.tmpout, 'r') as fout: |
149 | 127 | s = fout.read()
|
150 |
| - fout.close() |
151 |
| - self.assertEqual(s, encodedtextwrapped % (0644, self.tmpin)) |
| 128 | + self.assertEqual(s, encodedtextwrapped % (0o644, self.tmpin)) |
152 | 129 |
|
153 |
| - # in_file and out_file as filenames |
154 |
| - uu.encode(self.tmpin, self.tmpout, self.tmpin, mode=0644) |
155 |
| - fout = open(self.tmpout, 'r') |
| 130 | + # in_file and out_file as filenames |
| 131 | + uu.encode(self.tmpin, self.tmpout, self.tmpin, mode=0o644) |
| 132 | + with open(self.tmpout, 'r') as fout: |
156 | 133 | s = fout.read()
|
157 |
| - fout.close() |
158 |
| - self.assertEqual(s, encodedtextwrapped % (0644, self.tmpin)) |
159 |
| - |
160 |
| - finally: |
161 |
| - self._kill(fin) |
162 |
| - self._kill(fout) |
| 134 | + self.assertEqual(s, encodedtextwrapped % (0o644, self.tmpin)) |
163 | 135 |
|
164 | 136 | def test_decode(self):
|
165 |
| - f = None |
166 |
| - try: |
167 |
| - test_support.unlink(self.tmpin) |
168 |
| - f = open(self.tmpin, 'w') |
169 |
| - f.write(encodedtextwrapped % (0644, self.tmpout)) |
170 |
| - f.close() |
| 137 | + with open(self.tmpin, 'w') as f: |
| 138 | + f.write(encodedtextwrapped % (0o644, self.tmpout)) |
171 | 139 |
|
172 |
| - f = open(self.tmpin, 'r') |
| 140 | + with open(self.tmpin, 'r') as f: |
173 | 141 | uu.decode(f)
|
174 |
| - f.close() |
175 | 142 |
|
176 |
| - f = open(self.tmpout, 'r') |
| 143 | + with open(self.tmpout, 'r') as f: |
177 | 144 | s = f.read()
|
178 |
| - f.close() |
179 |
| - self.assertEqual(s, plaintext) |
180 |
| - # XXX is there an xp way to verify the mode? |
181 |
| - finally: |
182 |
| - self._kill(f) |
| 145 | + self.assertEqual(s, plaintext) |
| 146 | + # XXX is there an xp way to verify the mode? |
183 | 147 |
|
184 | 148 | def test_decode_filename(self):
|
185 |
| - f = None |
186 |
| - try: |
187 |
| - test_support.unlink(self.tmpin) |
188 |
| - f = open(self.tmpin, 'w') |
189 |
| - f.write(encodedtextwrapped % (0644, self.tmpout)) |
190 |
| - f.close() |
| 149 | + with open(self.tmpin, 'w') as f: |
| 150 | + f.write(encodedtextwrapped % (0o644, self.tmpout)) |
191 | 151 |
|
192 |
| - uu.decode(self.tmpin) |
| 152 | + uu.decode(self.tmpin) |
193 | 153 |
|
194 |
| - f = open(self.tmpout, 'r') |
| 154 | + with open(self.tmpout, 'r') as f: |
195 | 155 | s = f.read()
|
196 |
| - f.close() |
197 |
| - self.assertEqual(s, plaintext) |
198 |
| - finally: |
199 |
| - self._kill(f) |
| 156 | + self.assertEqual(s, plaintext) |
200 | 157 |
|
201 | 158 | def test_decodetwice(self):
|
202 | 159 | # Verify that decode() will refuse to overwrite an existing file
|
203 |
| - f = None |
204 |
| - try: |
205 |
| - f = cStringIO.StringIO(encodedtextwrapped % (0644, self.tmpout)) |
206 |
| - |
207 |
| - f = open(self.tmpin, 'r') |
| 160 | + with open(self.tmpin, 'wb') as f: |
| 161 | + f.write(encodedtextwrapped % (0o644, self.tmpout)) |
| 162 | + with open(self.tmpin, 'r') as f: |
208 | 163 | uu.decode(f)
|
209 |
| - f.close() |
210 | 164 |
|
211 |
| - f = open(self.tmpin, 'r') |
| 165 | + with open(self.tmpin, 'r') as f: |
212 | 166 | self.assertRaises(uu.Error, uu.decode, f)
|
213 |
| - f.close() |
214 |
| - finally: |
215 |
| - self._kill(f) |
216 | 167 |
|
217 | 168 | def test_main():
|
218 |
| - test_support.run_unittest(UUTest, UUStdIOTest, UUFileTest) |
| 169 | + support.run_unittest(UUTest, UUStdIOTest, UUFileTest) |
219 | 170 |
|
220 | 171 | if __name__=="__main__":
|
221 | 172 | test_main()
|
0 commit comments