Skip to main content

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

  1. Manual error handling/formatting:
    • ➕ customizable formatting
    • ➕ no dependencies
    • ➖ ad-hoc code may not be tested as thoroughly
  2. Don't capture stderr:
    • ➕ no dependencies
    • ➖ output to terminal in non-error case may be verbose, if sub-process stderr outputs regardless
    • ➖ if the error is re-raised after other operations, which output corresponds to the failed sub-process may not be nearby, or obvious
  3. Wrap subprocess.run instead of using a context manager
    • ➕ no need to pass capture_output=True, check=True
    • ➖ IDEs do not understand "transparent function wrappers" well, so autocomplete and type hints are compromised

Metadata

Release files for subprocess-check 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for subprocess-check 0.1.0
File Size Uploaded
subprocess-check-0.1.0.tar.gz 5.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for subprocess-check 0.1.0
File Interpreter ABI Platform
subprocess_check-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 10.4 kB

Release files / subprocess-check-0.1.0.tar.gz

Download URL subprocess-check-0.1.0.tar.gz
Size 5.8 kB
Tags Source
SHA-256 checksum
How to use checksums
b6b06e8439b3d642d7f5fcc9ada1ab78065c6557f89ea8f3beb9a36e67dc0eaf
BLAKE2b-256 checksum
How to use checksums
de2ff931b8fa4164a0e0d9e430438c29f0f9a6d9952068b4c12d09657cfd1de8
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / subprocess_check-0.1.0-py3-none-any.whl

Download URL subprocess_check-0.1.0-py3-none-any.whl
Size 4.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d7878caa4c5d017611f307d6845a43e6c1a1b97b7600e178f781b69974e6429e
BLAKE2b-256 checksum
How to use checksums
e7a9f064d45a7842bd8bb5089b29fd7ee06b5592fcc695676f960e9d4f155414
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page