]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-115596: Fix ProgramPriorityTests in test_os permanently changing the process prior...
authorBrian Schubert <brianm.schubert@gmail.com>
Sat, 17 Feb 2024 16:42:57 +0000 (11:42 -0500)
committerGitHub <noreply@github.com>
Sat, 17 Feb 2024 16:42:57 +0000 (16:42 +0000)
Lib/test/test_os.py
Misc/NEWS.d/next/Tests/2024-02-17-08-25-01.gh-issue-115596.RGPCrR.rst [new file with mode: 0644]

index ce778498c61d875d84ee4ca80ab802987a289e3e..2372ac4c21efd9fd1a939991023b141130a46346 100644 (file)
@@ -3506,23 +3506,22 @@ class ProgramPriorityTests(unittest.TestCase):
     """Tests for os.getpriority() and os.setpriority()."""
 
     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())
-            # 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 "
-                                        "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.
+        _, out, _ = assert_python_ok("-c", code)
+        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 "
+                                    "at current nice level of %s" % base)
+        else:
+            self.assertEqual(new_prio, base + 1)
 
 
 @unittest.skipUnless(hasattr(os, 'sendfile'), "test needs os.sendfile()")
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 (file)
index 0000000..2bcb8b9
--- /dev/null
@@ -0,0 +1,2 @@
+Fix ``ProgramPriorityTests`` in ``test_os`` permanently changing the process
+priority.