@@ -68,31 +68,25 @@ def test_fsize_enforced(self):
6868 self .addCleanup (resource .setrlimit , resource .RLIMIT_FSIZE , (cur , max_lim ))
6969
7070 resource .setrlimit (resource .RLIMIT_FSIZE , (1024 , max_lim ))
71-
72- f = open (os_helper .TESTFN , "wb" )
71+
7372 try :
74- f .write (b"X" * 1024 )
75- try :
76- f .write (b"Y" )
77- f .flush ()
78- # On some systems (e.g., Ubuntu on hppa) the flush()
79- # doesn't always cause the exception, but the close()
80- # does eventually. Try flushing several times in
81- # an attempt to ensure the file is really synced and
82- # the exception raised.
83- for i in range (5 ):
84- time .sleep (.1 )
73+ with open (os_helper .TESTFN , "wb" ) as f :
74+ f .write (b"X" * 1024 )
75+ # This should raise OSError because it exceeds the 1024 byte limit
76+ with self .assertRaises (OSError , msg = "f.write() did not raise OSError when exceeding RLIMIT_FSIZE" ):
77+ f .write (b"Y" )
8578 f .flush ()
86- except OSError :
87- pass
88- else :
89- self . fail ( "f.write() did not raise OSError when exceeding RLIMIT_FSIZE" )
90-
91- # Close will attempt to flush the byte we wrote
92- # Restore limit first to avoid getting a spurious error
93- resource . setrlimit ( resource . RLIMIT_FSIZE , ( cur , max_lim ) )
79+ # On some systems (e.g., Ubuntu on hppa) the flush()
80+ # doesn't always cause the exception, but the close()
81+ # does eventually. Try flushing several times in
82+ # an attempt to ensure the file is really synced and
83+ # the exception raised.
84+ for i in range ( 5 ):
85+ time . sleep ( .1 )
86+ f . flush ( )
9487 finally :
95- f .close ()
88+ # Restore limit after the file is closed by the 'with' block
89+ resource .setrlimit (resource .RLIMIT_FSIZE , (cur , max_lim ))
9690
9791 @unittest .skipIf (sys .platform == "vxworks" ,
9892 "setting RLIMIT_FSIZE is not supported on VxWorks" )
0 commit comments