Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 39 additions & 8 deletions Lib/test/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -2701,7 +2701,7 @@ def test_getppid(self):
# We are the parent of our subprocess
self.assertEqual(int(stdout), os.getpid())

def check_waitpid(self, code, exitcode):
def check_waitpid(self, code, exitcode, callback=None):
if sys.platform == 'win32':
# On Windows, os.spawnv() simply joins arguments with spaces:
# arguments need to be quoted
Expand All @@ -2710,12 +2710,13 @@ def check_waitpid(self, code, exitcode):
args = [sys.executable, '-c', code]
pid = os.spawnv(os.P_NOWAIT, sys.executable, args)

if callback is not None:
callback(pid)

# don't use support.wait_process() to test directly os.waitpid()
# and os.waitstatus_to_exitcode()
pid2, status = os.waitpid(pid, 0)
if sys.platform == 'win32':
self.assertEqual(status, exitcode << 8)
else:
self.assertTrue(os.WIFEXITED(status), status)
self.assertEqual(os.WEXITSTATUS(status), exitcode)
self.assertEqual(os.waitstatus_to_exitcode(status), exitcode)
self.assertEqual(pid2, pid)

# TODO: RUSTPYTHON (AttributeError: module 'os' has no attribute 'spawnv')
Expand All @@ -2725,18 +2726,48 @@ def test_waitpid(self):

# TODO: RUSTPYTHON (AttributeError: module 'os' has no attribute 'spawnv')
@unittest.expectedFailure
def test_waitpid_exitcode(self):
def test_waitstatus_to_exitcode(self):
exitcode = 23
code = f'import sys; sys.exit({exitcode})'
self.check_waitpid(code, exitcode=exitcode)

with self.assertRaises(TypeError):
os.waitstatus_to_exitcode(0.0)

@unittest.skipUnless(sys.platform == 'win32', 'win32-specific test')
def test_waitpid_windows(self):
# bpo-40138: test os.waitpid() with exit code larger than INT_MAX.
# bpo-40138: test os.waitpid() and os.waitstatus_to_exitcode()
# with exit code larger than INT_MAX.
STATUS_CONTROL_C_EXIT = 0xC000013A
code = f'import _winapi; _winapi.ExitProcess({STATUS_CONTROL_C_EXIT})'
self.check_waitpid(code, exitcode=STATUS_CONTROL_C_EXIT)

@unittest.skipUnless(sys.platform == 'win32', 'win32-specific test')
def test_waitstatus_to_exitcode_windows(self):
max_exitcode = 2 ** 32 - 1
for exitcode in (0, 1, 5, max_exitcode):
self.assertEqual(os.waitstatus_to_exitcode(exitcode << 8),
exitcode)

# invalid values
with self.assertRaises(ValueError):
os.waitstatus_to_exitcode((max_exitcode + 1) << 8)
with self.assertRaises(OverflowError):
os.waitstatus_to_exitcode(-1)

# Skip the test on Windows
@unittest.skipUnless(hasattr(signal, 'SIGKILL'), 'need signal.SIGKILL')
# TODO: RUSTPYTHON (AttributeError: module 'os' has no attribute 'spawnv')
@unittest.expectedFailure
def test_waitstatus_to_exitcode_kill(self):
code = f'import time; time.sleep({support.LONG_TIMEOUT})'
signum = signal.SIGKILL

def kill_process(pid):
os.kill(pid, signum)

self.check_waitpid(code, exitcode=-signum, callback=kill_process)


class SpawnTests(unittest.TestCase):
def create_args(self, *, with_env=False, use_bytes=False):
Expand Down