[3.11] gh-115596: Fix ProgramPriorityTests in test_os permanently changing the process priority (GH-115610) (GH-115617)

(cherry picked from commit 90dd653a61)

Co-authored-by: Brian Schubert <brianm.schubert@gmail.com>
This commit is contained in:
Miss Islington (bot) 2024-02-17 18:14:41 +01:00 committed by GitHub
parent ee3d8dcb00
commit cd0a071fdd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 16 deletions

View File

@ -3320,23 +3320,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()")

View File

@ -0,0 +1,2 @@
Fix ``ProgramPriorityTests`` in ``test_os`` permanently changing the process
priority.