The ‘py’ lib provides helper classes for capturing IO during execution of a program.

IO Capturing examples

Basic Example:

>>> import py
>>> capture =
>>> print "hello"
>>> out,err = capture.reset()
>>> out.strip() == "hello"

For calling functions you may use a shortcut:

>>> import py
>>> def f(): print "hello"
>>> res, out, err =
>>> out.strip() == "hello"

If you also want to capture writes to the stdout/stderr filedescriptors you may invoke:

>>> import py, sys
>>> capture =, in_=False)
>>> sys.stderr.write("world")
>>> out,err = capture.reset()
>>> err
'world' object reference

Project Versions

Table Of Contents

Previous topic

py.code: higher level python code and introspection objects

Next topic

py.log documentation and musings

This Page