Skip to content

Commit d9ba434

Browse files
committed
Extract spawn._handle_error for trapping the relevant error contexts. Re-use error handling function of subprocess.check_call to cause non-zero exits to raise an exception.
1 parent 7aa5abe commit d9ba434

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

distutils/spawn.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import sys
1010
import os
1111
import subprocess
12+
import contextlib
1213

1314
from distutils.errors import DistutilsPlatformError, DistutilsExecError
1415
from distutils.debug import DEBUG
@@ -71,21 +72,21 @@ def spawn(cmd, search_path=1, verbose=0, dry_run=0):
7172
env = dict(os.environ,
7273
MACOSX_DEPLOYMENT_TARGET=cur_target)
7374

75+
with _handle_error(cmd):
76+
subprocess.check_call(cmd, env=env)
77+
78+
79+
@contextlib.contextmanager
80+
def _handle_error(cmd):
81+
rep = cmd if DEBUG else cmd[0]
7482
try:
75-
proc = subprocess.Popen(cmd, env=env)
76-
proc.wait()
77-
exitcode = proc.returncode
83+
yield
84+
except subprocess.CalledProcessError as exc:
85+
msg = "command %r failed with exit code %s" % (rep, exc.returncode)
86+
raise DistutilsExecError(msg) from exc
7887
except OSError as exc:
79-
if not DEBUG:
80-
cmd = cmd[0]
81-
raise DistutilsExecError(
82-
"command %r failed: %s" % (cmd, exc.args[-1])) from exc
83-
84-
if exitcode:
85-
if not DEBUG:
86-
cmd = cmd[0]
87-
raise DistutilsExecError(
88-
"command %r failed with exit code %s" % (cmd, exitcode))
88+
msg = "command %r failed: %s" % (rep, exc.args[-1])
89+
raise DistutilsExecError(msg) from exc
8990

9091

9192
def find_executable(executable, path=None):

0 commit comments

Comments
 (0)