@@ -118,15 +118,12 @@ def test_basic(self):
118118    @unittest .skipUnless (hasattr (mmap .mmap , 'resize' ), 'requires mmap.resize' ) 
119119    def  test_resize (self ):
120120        # Create a file to be mmap'ed. 
121-         f  =  open (TESTFN , 'bw+' )
122-         try :
121+         with  open (TESTFN , 'bw+' ) as  f :
123122            # Write 2 pages worth of data to the file 
124123            f .write (b'\0 ' *  2  *  PAGESIZE )
125124            f .flush ()
126125            m  =  mmap .mmap (f .fileno (), 2  *  PAGESIZE )
127126            self .addCleanup (m .close )
128-         finally :
129-             f .close ()
130127
131128        # Try resizing map 
132129        m .resize (512 )
@@ -136,12 +133,9 @@ def test_resize(self):
136133
137134        # Check that the underlying file is truncated too 
138135        # (bug #728515) 
139-         f  =  open (TESTFN , 'rb' )
140-         try :
136+         with  open (TESTFN , 'rb' ) as  f :
141137            f .seek (0 , 2 )
142138            self .assertEqual (f .tell (), 512 )
143-         finally :
144-             f .close ()
145139        self .assertEqual (m .size (), 512 )
146140
147141    def  test_access_parameter (self ):
@@ -187,15 +181,10 @@ def test_access_parameter(self):
187181            else :
188182                self .fail ("Able to write to readonly memory map" )
189183
190-             # Ensuring that readonly mmap can't be resized 
191-             try :
192-                 m .resize (2 * mapsize )
193-             except  AttributeError :   # resize is not universally supported 
194-                 pass 
195-             except  TypeError :
196-                 pass 
197-             else :
198-                 self .fail ("Able to resize readonly memory map" )
184+             if  hasattr (m , 'resize' ):
185+                 # Ensuring that readonly mmap can't be resized 
186+                 with  self .assertRaises (TypeError ):
187+                     m .resize (2 * mapsize )
199188            with  open (TESTFN , "rb" ) as  fp :
200189                self .assertEqual (fp .read (), b'a' * mapsize ,
201190                                 "Readonly memory map data file was modified" )
@@ -621,13 +610,9 @@ def test_offset (self):
621610            self .assertEqual (m [0 :3 ], b'foo' )
622611            f .close ()
623612
624-             # Try resizing map 
625-             try : 
613+             if   hasattr ( m ,  'resize' ): 
614+                  # Try resizing map 
626615                m .resize (512 )
627-             except  AttributeError :
628-                 pass 
629-             else :
630-                 # resize() is supported 
631616                self .assertEqual (len (m ), 512 )
632617                # Check that we can no longer seek beyond the new size. 
633618                self .assertRaises (ValueError , m .seek , 513 , 0 )
0 commit comments