diff options
author | Victor Stinner <vstinner@python.org> | 2020-04-01 18:49:29 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-01 18:49:29 +0200 |
commit | 65a796e5272f61b42792d3a8c69686558c1872c5 (patch) | |
tree | 138d64a8dd04ab4d1cac2eb5c415aa10e0bbe00f /Lib/test/test_os.py | |
parent | 5dd836030e0e399b21ab0865ae0d93934bdb3930 (diff) | |
download | cpython-git-65a796e5272f61b42792d3a8c69686558c1872c5.tar.gz |
bpo-40094: Add os.waitstatus_to_exitcode() (GH-19201)
Add os.waitstatus_to_exitcode() function to convert a wait status to an
exitcode.
Suggest waitstatus_to_exitcode() usage in the documentation when
appropriate.
Use waitstatus_to_exitcode() in:
* multiprocessing, os, subprocess and _bootsubprocess modules;
* test.support.wait_process();
* setup.py: run_command();
* and many tests.
Diffstat (limited to 'Lib/test/test_os.py')
-rw-r--r-- | Lib/test/test_os.py | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index be85616ff8..142cfea364 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -2794,6 +2794,35 @@ class PidTests(unittest.TestCase): pid = os.spawnv(os.P_NOWAIT, FakePath(args[0]), args) support.wait_process(pid, exitcode=0) + def test_waitstatus_to_exitcode(self): + exitcode = 23 + filename = support.TESTFN + self.addCleanup(support.unlink, filename) + + with open(filename, "w") as fp: + print(f'import sys; sys.exit({exitcode})', file=fp) + fp.flush() + args = [sys.executable, filename] + pid = os.spawnv(os.P_NOWAIT, args[0], args) + + pid2, status = os.waitpid(pid, 0) + self.assertEqual(os.waitstatus_to_exitcode(status), exitcode) + self.assertEqual(pid2, pid) + + # Skip the test on Windows + @unittest.skipUnless(hasattr(signal, 'SIGKILL'), 'need signal.SIGKILL') + def test_waitstatus_to_exitcode_kill(self): + signum = signal.SIGKILL + args = [sys.executable, '-c', + f'import time; time.sleep({support.LONG_TIMEOUT})'] + pid = os.spawnv(os.P_NOWAIT, args[0], args) + + os.kill(pid, signum) + + pid2, status = os.waitpid(pid, 0) + self.assertEqual(os.waitstatus_to_exitcode(status), -signum) + self.assertEqual(pid2, pid) + class SpawnTests(unittest.TestCase): def create_args(self, *, with_env=False, use_bytes=False): |