From 3cae2262a0ddac67d547c69d209ebaf7174bf0b0 Mon Sep 17 00:00:00 2001 From: Shrey Naithani Date: Fri, 6 Mar 2026 12:50:26 +0500 Subject: [PATCH 1/3] gh-37883: Safely skip test_resource file size tests when limits are strict --- Lib/test/test_resource.py | 74 ++++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 36 deletions(-) diff --git a/Lib/test/test_resource.py b/Lib/test/test_resource.py index 5fd076bee38e79..0e1738ef0ad04a 100644 --- a/Lib/test/test_resource.py +++ b/Lib/test/test_resource.py @@ -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") From f217fb1a9dcbeec2b51e5187b9207d6fb94fd2a7 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Fri, 6 Mar 2026 09:43:51 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Tests/2026-03-06-09-43-49.gh-issue-37883.I-3oZq.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Tests/2026-03-06-09-43-49.gh-issue-37883.I-3oZq.rst diff --git a/Misc/NEWS.d/next/Tests/2026-03-06-09-43-49.gh-issue-37883.I-3oZq.rst b/Misc/NEWS.d/next/Tests/2026-03-06-09-43-49.gh-issue-37883.I-3oZq.rst new file mode 100644 index 00000000000000..f301cba7e9cb98 --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2026-03-06-09-43-49.gh-issue-37883.I-3oZq.rst @@ -0,0 +1 @@ +Fix :meth:`test_resource.test_fsize_enforced` to gracefully skip when the system hard limit or privileges prevent setting file size limits. From ef28dbcb738a0a0df4ce3ad487a9385802f59cd8 Mon Sep 17 00:00:00 2001 From: Shrey Naithani Date: Fri, 6 Mar 2026 17:00:55 +0500 Subject: [PATCH 3/3] Update News Entry --- .../next/Tests/2026-03-06-09-43-49.gh-issue-37883.I-3oZq.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Tests/2026-03-06-09-43-49.gh-issue-37883.I-3oZq.rst b/Misc/NEWS.d/next/Tests/2026-03-06-09-43-49.gh-issue-37883.I-3oZq.rst index f301cba7e9cb98..a047b69646c4ab 100644 --- a/Misc/NEWS.d/next/Tests/2026-03-06-09-43-49.gh-issue-37883.I-3oZq.rst +++ b/Misc/NEWS.d/next/Tests/2026-03-06-09-43-49.gh-issue-37883.I-3oZq.rst @@ -1 +1 @@ -Fix :meth:`test_resource.test_fsize_enforced` to gracefully skip when the system hard limit or privileges prevent setting file size limits. +Fix ``test_resource.test_fsize_enforced`` to gracefully skip when the system hard limit or privileges prevent setting file size limits.