12
12
import io
13
13
import _pyio as pyio
14
14
15
- from test .test_support import TESTFN , run_unittest
15
+ from test .support import TESTFN , run_unittest
16
+ from test import support
16
17
from UserList import UserList
17
18
18
19
class AutoFileTests (unittest .TestCase ):
@@ -24,7 +25,7 @@ def setUp(self):
24
25
def tearDown (self ):
25
26
if self .f :
26
27
self .f .close ()
27
- os . remove (TESTFN )
28
+ support . unlink (TESTFN )
28
29
29
30
def testWeakRefs (self ):
30
31
# verify weak references
@@ -143,8 +144,12 @@ class PyAutoFileTests(AutoFileTests):
143
144
144
145
class OtherFileTests (unittest .TestCase ):
145
146
147
+ def tearDown (self ):
148
+ support .unlink (TESTFN )
149
+
146
150
def testModeStrings (self ):
147
151
# check invalid mode strings
152
+ self .open (TESTFN , 'wb' ).close ()
148
153
for mode in ("" , "aU" , "wU+" ):
149
154
try :
150
155
f = self .open (TESTFN , mode )
@@ -191,7 +196,6 @@ def testTruncateOnWindows(self):
191
196
# SF bug <http://www.python.org/sf/801631>
192
197
# "file.truncate fault on windows"
193
198
194
- os .unlink (TESTFN )
195
199
f = self .open (TESTFN , 'wb' )
196
200
197
201
try :
@@ -215,7 +219,6 @@ def testTruncateOnWindows(self):
215
219
self .fail ("File size after ftruncate wrong %d" % size )
216
220
finally :
217
221
f .close ()
218
- os .unlink (TESTFN )
219
222
220
223
def testIteration (self ):
221
224
# Test the complex interaction when mixing file-iteration and the
@@ -236,86 +239,83 @@ def testIteration(self):
236
239
methods = [("readline" , ()), ("read" , ()), ("readlines" , ()),
237
240
("readinto" , (array ("b" , b" " * 100 ),))]
238
241
239
- try :
240
- # Prepare the testfile
241
- bag = self .open (TESTFN , "wb" )
242
- bag .write (filler * nchunks )
243
- bag .writelines (testlines )
244
- bag .close ()
245
- # Test for appropriate errors mixing read* and iteration
246
- for methodname , args in methods :
247
- f = self .open (TESTFN , 'rb' )
248
- if next (f ) != filler :
249
- self .fail , "Broken testfile"
250
- meth = getattr (f , methodname )
251
- meth (* args ) # This simply shouldn't fail
252
- f .close ()
253
-
254
- # Test to see if harmless (by accident) mixing of read* and
255
- # iteration still works. This depends on the size of the internal
256
- # iteration buffer (currently 8192,) but we can test it in a
257
- # flexible manner. Each line in the bag o' ham is 4 bytes
258
- # ("h", "a", "m", "\n"), so 4096 lines of that should get us
259
- # exactly on the buffer boundary for any power-of-2 buffersize
260
- # between 4 and 16384 (inclusive).
242
+ # Prepare the testfile
243
+ bag = self .open (TESTFN , "wb" )
244
+ bag .write (filler * nchunks )
245
+ bag .writelines (testlines )
246
+ bag .close ()
247
+ # Test for appropriate errors mixing read* and iteration
248
+ for methodname , args in methods :
261
249
f = self .open (TESTFN , 'rb' )
262
- for i in range (nchunks ):
263
- next (f )
264
- testline = testlines .pop (0 )
265
- try :
266
- line = f .readline ()
267
- except ValueError :
268
- self .fail ("readline() after next() with supposedly empty "
269
- "iteration-buffer failed anyway" )
270
- if line != testline :
271
- self .fail ("readline() after next() with empty buffer "
272
- "failed. Got %r, expected %r" % (line , testline ))
273
- testline = testlines .pop (0 )
274
- buf = array ("b" , b"\x00 " * len (testline ))
250
+ if next (f ) != filler :
251
+ self .fail , "Broken testfile"
252
+ meth = getattr (f , methodname )
253
+ meth (* args ) # This simply shouldn't fail
254
+ f .close ()
255
+
256
+ # Test to see if harmless (by accident) mixing of read* and
257
+ # iteration still works. This depends on the size of the internal
258
+ # iteration buffer (currently 8192,) but we can test it in a
259
+ # flexible manner. Each line in the bag o' ham is 4 bytes
260
+ # ("h", "a", "m", "\n"), so 4096 lines of that should get us
261
+ # exactly on the buffer boundary for any power-of-2 buffersize
262
+ # between 4 and 16384 (inclusive).
263
+ f = self .open (TESTFN , 'rb' )
264
+ for i in range (nchunks ):
265
+ next (f )
266
+ testline = testlines .pop (0 )
267
+ try :
268
+ line = f .readline ()
269
+ except ValueError :
270
+ self .fail ("readline() after next() with supposedly empty "
271
+ "iteration-buffer failed anyway" )
272
+ if line != testline :
273
+ self .fail ("readline() after next() with empty buffer "
274
+ "failed. Got %r, expected %r" % (line , testline ))
275
+ testline = testlines .pop (0 )
276
+ buf = array ("b" , b"\x00 " * len (testline ))
277
+ try :
278
+ f .readinto (buf )
279
+ except ValueError :
280
+ self .fail ("readinto() after next() with supposedly empty "
281
+ "iteration-buffer failed anyway" )
282
+ line = buf .tostring ()
283
+ if line != testline :
284
+ self .fail ("readinto() after next() with empty buffer "
285
+ "failed. Got %r, expected %r" % (line , testline ))
286
+
287
+ testline = testlines .pop (0 )
288
+ try :
289
+ line = f .read (len (testline ))
290
+ except ValueError :
291
+ self .fail ("read() after next() with supposedly empty "
292
+ "iteration-buffer failed anyway" )
293
+ if line != testline :
294
+ self .fail ("read() after next() with empty buffer "
295
+ "failed. Got %r, expected %r" % (line , testline ))
296
+ try :
297
+ lines = f .readlines ()
298
+ except ValueError :
299
+ self .fail ("readlines() after next() with supposedly empty "
300
+ "iteration-buffer failed anyway" )
301
+ if lines != testlines :
302
+ self .fail ("readlines() after next() with empty buffer "
303
+ "failed. Got %r, expected %r" % (line , testline ))
304
+ # Reading after iteration hit EOF shouldn't hurt either
305
+ f .close ()
306
+ f = self .open (TESTFN , 'rb' )
307
+ try :
308
+ for line in f :
309
+ pass
275
310
try :
311
+ f .readline ()
276
312
f .readinto (buf )
313
+ f .read ()
314
+ f .readlines ()
277
315
except ValueError :
278
- self .fail ("readinto() after next() with supposedly empty "
279
- "iteration-buffer failed anyway" )
280
- line = buf .tostring ()
281
- if line != testline :
282
- self .fail ("readinto() after next() with empty buffer "
283
- "failed. Got %r, expected %r" % (line , testline ))
284
-
285
- testline = testlines .pop (0 )
286
- try :
287
- line = f .read (len (testline ))
288
- except ValueError :
289
- self .fail ("read() after next() with supposedly empty "
290
- "iteration-buffer failed anyway" )
291
- if line != testline :
292
- self .fail ("read() after next() with empty buffer "
293
- "failed. Got %r, expected %r" % (line , testline ))
294
- try :
295
- lines = f .readlines ()
296
- except ValueError :
297
- self .fail ("readlines() after next() with supposedly empty "
298
- "iteration-buffer failed anyway" )
299
- if lines != testlines :
300
- self .fail ("readlines() after next() with empty buffer "
301
- "failed. Got %r, expected %r" % (line , testline ))
302
- # Reading after iteration hit EOF shouldn't hurt either
303
- f .close ()
304
- f = self .open (TESTFN , 'rb' )
305
- try :
306
- for line in f :
307
- pass
308
- try :
309
- f .readline ()
310
- f .readinto (buf )
311
- f .read ()
312
- f .readlines ()
313
- except ValueError :
314
- self .fail ("read* failed after next() consumed file" )
315
- finally :
316
- f .close ()
316
+ self .fail ("read* failed after next() consumed file" )
317
317
finally :
318
- os . unlink ( TESTFN )
318
+ f . close ( )
319
319
320
320
class COtherFileTests (OtherFileTests ):
321
321
open = io .open
@@ -325,14 +325,8 @@ class PyOtherFileTests(OtherFileTests):
325
325
326
326
327
327
def test_main ():
328
- # Historically, these tests have been sloppy about removing TESTFN.
329
- # So get rid of it no matter what.
330
- try :
331
- run_unittest (CAutoFileTests , PyAutoFileTests ,
332
- COtherFileTests , PyOtherFileTests )
333
- finally :
334
- if os .path .exists (TESTFN ):
335
- os .unlink (TESTFN )
328
+ run_unittest (CAutoFileTests , PyAutoFileTests ,
329
+ COtherFileTests , PyOtherFileTests )
336
330
337
331
if __name__ == '__main__' :
338
332
test_main ()
0 commit comments