From 44b4336b6123a8182bbdf72d9a7ae242759bf7a3 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 5 Sep 2023 22:57:04 +0200 Subject: [PATCH 1/3] gh-108962: Skip test_tempfile.test_flags() if not supported Skip test_tempfile.test_flags() if chflags() fails with "OSError: [Errno 45] Operation not supported" (ex: on FreeBSD 13). --- Lib/test/test_tempfile.py | 14 ++++++++++++++ .../2023-09-05-23-00-09.gh-issue-108962.R4NwuU.rst | 3 +++ 2 files changed, 17 insertions(+) create mode 100644 Misc/NEWS.d/next/Tests/2023-09-05-23-00-09.gh-issue-108962.R4NwuU.rst diff --git a/Lib/test/test_tempfile.py b/Lib/test/test_tempfile.py index db08fb1c7f2a42..3aa622604cb85b 100644 --- a/Lib/test/test_tempfile.py +++ b/Lib/test/test_tempfile.py @@ -1837,6 +1837,20 @@ def test_modes(self): @unittest.skipUnless(hasattr(os, 'chflags'), 'requires os.lchflags') def test_flags(self): flags = stat.UF_IMMUTABLE | stat.UF_NOUNLINK + + # skip the test if these flags are not supported (ex: FreeBSD 13) + filename = TESTFN + try: + open(filename, "w").close() + try: + os.chflags(filename, flags) + except OSError as exc: + # "OSError: [Errno 45] Operation not supported" + self.skipTest("chflags() doesn't support " + "UF_IMMUTABLE|UF_NOUNLINK: {exc}") + finally: + support.unlink(filename) + d = self.do_create(recurse=3, dirs=2, files=2) with d: # Change files and directories flags recursively. diff --git a/Misc/NEWS.d/next/Tests/2023-09-05-23-00-09.gh-issue-108962.R4NwuU.rst b/Misc/NEWS.d/next/Tests/2023-09-05-23-00-09.gh-issue-108962.R4NwuU.rst new file mode 100644 index 00000000000000..380fb20b8881b2 --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2023-09-05-23-00-09.gh-issue-108962.R4NwuU.rst @@ -0,0 +1,3 @@ +Skip ``test_tempfile.test_flags()`` if ``chflags()`` fails with "OSError: +[Errno 45] Operation not supported" (ex: on FreeBSD 13). Patch by Victor +Stinner. From 2c2dbbf91c26cac57c17a76d666a60972feade61 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 5 Sep 2023 23:03:37 +0200 Subject: [PATCH 2/3] fix typo --- Lib/test/test_tempfile.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_tempfile.py b/Lib/test/test_tempfile.py index 3aa622604cb85b..2e5eeb8823c4a7 100644 --- a/Lib/test/test_tempfile.py +++ b/Lib/test/test_tempfile.py @@ -1834,12 +1834,12 @@ def test_modes(self): d.cleanup() self.assertFalse(os.path.exists(d.name)) - @unittest.skipUnless(hasattr(os, 'chflags'), 'requires os.lchflags') + @unittest.skipUnless(hasattr(os, 'chflags'), 'requires os.chflags') def test_flags(self): flags = stat.UF_IMMUTABLE | stat.UF_NOUNLINK # skip the test if these flags are not supported (ex: FreeBSD 13) - filename = TESTFN + filename = os_helper.TESTFN try: open(filename, "w").close() try: @@ -1849,7 +1849,7 @@ def test_flags(self): self.skipTest("chflags() doesn't support " "UF_IMMUTABLE|UF_NOUNLINK: {exc}") finally: - support.unlink(filename) + os_helper.unlink(filename) d = self.do_create(recurse=3, dirs=2, files=2) with d: From 6843d16c05945812eacf1d2c6efd7c770cb38ca5 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 5 Sep 2023 23:10:28 +0200 Subject: [PATCH 3/3] reset flags to be able to remove the file --- Lib/test/test_tempfile.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_tempfile.py b/Lib/test/test_tempfile.py index 2e5eeb8823c4a7..1673507e2f7c91 100644 --- a/Lib/test/test_tempfile.py +++ b/Lib/test/test_tempfile.py @@ -1846,8 +1846,10 @@ def test_flags(self): os.chflags(filename, flags) except OSError as exc: # "OSError: [Errno 45] Operation not supported" - self.skipTest("chflags() doesn't support " - "UF_IMMUTABLE|UF_NOUNLINK: {exc}") + self.skipTest(f"chflags() doesn't support " + f"UF_IMMUTABLE|UF_NOUNLINK: {exc}") + else: + os.chflags(filename, 0) finally: os_helper.unlink(filename)