Skip to content

Commit dadc768

Browse files
[3.12] gh-115596: Fix ProgramPriorityTests in test_os permanently changing the process priority (GH-115610) (GH-115616)
(cherry picked from commit 90dd653) Co-authored-by: Brian Schubert <[email protected]>
1 parent 9563e46 commit dadc768

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

Lib/test/test_os.py

+15-16
Original file line numberDiff line numberDiff line change
@@ -3485,23 +3485,22 @@ class ProgramPriorityTests(unittest.TestCase):
34853485
"""Tests for os.getpriority() and os.setpriority()."""
34863486

34873487
def test_set_get_priority(self):
3488-
34893488
base = os.getpriority(os.PRIO_PROCESS, os.getpid())
3490-
os.setpriority(os.PRIO_PROCESS, os.getpid(), base + 1)
3491-
try:
3492-
new_prio = os.getpriority(os.PRIO_PROCESS, os.getpid())
3493-
# nice value cap is 19 for linux and 20 for FreeBSD
3494-
if base >= 19 and new_prio <= base:
3495-
raise unittest.SkipTest("unable to reliably test setpriority "
3496-
"at current nice level of %s" % base)
3497-
else:
3498-
self.assertEqual(new_prio, base + 1)
3499-
finally:
3500-
try:
3501-
os.setpriority(os.PRIO_PROCESS, os.getpid(), base)
3502-
except OSError as err:
3503-
if err.errno != errno.EACCES:
3504-
raise
3489+
code = f"""if 1:
3490+
import os
3491+
os.setpriority(os.PRIO_PROCESS, os.getpid(), {base} + 1)
3492+
print(os.getpriority(os.PRIO_PROCESS, os.getpid()))
3493+
"""
3494+
3495+
# Subprocess inherits the current process' priority.
3496+
_, out, _ = assert_python_ok("-c", code)
3497+
new_prio = int(out)
3498+
# nice value cap is 19 for linux and 20 for FreeBSD
3499+
if base >= 19 and new_prio <= base:
3500+
raise unittest.SkipTest("unable to reliably test setpriority "
3501+
"at current nice level of %s" % base)
3502+
else:
3503+
self.assertEqual(new_prio, base + 1)
35053504

35063505

35073506
@unittest.skipUnless(hasattr(os, 'sendfile'), "test needs os.sendfile()")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix ``ProgramPriorityTests`` in ``test_os`` permanently changing the process
2+
priority.

0 commit comments

Comments
 (0)