summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2020-03-31 21:45:13 (GMT)
committerGitHub <noreply@github.com>2020-03-31 21:45:13 (GMT)
commit40bfdb1594189f3c0238e5d2098dc3abf114e200 (patch)
treeff58a82653d8dc1aabbb4a1daee4c59a31fc7d29 /Lib
parent2c003eff8fef430f1876adf88e466bcfcbd0cc9e (diff)
downloadcpython-40bfdb1594189f3c0238e5d2098dc3abf114e200.zip
cpython-40bfdb1594189f3c0238e5d2098dc3abf114e200.tar.gz
cpython-40bfdb1594189f3c0238e5d2098dc3abf114e200.tar.bz2
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.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/_bootsubprocess.py26
1 files changed, 15 insertions, 11 deletions
diff --git a/Lib/_bootsubprocess.py b/Lib/_bootsubprocess.py
index 962301a..9c1912f 100644
--- a/Lib/_bootsubprocess.py
+++ b/Lib/_bootsubprocess.py
@@ -6,6 +6,15 @@ subprocess is unavailable. setup.py is not used on Windows.
import os
+def _waitstatus_to_exitcode(status):
+ if os.WIFEXITED(status):
+ return os.WEXITSTATUS(status)
+ elif os.WIFSIGNALED(status):
+ return -os.WTERMSIG(status)
+ else:
+ raise ValueError(f"invalid wait status: {status!r}")
+
+
# distutils.spawn used by distutils.command.build_ext
# calls subprocess.Popen().wait()
class Popen:
@@ -27,15 +36,8 @@ class Popen:
os._exit(1)
else:
# Parent process
- pid, status = os.waitpid(pid, 0)
- if os.WIFSIGNALED(status):
- self.returncode = -os.WTERMSIG(status)
- elif os.WIFEXITED(status):
- self.returncode = os.WEXITSTATUS(status)
- elif os.WIFSTOPPED(status):
- self.returncode = -os.WSTOPSIG(status)
- else:
- raise Exception(f"unknown child process exit status: {status!r}")
+ _, status = os.waitpid(pid, 0)
+ self.returncode = _waitstatus_to_exitcode(status)
return self.returncode
@@ -85,8 +87,10 @@ def check_output(cmd, **kwargs):
try:
# system() spawns a shell
status = os.system(cmd)
- if status:
- raise ValueError(f"Command {cmd!r} failed with status {status!r}")
+ exitcode = _waitstatus_to_exitcode(status)
+ if exitcode:
+ raise ValueError(f"Command {cmd!r} returned non-zero "
+ f"exit status {exitcode!r}")
try:
with open(tmp_filename, "rb") as fp: