subprocess wrapper that shows sub-process stdout/stderr in error text.
Project description
subprocess-check
Provides a wrapper that traces sub-process output, if captured.
Uses
Easier debugging when capturing output in a sub-process call
Before:
import subprocess
result = subprocess.run(
"echo error message >&2 && exit 1", shell=True, capture_output=True, check=True
)
Since the stderr output of the sub-process has been captured, we only see the command and exit code:
$ python t.py
Traceback (most recent call last):
File "t.py", line 3, in <module>
result = subprocess.run(
File "lib/python3.8/subprocess.py", line 512, in run
raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command 'echo error message >&2 && exit 1' returned non-zero exit status 1.
After:
import subprocess
from subprocess_check import output_in_errors
with output_in_errors():
result = subprocess.run(
"echo error message >&2 && exit 1", shell=True, capture_output=True, check=True
)
The stdout/stderr is traced right next to the exit code and command:
$ python t.py
Traceback (most recent call last):
File "t.py", line 6, in <module>
result = subprocess.run(
File "lib/python3.8/site-packages/subprocess_check/_util.py", line 108, in wrap_subprocess_exception
raise CalledProcessErrorWithOutput(exc_value).with_traceback(tb) from None
File "lib/python3.8/subprocess.py", line 512, in run
raise CalledProcessError(retcode, process.args,
subprocess_check._util.CalledProcessErrorWithOutput: Command 'echo error message >&2 && exit 1' returned non-zero exit status 1. Output captured:
stdout: None
stderr:
error message
Alternatives
- Manual error handling/formatting:
- :heavy_plus_sign: customizable formatting
- :heavy_plus_sign: no dependencies
- :heavy_minus_sign: ad-hoc code may not be tested as thoroughly
- Don't capture stderr:
- :heavy_plus_sign: no dependencies
- :heavy_minus_sign: output to terminal in non-error case may be verbose, if sub-process stderr outputs regardless
- :heavy_minus_sign: if the error is re-raised after other operations, which output corresponds to the failed sub-process may not be nearby, or obvious
- Wrap subprocess.run instead of using a context manager
- :heavy_plus_sign: no need to pass
capture_output=True, check=True
- :heavy_minus_sign: IDEs do not understand "transparent function wrappers" well, so autocomplete and type hints are compromised
- :heavy_plus_sign: no need to pass
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file subprocess-check-0.1.0.tar.gz
.
File metadata
- Download URL: subprocess-check-0.1.0.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.8.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b6b06e8439b3d642d7f5fcc9ada1ab78065c6557f89ea8f3beb9a36e67dc0eaf |
|
MD5 | 71b235bdff2bf36f864723c5130bb717 |
|
BLAKE2b-256 | de2ff931b8fa4164a0e0d9e430438c29f0f9a6d9952068b4c12d09657cfd1de8 |
File details
Details for the file subprocess_check-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: subprocess_check-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.8.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d7878caa4c5d017611f307d6845a43e6c1a1b97b7600e178f781b69974e6429e |
|
MD5 | af851abc6a96bb16d959e6929ff41425 |
|
BLAKE2b-256 | e7a9f064d45a7842bd8bb5089b29fd7ee06b5592fcc695676f960e9d4f155414 |