@@ -57,29 +57,16 @@ def test_fsize_enforced(self):
5757
5858 if max_lim != resource .RLIM_INFINITY and max_lim < 1025 :
5959 self .skipTest (f"system RLIMIT_FSIZE hard limit ({ max_lim } ) is too small for this test" )
60- try :
60+ with open ( os_helper . TESTFN , "wb" ) as f :
6161 try :
62- with open (os_helper .TESTFN , "wb" ) as f :
63- resource .setrlimit (resource .RLIMIT_FSIZE , (1024 , max_lim ))
64- f .write (b"X" * 1024 )
65- with self .assertRaises (OSError , msg = "f.write() did not raise OSError when exceeding RLIMIT_FSIZE" ):
66- f .write (b"Y" )
67- f .flush ()
68- # On some systems (e.g., Ubuntu on hppa) the flush()
69- # doesn't always cause the exception, but the close()
70- # does eventually. Try flushing several times in
71- # an attempt to ensure the file is really synced and
72- # the exception raised.
73- for i in range (5 ):
74- time .sleep (.1 )
75- f .flush ()
76- except OSError as e :
77- if e .errno == errno .EFBIG :
78- self .skipTest (f"ASAN/OS raised EFBIG prematurely on open/setup: { e } " )
79- raise
80- finally :
81- resource .setrlimit (resource .RLIMIT_FSIZE , (cur , max_lim ))
82-
62+ resource .setrlimit (resource .RLIMIT_FSIZE , (1024 , max_lim ))
63+ f .write (b"X" * 1024 )
64+ with self .assertRaises (OSError , msg = "f.write() did not raise OSError when exceeding RLIMIT_FSIZE" ):
65+ f .write (b"Y" )
66+ finally :
67+ # Close will attempt to flush the byte we wrote
68+ # Restore limit first to avoid getting a spurious error
69+ resource .setrlimit (resource .RLIMIT_FSIZE , (cur , max_lim ))
8370 @unittest .skipIf (sys .platform == "vxworks" ,
8471 "setting RLIMIT_FSIZE is not supported on VxWorks" )
8572 @unittest .skipUnless (hasattr (resource , 'RLIMIT_FSIZE' ), 'requires resource.RLIMIT_FSIZE' )
0 commit comments