Skip to content

Commit cd0a071

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

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
@@ -3320,23 +3320,22 @@ class ProgramPriorityTests(unittest.TestCase):
33203320
"""Tests for os.getpriority() and os.setpriority()."""
33213321

33223322
def test_set_get_priority(self):
3323-
33243323
base = os.getpriority(os.PRIO_PROCESS, os.getpid())
3325-
os.setpriority(os.PRIO_PROCESS, os.getpid(), base + 1)
3326-
try:
3327-
new_prio = os.getpriority(os.PRIO_PROCESS, os.getpid())
3328-
# nice value cap is 19 for linux and 20 for FreeBSD
3329-
if base >= 19 and new_prio <= base:
3330-
raise unittest.SkipTest("unable to reliably test setpriority "
3331-
"at current nice level of %s" % base)
3332-
else:
3333-
self.assertEqual(new_prio, base + 1)
3334-
finally:
3335-
try:
3336-
os.setpriority(os.PRIO_PROCESS, os.getpid(), base)
3337-
except OSError as err:
3338-
if err.errno != errno.EACCES:
3339-
raise
3324+
code = f"""if 1:
3325+
import os
3326+
os.setpriority(os.PRIO_PROCESS, os.getpid(), {base} + 1)
3327+
print(os.getpriority(os.PRIO_PROCESS, os.getpid()))
3328+
"""
3329+
3330+
# Subprocess inherits the current process' priority.
3331+
_, out, _ = assert_python_ok("-c", code)
3332+
new_prio = int(out)
3333+
# nice value cap is 19 for linux and 20 for FreeBSD
3334+
if base >= 19 and new_prio <= base:
3335+
raise unittest.SkipTest("unable to reliably test setpriority "
3336+
"at current nice level of %s" % base)
3337+
else:
3338+
self.assertEqual(new_prio, base + 1)
33403339

33413340

33423341
@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)