Capture stdout/stderr and optionally release when an exception occurs.
Project description
Capture stdout/stderr and optionally release when an exception occurs.
from abduct import captured, stdout, stderr
with captured(stdout()) as out:
...
with captured(stdout(), stderr()) as (out, err):
...
Installation:
$ pip install abduct
When stdout or stderr is captured, the related sys.stdout or sys.stderr object is replaced with a StringIO object for the life of the context.
Examples
It’s often useful to capture the output of a block of code. Abduct makes this easy:
with captured(stdout()) as out:
print('hello!')
assert out.getvalue() == 'hello!'
Sometimes you may want to hide the output of some code unless something goes wrong. In this case, simply specify release_on_exception=True:
with captured(stdout(release_on_exception=True)):
if blow_up:
raise RuntimeError('Oh noes!')
In this case, Oh noes! will be printed on stdout if blow_up is triggered.
Changelog
1.0.4
Fixed Travis release criteria.
1.0.3
Refactored test runner.
1.0.2
Fixed README and description.
1.0.1
Travis config now defers to tox.
Added examples to README.
1.0.0
Actual working code. Yay!
0.0.1
Initial release.
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
Built Distribution
Hashes for abduct-1.0.4-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3ec4ebd894c85626faf0e422a60addefa28ff6570ec92c5fc935d7c63c5b3d89 |
|
MD5 | 70c090545aadf9e24aea2e9582ead4c0 |
|
BLAKE2b-256 | 6bbf75033cfdc5c1be5ef63f6d23bd16426023bc2b14e649c8ad84ab1647126a |