@@ -475,22 +475,18 @@ def test_tofromfile(self):
475475 self .assertRaises (TypeError , a .tofile )
476476 with os_helper .temp_dir () as temp_dir :
477477 temp_path = os .path .join (temp_dir , os_helper .TESTFN )
478- f = open (temp_path , 'wb' )
479478 try :
480- a . tofile ( f )
481- f . close ( )
479+ with open ( temp_path , 'wb' ) as f :
480+ a . tofile ( f )
482481 b = array .array (self .typecode )
483- f = open (temp_path , 'rb' )
484- self .assertRaises (TypeError , b .fromfile )
485- b .fromfile (f , len (self .example ))
486- self .assertEqual (b , array .array (self .typecode , self .example ))
487- self .assertNotEqual (a , b )
488- self .assertRaises (EOFError , b .fromfile , f , len (self .example )+ 1 )
489- self .assertEqual (a , b )
490- f .close ()
482+ with open (temp_path , 'rb' ) as f :
483+ self .assertRaises (TypeError , b .fromfile )
484+ b .fromfile (f , len (self .example ))
485+ self .assertEqual (b , array .array (self .typecode , self .example ))
486+ self .assertNotEqual (a , b )
487+ self .assertRaises (EOFError , b .fromfile , f , len (self .example )+ 1 )
488+ self .assertEqual (a , b )
491489 finally :
492- if not f .closed :
493- f .close ()
494490 os_helper .unlink (temp_path )
495491
496492 def test_fromfile_ioerror (self ):
@@ -499,32 +495,27 @@ def test_fromfile_ioerror(self):
499495 a = array .array (self .typecode )
500496 with os_helper .temp_dir () as temp_dir :
501497 temp_path = os .path .join (temp_dir , os_helper .TESTFN )
502- f = open (temp_path , 'wb' )
503498 try :
504- self .assertRaises (OSError , a .fromfile , f , len (self .example ))
499+ with open (temp_path , 'wb' ) as f :
500+ self .assertRaises (OSError , a .fromfile , f , len (self .example ))
505501 finally :
506- f .close ()
507502 os_helper .unlink (temp_path )
508503
509504 def test_filewrite (self ):
510505 a = array .array (self .typecode , 2 * self .example )
511506 with os_helper .temp_dir () as temp_dir :
512507 temp_path = os .path .join (temp_dir , os_helper .TESTFN )
513- f = open (temp_path , 'wb' )
514508 try :
515- f . write ( a )
516- f . close ( )
509+ with open ( temp_path , 'wb' ) as f :
510+ f . write ( a )
517511 b = array .array (self .typecode )
518- f = open (temp_path , 'rb' )
519- b .fromfile (f , len (self .example ))
520- self .assertEqual (b , array .array (self .typecode , self .example ))
521- self .assertNotEqual (a , b )
522- b .fromfile (f , len (self .example ))
523- self .assertEqual (a , b )
524- f .close ()
512+ with open (temp_path , 'rb' ) as f :
513+ b .fromfile (f , len (self .example ))
514+ self .assertEqual (b , array .array (self .typecode , self .example ))
515+ self .assertNotEqual (a , b )
516+ b .fromfile (f , len (self .example ))
517+ self .assertEqual (a , b )
525518 finally :
526- if not f .closed :
527- f .close ()
528519 os_helper .unlink (temp_path )
529520
530521 def test_tofromlist (self ):
0 commit comments