##// END OF EJS Templates
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ipython branch. This is not a true merge in the formal sense because all history is not coming over with the files. For a detailed history of the added files, please see the ipython1-dev branch or the svn repository on scipy.org that ipython1-dev came from. More specifically, here is what I have done in this commit: 1) Moved the following by hand ipython1.config -> IPython.config ipython1.kernel -> IPython.kernel ipython1.external -> IPython.external ipython1.core -> IPython.kernel.core ipython1.testutils -> IPython.testing ipython1.tools -> IPython.tools 2) Moved IPython.tools.guid -> IPython1.external.guid 3) Renamed: ipython1 -> IPython IPython.core -> IPython.kernel.core IPython.testutils -> IPython.testing 4) Then did a "bzr add" for all the new stuff. That is all folks!

File last commit:

r1234:52b55407
r1234:52b55407
Show More
output_trap.py
99 lines | 2.4 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):
# Filelike objects to store stdout/stderr text.
self.out = StringIO()
self.err = StringIO()
# 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.
"""
sys.stdout = self._out_save
self.out_set = False
sys.stderr = self._err_save
self.err_set = False
def clear(self):
""" Clear out the buffers.
"""
self.out.close()
self.out = StringIO()
self.err.close()
self.err = StringIO()
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