Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 38 additions & 36 deletions Lib/test/test_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,47 +50,49 @@ def test_fsize_ismax(self):
"setting RLIMIT_FSIZE is not supported on VxWorks")
@unittest.skipUnless(hasattr(resource, 'RLIMIT_FSIZE'), 'requires resource.RLIMIT_FSIZE')
def test_fsize_enforced(self):
(cur, max) = resource.getrlimit(resource.RLIMIT_FSIZE)
# Check to see what happens when the RLIMIT_FSIZE is small. Some
# versions of Python were terminated by an uncaught SIGXFSZ, but
# pythonrun.c has been fixed to ignore that exception. If so, the
# write() should return EFBIG when the limit is exceeded.
try:
(cur, max_lim) = resource.getrlimit(resource.RLIMIT_FSIZE)
except OSError as e:
self.skipTest(f"getrlimit(RLIMIT_FSIZE) failed: {e}")

if max_lim != resource.RLIM_INFINITY and max_lim < 1025:
self.skipTest(f"system RLIMIT_FSIZE hard limit ({max_lim}) is too small for this test")

# At least one platform has an unlimited RLIMIT_FSIZE and attempts
# to change it raise ValueError instead.
try:
resource.setrlimit(resource.RLIMIT_FSIZE, (1024, max_lim))
resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max_lim))
except (ValueError, OSError, PermissionError) as e:
self.skipTest(f"cannot set RLIMIT_FSIZE to 1024: {e}")

self.addCleanup(os_helper.unlink, os_helper.TESTFN)
self.addCleanup(resource.setrlimit, resource.RLIMIT_FSIZE, (cur, max_lim))

resource.setrlimit(resource.RLIMIT_FSIZE, (1024, max_lim))

f = open(os_helper.TESTFN, "wb")
try:
f.write(b"X" * 1024)
try:
resource.setrlimit(resource.RLIMIT_FSIZE, (1024, max))
limit_set = True
except ValueError:
limit_set = False
f = open(os_helper.TESTFN, "wb")
try:
f.write(b"X" * 1024)
try:
f.write(b"Y")
f.write(b"Y")
f.flush()
# On some systems (e.g., Ubuntu on hppa) the flush()
# doesn't always cause the exception, but the close()
# does eventually. Try flushing several times in
# an attempt to ensure the file is really synced and
# the exception raised.
for i in range(5):
time.sleep(.1)
f.flush()
# On some systems (e.g., Ubuntu on hppa) the flush()
# doesn't always cause the exception, but the close()
# does eventually. Try flushing several times in
# an attempt to ensure the file is really synced and
# the exception raised.
for i in range(5):
time.sleep(.1)
f.flush()
except OSError:
if not limit_set:
raise
if limit_set:
# Close will attempt to flush the byte we wrote
# Restore limit first to avoid getting a spurious error
resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
finally:
f.close()
except OSError:
pass
else:
self.fail("f.write() did not raise OSError when exceeding RLIMIT_FSIZE")

# Close will attempt to flush the byte we wrote
# Restore limit first to avoid getting a spurious error
resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max_lim))
finally:
if limit_set:
resource.setrlimit(resource.RLIMIT_FSIZE, (cur, max))
os_helper.unlink(os_helper.TESTFN)
f.close()

@unittest.skipIf(sys.platform == "vxworks",
"setting RLIMIT_FSIZE is not supported on VxWorks")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix ``test_resource.test_fsize_enforced`` to gracefully skip when the system hard limit or privileges prevent setting file size limits.
Loading