From 47984639452324c248d3aeafc023b2313322e4c1 Mon Sep 17 00:00:00 2001 From: Brian Schubert Date: Sat, 17 Feb 2024 08:20:25 -0500 Subject: [PATCH 1/5] Fix test_os permanently changing process priority --- Lib/test/test_os.py | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index 2c8823ae47c726..99f89e6c0f59c6 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -3505,23 +3505,24 @@ def test_getlogin(self): class ProgramPriorityTests(unittest.TestCase): """Tests for os.getpriority() and os.setpriority().""" + @support.requires_subprocess() def test_set_get_priority(self): - base = os.getpriority(os.PRIO_PROCESS, os.getpid()) - os.setpriority(os.PRIO_PROCESS, os.getpid(), base + 1) - try: - new_prio = os.getpriority(os.PRIO_PROCESS, os.getpid()) - if base >= 19 and new_prio <= 19: - raise unittest.SkipTest("unable to reliably test setpriority " - "at current nice level of %s" % base) - else: - self.assertEqual(new_prio, base + 1) - finally: - try: - os.setpriority(os.PRIO_PROCESS, os.getpid(), base) - except OSError as err: - if err.errno != errno.EACCES: - raise + code = f"""if 1: + import os + os.setpriority(os.PRIO_PROCESS, os.getpid(), {base} + 1) + print(os.getpriority(os.PRIO_PROCESS, os.getpid())) + """ + + # Subprocess inherits the current process' priority. + proc = subprocess.run([sys.executable, "-c", code], check=True, + stdout=subprocess.PIPE, text=True) + new_prio = int(proc.stdout.rstrip()) + if base >= 19 and new_prio <= 19: + raise unittest.SkipTest("unable to reliably test setpriority " + "at current nice level of %s" % base) + else: + self.assertEqual(new_prio, base + 1) @unittest.skipUnless(hasattr(os, 'sendfile'), "test needs os.sendfile()") From d62467049cde5d981722392028986cb9cf2ee4d2 Mon Sep 17 00:00:00 2001 From: Brian Schubert Date: Sat, 17 Feb 2024 08:25:17 -0500 Subject: [PATCH 2/5] Add NEWS entry --- .../next/Tests/2024-02-17-08-25-01.gh-issue-115596.RGPCrR.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Tests/2024-02-17-08-25-01.gh-issue-115596.RGPCrR.rst diff --git a/Misc/NEWS.d/next/Tests/2024-02-17-08-25-01.gh-issue-115596.RGPCrR.rst b/Misc/NEWS.d/next/Tests/2024-02-17-08-25-01.gh-issue-115596.RGPCrR.rst new file mode 100644 index 00000000000000..2bcb8b9ac6bcd4 --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2024-02-17-08-25-01.gh-issue-115596.RGPCrR.rst @@ -0,0 +1,2 @@ +Fix ``ProgramPriorityTests`` in ``test_os`` permanently changing the process +priority. From 5be0d01188f8776206c08ee357b253d05505c627 Mon Sep 17 00:00:00 2001 From: Brian Schubert Date: Sat, 17 Feb 2024 10:50:53 -0500 Subject: [PATCH 3/5] Use assert_python_ok for better readout on subprocess failure --- Lib/test/test_os.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index f48a62da188d05..5b38c1c4f7e49a 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -3515,9 +3515,8 @@ def test_set_get_priority(self): """ # Subprocess inherits the current process' priority. - proc = subprocess.run([sys.executable, "-c", code], check=True, - stdout=subprocess.PIPE, text=True) - new_prio = int(proc.stdout.rstrip()) + _, out, _ = assert_python_ok("-c", code) + new_prio = int(out.decode("utf-8")) # nice value cap is 19 for linux and 20 for FreeBSD if base >= 19 and new_prio <= base: raise unittest.SkipTest("unable to reliably test setpriority " From ab154ca007a22ab38c9bee250452f91d0577c63f Mon Sep 17 00:00:00 2001 From: Brian Schubert Date: Sat, 17 Feb 2024 11:00:52 -0500 Subject: [PATCH 4/5] Remove @support.requires_subprocess() - covered by assert_python_ok --- Lib/test/test_os.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index 5b38c1c4f7e49a..3148514bdecd2e 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -3505,7 +3505,6 @@ def test_getlogin(self): class ProgramPriorityTests(unittest.TestCase): """Tests for os.getpriority() and os.setpriority().""" - @support.requires_subprocess() def test_set_get_priority(self): base = os.getpriority(os.PRIO_PROCESS, os.getpid()) code = f"""if 1: From 25d2e70ce4b1e1a0cceeba138bfad6b2be8802f6 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sat, 17 Feb 2024 18:03:05 +0200 Subject: [PATCH 5/5] Update Lib/test/test_os.py --- Lib/test/test_os.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index 3148514bdecd2e..2372ac4c21efd9 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -3515,7 +3515,7 @@ def test_set_get_priority(self): # Subprocess inherits the current process' priority. _, out, _ = assert_python_ok("-c", code) - new_prio = int(out.decode("utf-8")) + new_prio = int(out) # nice value cap is 19 for linux and 20 for FreeBSD if base >= 19 and new_prio <= base: raise unittest.SkipTest("unable to reliably test setpriority "