Skip to content

Commit 461125a

Browse files
authored
gh-37883: Safely skip test_resource file size tests when limits are strict (GH-145579)
1 parent 8687b9d commit 461125a

File tree

1 file changed

+13
-37
lines changed

1 file changed

+13
-37
lines changed

Lib/test/test_resource.py

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from test import support
55
from test.support import import_helper
66
from test.support import os_helper
7-
import time
87

98
resource = import_helper.import_module('resource')
109

@@ -41,7 +40,7 @@ def test_fsize_ismax(self):
4140
# the number to a C long long and that the conversion doesn't raise
4241
# an error.
4342
self.assertGreater(resource.RLIM_INFINITY, 0)
44-
self.assertEqual(resource.RLIM_INFINITY, max)
43+
self.assertGreaterEqual(max, 0)
4544
self.assertLessEqual(cur, max)
4645
resource.setrlimit(resource.RLIMIT_FSIZE, (max, max))
4746
resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
@@ -50,47 +49,24 @@ def test_fsize_ismax(self):
5049
"setting RLIMIT_FSIZE is not supported on VxWorks")
5150
@unittest.skipUnless(hasattr(resource, 'RLIMIT_FSIZE'), 'requires resource.RLIMIT_FSIZE')
5251
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.
58-
59-
# At least one platform has an unlimited RLIMIT_FSIZE and attempts
60-
# to change it raise ValueError instead.
52+
self.addCleanup(os_helper.unlink, os_helper.TESTFN)
6153
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+
if max_lim != resource.RLIM_INFINITY and max_lim < 1025:
58+
self.skipTest(f"system RLIMIT_FSIZE hard limit ({max_lim}) is too small for this test")
59+
with open(os_helper.TESTFN, "wb", buffering=0) as f:
6260
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:
61+
resource.setrlimit(resource.RLIMIT_FSIZE, (1024, max_lim))
6962
f.write(b"X" * 1024)
70-
try:
63+
with self.assertRaises(OSError, msg="f.write() did not raise OSError when exceeding RLIMIT_FSIZE"):
7164
f.write(b"Y")
7265
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))
8866
finally:
89-
f.close()
90-
finally:
91-
if limit_set:
92-
resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
93-
os_helper.unlink(os_helper.TESTFN)
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))
9470

9571
@unittest.skipIf(sys.platform == "vxworks",
9672
"setting RLIMIT_FSIZE is not supported on VxWorks")

0 commit comments

Comments
 (0)