8000 bpo-40094: Add _bootsubprocess._waitstatus_to_exitcode (GH-19264) · python/cpython@40bfdb1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 40bfdb1

Browse files
authored
bpo-40094: Add _bootsubprocess._waitstatus_to_exitcode (GH-19264)
* Add _waitstatus_to_exitcode() helper function to _bootsubprocess. * Enhance check_output() error message if the command fails. _bootsubprocess no longer handles WIFSTOPPED() case: it now raises a ValueError.
1 parent 2c003ef commit 40bfdb1

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

Lib/_bootsubprocess.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66
import os
77

88

9+
def _waitstatus_to_exitcode(status):
10+
if os.WIFEXITED(status):
11+
return os.WEXITSTATUS(status)
12+
elif os.WIFSIGNALED(status):
13+
return -os.WTERMSIG(status)
14+
else:
15+
raise ValueError(f"invalid wait status: {status!r}")
16+
17+
918
# distutils.spawn used by distutils.command.build_ext
1019
# calls subprocess.Popen().wait()
1120
class Popen:
@@ -27,15 +36,8 @@ def wait(self):
2736
os._exit(1)
2837
else:
2938
# Parent process
30-
pid, status = os.waitpid(pid, 0)
31-
if os.WIFSIGNALED(status):
32-
self.returncode = -os.WTERMSIG(status)
33-
elif os.WIFEXITED(status):
34-
self.returncode = os.WEXITSTATUS(status)
35-
elif os.WIFSTOPPED(status):
36-
self.returncode = -os.WSTOPSIG(status)
37-
else:
38-
raise Exception(f"unknown child process exit status: {status!r}")
39+
_, status = os.waitpid(pid, 0)
40+
self.returncode = _waitstatus_to_exitcode(status)
3941

4042
return self.returncode
4143

@@ -85,8 +87,10 @@ def check_output(cmd, **kwargs):
8587
try:
8688
# system() spawns a shell
8789
status = os.system(cmd)
88-
if status:
89-
raise ValueError(f"Command {cmd!r} failed with status {status!r}")
90+
exitcode = _waitstatus_to_exitcode(status)
91+
if exitcode:
92+
raise ValueError(f"Command {cmd!r} returned non-zero "
93+
f"exit status {exitcode!r}")
9094

9195
try:
9296
with open(tmp_filename, "rb") as fp:

0 commit comments

Comments
 (0)
0