##// END OF EJS Templates
Work to address the review comments on Fernando's branch....
Work to address the review comments on Fernando's branch. * Added comment about Magic(object) (r1224) * Moved InteractiveTB.set_mode from IPythonApp -> InteractiveShell (r1229) * Moved pylabtools.py to IPython/lib (r1229) * Cleaned up comments and copyrights in testing (r1233) * Added comment about ip.shell._ofind (r1237) * Removed "Bye." from quitter (r1240). * Refactored and removed :mod:`IPython.utils.genutils` and :mod:`IPython.utils.platutils`. These modules have been replaced by topical focused modules in :mod:`IPython.utils`. * Refactored tests in :mod:`IPython.utils.tests`. * Moved :func:`IPython.testing.tools.temp_pyfile` to :mod:`IPython.utils.io`. * Moved :func:`IPython.testing.tools.cmd2argv` to :func:`IPython.testing.tools.pycmd2argv` and documented the fact that this only works with Python based command line programs. * Created a new :func:`IPython.utils.path.get_ipython_module_path` to use in finding paths to IPython modules.

File last commit:

r1472:dddf1831
r2498:3eae1372
Show More
output_trap.py
107 lines | 2.6 KiB | text/x-python | PythonLexer
# encoding: utf-8
""" Trap stdout/stderr."""
__docformat__ = "restructuredtext en"
#-------------------------------------------------------------------------------
# Copyright (C) 2008 The IPython Development Team
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# Imports
#-------------------------------------------------------------------------------
import sys
from cStringIO import StringIO
class OutputTrap(object):
""" Object which can trap text sent to stdout and stderr.
"""
def __init__(self, out=None, err=None):
# Filelike objects to store stdout/stderr text.
if out is None:
self.out = StringIO()
else:
self.out = out
if err is None:
self.err = StringIO()
else:
self.err = err
# Boolean to check if the stdout/stderr hook is set.
self.out_set = False
self.err_set = False
@property
def out_text(self):
""" Return the text currently in the stdout buffer.
"""
return self.out.getvalue()
@property
def err_text(self):
""" Return the text currently in the stderr buffer.
"""
return self.err.getvalue()
def set(self):
""" Set the hooks.
"""
if sys.stdout is not self.out:
self._out_save = sys.stdout
sys.stdout = self.out
self.out_set = True
if sys.stderr is not self.err:
self._err_save = sys.stderr
sys.stderr = self.err
self.err_set = True
def unset(self):
""" Remove the hooks.
"""
if self.out_set:
sys.stdout = self._out_save
self.out_set = False
if self.err_set:
sys.stderr = self._err_save
self.err_set = False
def clear(self):
""" Clear out the buffers.
"""
self.out.reset()
self.out.truncate()
self.err.reset()
self.err.truncate()
def add_to_message(self, message):
""" Add the text from stdout and stderr to the message from the
interpreter to its listeners.
Parameters
----------
message : dict
"""
out_text = self.out_text
if out_text:
message['stdout'] = out_text
err_text = self.err_text
if err_text:
message['stderr'] = err_text