8
8
import _pyio as pyio
9
9
10
10
from test .support import TESTFN
11
+ from test import support
11
12
from collections import UserList
12
13
13
14
class AutoFileTests :
@@ -19,7 +20,7 @@ def setUp(self):
19
20
def tearDown (self ):
20
21
if self .f :
21
22
self .f .close ()
22
- os . remove (TESTFN )
23
+ support . unlink (TESTFN )
23
24
24
25
def testWeakRefs (self ):
25
26
# verify weak references
@@ -137,8 +138,12 @@ class PyAutoFileTests(AutoFileTests, unittest.TestCase):
137
138
138
139
class OtherFileTests :
139
140
141
+ def tearDown (self ):
142
+ support .unlink (TESTFN )
143
+
140
144
def testModeStrings (self ):
141
145
# check invalid mode strings
146
+ self .open (TESTFN , 'wb' ).close ()
142
147
for mode in ("" , "aU" , "wU+" , "U+" , "+U" , "rU+" ):
143
148
try :
144
149
f = self .open (TESTFN , mode )
@@ -185,7 +190,6 @@ def testTruncateOnWindows(self):
185
190
# SF bug <http://www.python.org/sf/801631>
186
191
# "file.truncate fault on windows"
187
192
188
- os .unlink (TESTFN )
189
193
f = self .open (TESTFN , 'wb' )
190
194
191
195
try :
@@ -209,7 +213,6 @@ def testTruncateOnWindows(self):
209
213
self .fail ("File size after ftruncate wrong %d" % size )
210
214
finally :
211
215
f .close ()
212
- os .unlink (TESTFN )
213
216
214
217
def testIteration (self ):
215
218
# Test the complex interaction when mixing file-iteration and the
@@ -230,87 +233,84 @@ def testIteration(self):
230
233
methods = [("readline" , ()), ("read" , ()), ("readlines" , ()),
231
234
("readinto" , (array ("b" , b" " * 100 ),))]
232
235
236
+ # Prepare the testfile
237
+ bag = self .open (TESTFN , "wb" )
238
+ bag .write (filler * nchunks )
239
+ bag .writelines (testlines )
240
+ bag .close ()
241
+ # Test for appropriate errors mixing read* and iteration
242
+ for methodname , args in methods :
243
+ f = self .open (TESTFN , 'rb' )
244
+ if next (f ) != filler :
245
+ self .fail , "Broken testfile"
246
+ meth = getattr (f , methodname )
247
+ meth (* args ) # This simply shouldn't fail
248
+ f .close ()
249
+
250
+ # Test to see if harmless (by accident) mixing of read* and
251
+ # iteration still works. This depends on the size of the internal
252
+ # iteration buffer (currently 8192,) but we can test it in a
253
+ # flexible manner. Each line in the bag o' ham is 4 bytes
254
+ # ("h", "a", "m", "\n"), so 4096 lines of that should get us
255
+ # exactly on the buffer boundary for any power-of-2 buffersize
256
+ # between 4 and 16384 (inclusive).
257
+ f = self .open (TESTFN , 'rb' )
258
+ for i in range (nchunks ):
259
+ next (f )
260
+ testline = testlines .pop (0 )
233
261
try :
234
- # Prepare the testfile
235
- bag = self .open (TESTFN , "wb" )
236
- bag .write (filler * nchunks )
237
- bag .writelines (testlines )
238
- bag .close ()
239
- # Test for appropriate errors mixing read* and iteration
240
- for methodname , args in methods :
241
- f = self .open (TESTFN , 'rb' )
242
- if next (f ) != filler :
243
- self .fail , "Broken testfile"
244
- meth = getattr (f , methodname )
245
- meth (* args ) # This simply shouldn't fail
246
- f .close ()
262
+ line = f .readline ()
263
+ except ValueError :
264
+ self .fail ("readline() after next() with supposedly empty "
265
+ "iteration-buffer failed anyway" )
266
+ if line != testline :
267
+ self .fail ("readline() after next() with empty buffer "
268
+ "failed. Got %r, expected %r" % (line , testline ))
269
+ testline = testlines .pop (0 )
270
+ buf = array ("b" , b"\x00 " * len (testline ))
271
+ try :
272
+ f .readinto (buf )
273
+ except ValueError :
274
+ self .fail ("readinto() after next() with supposedly empty "
275
+ "iteration-buffer failed anyway" )
276
+ line = buf .tobytes ()
277
+ if line != testline :
278
+ self .fail ("readinto() after next() with empty buffer "
279
+ "failed. Got %r, expected %r" % (line , testline ))
280
+
281
+ testline = testlines .pop (0 )
282
+ try :
283
+ line = f .read (len (testline ))
284
+ except ValueError :
285
+ self .fail ("read() after next() with supposedly empty "
286
+ "iteration-buffer failed anyway" )
287
+ if line != testline :
288
+ self .fail ("read() after next() with empty buffer "
289
+ "failed. Got %r, expected %r" % (line , testline ))
290
+ try :
291
+ lines = f .readlines ()
292
+ except ValueError :
293
+ self .fail ("readlines() after next() with supposedly empty "
294
+ "iteration-buffer failed anyway" )
295
+ if lines != testlines :
296
+ self .fail ("readlines() after next() with empty buffer "
297
+ "failed. Got %r, expected %r" % (line , testline ))
298
+ f .close ()
247
299
248
- # Test to see if harmless (by accident) mixing of read* and
249
- # iteration still works. This depends on the size of the internal
250
- # iteration buffer (currently 8192,) but we can test it in a
251
- # flexible manner. Each line in the bag o' ham is 4 bytes
252
- # ("h", "a", "m", "\n"), so 4096 lines of that should get us
253
- # exactly on the buffer boundary for any power-of-2 buffersize
254
- # between 4 and 16384 (inclusive).
255
- f = self .open (TESTFN , 'rb' )
256
- for i in range (nchunks ):
257
- next (f )
258
- testline = testlines .pop (0 )
259
- try :
260
- line = f .readline ()
261
- except ValueError :
262
- self .fail ("readline() after next() with supposedly empty "
263
- "iteration-buffer failed anyway" )
264
- if line != testline :
265
- self .fail ("readline() after next() with empty buffer "
266
- "failed. Got %r, expected %r" % (line , testline ))
267
- testline = testlines .pop (0 )
268
- buf = array ("b" , b"\x00 " * len (testline ))
300
+ # Reading after iteration hit EOF shouldn't hurt either
301
+ f = self .open (TESTFN , 'rb' )
302
+ try :
303
+ for line in f :
304
+ pass
269
305
try :
306
+ f .readline ()
270
307
f .readinto (buf )
308
+ f .read ()
309
+ f .readlines ()
271
310
except ValueError :
272
- self .fail ("readinto() after next() with supposedly empty "
273
- "iteration-buffer failed anyway" )
274
- line = buf .tobytes ()
275
- if line != testline :
276
- self .fail ("readinto() after next() with empty buffer "
277
- "failed. Got %r, expected %r" % (line , testline ))
278
-
279
- testline = testlines .pop (0 )
280
- try :
281
- line = f .read (len (testline ))
282
- except ValueError :
283
- self .fail ("read() after next() with supposedly empty "
284
- "iteration-buffer failed anyway" )
285
- if line != testline :
286
- self .fail ("read() after next() with empty buffer "
287
- "failed. Got %r, expected %r" % (line , testline ))
288
- try :
289
- lines = f .readlines ()
290
- except ValueError :
291
- self .fail ("readlines() after next() with supposedly empty "
292
- "iteration-buffer failed anyway" )
293
- if lines != testlines :
294
- self .fail ("readlines() after next() with empty buffer "
295
- "failed. Got %r, expected %r" % (line , testline ))
296
- f .close ()
297
-
298
- # Reading after iteration hit EOF shouldn't hurt either
299
- f = self .open (TESTFN , 'rb' )
300
- try :
301
- for line in f :
302
- pass
303
- try :
304
- f .readline ()
305
- f .readinto (buf )
306
- f .read ()
307
- f .readlines ()
308
- except ValueError :
309
- self .fail ("read* failed after next() consumed file" )
310
- finally :
311
- f .close ()
311
+ self .fail ("read* failed after next() consumed file" )
312
312
finally :
313
- os . unlink ( TESTFN )
313
+ f . close ( )
314
314
315
315
class COtherFileTests (OtherFileTests , unittest .TestCase ):
316
316
open = io .open
@@ -319,11 +319,5 @@ class PyOtherFileTests(OtherFileTests, unittest.TestCase):
319
319
open = staticmethod (pyio .open )
320
320
321
321
322
- def tearDownModule ():
323
- # Historically, these tests have been sloppy about removing TESTFN.
324
- # So get rid of it no matter what.
325
- if os .path .exists (TESTFN ):
326
- os .unlink (TESTFN )
327
-
328
322
if __name__ == '__main__' :
329
323
unittest .main ()
0 commit comments