Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-115596: Fix ProgramPriorityTests in test_os permanently changing the process priority #115610

Merged
merged 6 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 15 additions & 16 deletions Lib/test/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix ``ProgramPriorityTests`` in ``test_os`` permanently changing the process
priority.