@@ -50,47 +50,49 @@ def test_fsize_ismax(self):
5050 "setting RLIMIT_FSIZE is not supported on VxWorks" )
5151 @unittest .skipUnless (hasattr (resource , 'RLIMIT_FSIZE' ), 'requires resource.RLIMIT_FSIZE' )
5252 def test_fsize_enforced (self ):
53- (cur , max ) = resource .getrlimit (resource .RLIMIT_FSIZE )
54- # Check to see what happens when the RLIMIT_FSIZE is small. Some
55- # versions of Python were terminated by an uncaught SIGXFSZ, but
56- # pythonrun.c has been fixed to ignore that exception. If so, the
57- # write() should return EFBIG when the limit is exceeded.
53+ try :
54+ (cur , max_lim ) = resource .getrlimit (resource .RLIMIT_FSIZE )
55+ except OSError as e :
56+ self .skipTest (f"getrlimit(RLIMIT_FSIZE) failed: { e } " )
57+
58+ if max_lim != resource .RLIM_INFINITY and max_lim < 1025 :
59+ self .skipTest (f"system RLIMIT_FSIZE hard limit ({ max_lim } ) is too small for this test" )
5860
59- # At least one platform has an unlimited RLIMIT_FSIZE and attempts
60- # to change it raise ValueError instead.
6161 try :
62+ resource .setrlimit (resource .RLIMIT_FSIZE , (1024 , max_lim ))
63+ resource .setrlimit (resource .RLIMIT_FSIZE , (cur , max_lim ))
64+ except (ValueError , OSError , PermissionError ) as e :
65+ self .skipTest (f"cannot set RLIMIT_FSIZE to 1024: { e } " )
66+
67+ self .addCleanup (os_helper .unlink , os_helper .TESTFN )
68+ self .addCleanup (resource .setrlimit , resource .RLIMIT_FSIZE , (cur , max_lim ))
69+
70+ resource .setrlimit (resource .RLIMIT_FSIZE , (1024 , max_lim ))
71+
72+ f = open (os_helper .TESTFN , "wb" )
73+ try :
74+ f .write (b"X" * 1024 )
6275 try :
63- resource . setrlimit ( resource . RLIMIT_FSIZE , ( 1024 , max ) )
64- limit_set = True
65- except ValueError :
66- limit_set = False
67- f = open ( os_helper . TESTFN , "wb" )
68- try :
69- f . write ( b"X" * 1024 )
70- try :
71- f . write ( b"Y" )
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 )
7285 f .flush ()
73- # On some systems (e.g., Ubuntu on hppa) the flush()
74- # doesn't always cause the exception, but the close()
75- # does eventually. Try flushing several times in
76- # an attempt to ensure the file is really synced and
77- # the exception raised.
78- for i in range (5 ):
79- time .sleep (.1 )
80- f .flush ()
81- except OSError :
82- if not limit_set :
83- raise
84- if limit_set :
85- # Close will attempt to flush the byte we wrote
86- # Restore limit first to avoid getting a spurious error
87- resource .setrlimit (resource .RLIMIT_FSIZE , (cur , max ))
88- finally :
89- f .close ()
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 ))
9094 finally :
91- if limit_set :
92- resource .setrlimit (resource .RLIMIT_FSIZE , (cur , max ))
93- os_helper .unlink (os_helper .TESTFN )
95+ f .close ()
9496
9597 @unittest .skipIf (sys .platform == "vxworks" ,
9698 "setting RLIMIT_FSIZE is not supported on VxWorks" )
0 commit comments