File tree Expand file tree Collapse file tree 2 files changed +19
-12
lines changed Expand file tree Collapse file tree 2 files changed +19
-12
lines changed Original file line number Diff line number Diff line change
1
+ Properly handle `subprocess.CalledProcessError `. Catch the exception, print the error, and exit with the `.returncode ` attribute value.
Original file line number Diff line number Diff line change 9
9
import logging
10
10
import os
11
11
import pathlib
12
+ import subprocess
12
13
import sys
13
14
import typing
14
15
from collections .abc import Iterator
@@ -178,14 +179,24 @@ def run(
178
179
capture : bool = False ,
179
180
interactive : bool = False ,
180
181
** kwargs ,
181
- ) -> CompletedProcess [str ]:
182
+ ) -> CompletedProcess [str ] | None :
182
183
"""
183
184
Run a subprocess.
184
185
185
186
Either in a virtualenv context if one was configured or the system context.
186
187
"""
187
- if self .venv :
188
- return self .venv .run (
188
+ try :
189
+ if self .venv :
190
+ return self .venv .run (
191
+ * cmdline ,
192
+ check = check ,
193
+ timeout_secs = timeout_secs ,
194
+ no_output_timeout_secs = no_output_timeout_secs ,
195
+ capture = capture ,
196
+ interactive = interactive ,
197
+ ** kwargs ,
198
+ )
199
+ return self ._run (
189
200
* cmdline ,
190
201
check = check ,
191
202
timeout_secs = timeout_secs ,
@@ -194,15 +205,10 @@ def run(
194
205
interactive = interactive ,
195
206
** kwargs ,
196
207
)
197
- return self ._run (
198
- * cmdline ,
199
- check = check ,
200
- timeout_secs = timeout_secs ,
201
- no_output_timeout_secs = no_output_timeout_secs ,
202
- capture = capture ,
203
- interactive = interactive ,
204
- ** kwargs ,
205
- )
208
+ except subprocess .CalledProcessError as exc :
209
+ self .error (str (exc ))
210
+ self .exit (exc .returncode )
211
+ return None
206
212
207
213
@contextmanager
208
214
def chdir (self , path : pathlib .Path ) -> Iterator [pathlib .Path ]:
You can’t perform that action at this time.
0 commit comments