Catches/silences stdout/stderr output.
Project description
Provides a context manager that catches/suppresses output from sys.stderr and sys.stdout (StdOutCatcher, StdErrCatcher). Also provides an easy way to gather both stdout and stderr from processes while optionally piping stdin to the process as a str or bytes.
API
StdOutCatcher / StdErrCatcher
StdOutCatcher(escaped=False, max_length=0)
This will suppress any output running through sys.stdout or sys.stderr, and save it in an attribute for possible future use.
Arguments
escaped: If truthy, output is “encoded” using repr(), but without quotes. Default: False
max_length: If non-zero, final output will not exceed max_length. Once max_length is reached, further write() calls will be ignored. Default: 0
Usage
from outputcatcher import StdErrCatcher, StdOutCatcher
# Catching stdout
with StdOutCatcher() as fakeout:
print('This is a test. you shouldn\'t see it right away.')
print('Captured stdout: {}'.format(fakeout.output))
# Catching stderr
with StdErrCatcher() as fakeerr:
print('Testing stderr output.', file=sys.stderr)
print('Captured stderr: {}'.format(fakeerr.output))
ProcessOutput
ProcessOutput(args, stdin_data=None, timeout=None, **popenkwargs)
This runs an external process using subprocess.Popen and gathers both the stdout and stderr output in an attribute for future use. stdin data can be piped to the process initially, by providing the data as a str or bytes during initialization.
After initializing a ProcessOutput object with a command to run, and optional stdin input data, it can either be used as a context manager or the run() method must be called.
timeout is passed to self.proc.wait() before returning the output.
Note: As of Python 3.5, subprocess.run() can do much of this through the input argument and the subprocess.CompletedProcess return value. Though ProcessOutput does provide a handy method for iterating over output as it is received (iter_stdout, and iter_stderr).
Arguments
args: Command arguments, same as subprocess.Popen.
stdin_data: str or bytes to send to command as stdin. Default: None
timeout: Time to wait for process after collecting data. Default: None
**popenkwargs: Any extra kwargs for Popen(). stdin, stdout, and stderr are ignored.
Usage
from outputcatcher import ProcessOutput
# Basic usage:
with ProcessOutput(['ls']) as p:
print(p.stdout.decode())
# Checking for stdout and stderr:
with ProcessOutput(['ls', '/totally_nonexistent_dir']) as p:
if p.stdout:
print('Wow, it really does exist: {}'.format(p.stdout.decode()))
else:
print(p.stderr.decode())
# Without a context manager:
p = ProcessOutput(['ls'])
stdout, stderr = p.run()
# Sending stdin data to a process:
stdin_data = 'Hello cat!'
with ProcessOutput(['cat'], stdin_data=stdin_data) as p:
assert p.stdout.decode() == stdin_data
# cat received the data, and piped it back.
print(p.stdout.decode())
# Iterating over stdout data:
p = ProcessOutput(['ls'])
for line in p.iter_stdout():
print(line)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file OutputCatcher-0.0.9.tar.gz
.
File metadata
- Download URL: OutputCatcher-0.0.9.tar.gz
- Upload date:
- Size: 9.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: Python-urllib/3.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 033cf84231e0106bc59c98e556fca043bc44e0838ec938c16bd55ddfc4608d59 |
|
MD5 | 4c34209bd81e0dab7b9796bd746cc640 |
|
BLAKE2b-256 | aa075272dcd0114fc2a9edfa4e1545bc12f5c4f830e7c6e67f15d4faa946820f |