##// END OF EJS Templates
More work on refactoring things into components....
More work on refactoring things into components. * Prefilters have been componentized (prefilter.py). * We are not including auto_attr from NiPy in IPython.utils. * All components are now using auto_attr when resolving refs to other components. * The AliasManager component has been updated to reflect the prefilter refactor. * prefilter.splitUserInput has been moved to IPython.core.splitinput.split_user_input as we are using this in other places in the IPython code base.

File last commit:

r2244:13603eb6
r2244:13603eb6
Show More
display_trap.py
72 lines | 1.9 KiB | text/x-python | PythonLexer
#!/usr/bin/env python
# encoding: utf-8
"""
A context manager for handling sys.displayhook.
Authors:
* Robert Kern
* Brian Granger
"""
#-----------------------------------------------------------------------------
# Copyright (C) 2008-2009 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 IPython.core.component import Component
from IPython.utils.autoattr import auto_attr
#-----------------------------------------------------------------------------
# Classes and functions
#-----------------------------------------------------------------------------
class DisplayTrap(Component):
"""Object to manage sys.displayhook.
This came from IPython.core.kernel.display_hook, but is simplified
(no callbacks or formatters) until more of the core is refactored.
"""
def __init__(self, parent, hook):
super(DisplayTrap, self).__init__(parent, None, None)
self.hook = hook
self.old_hook = None
@auto_attr
def shell(self):
shell = Component.get_instances(
root=self.root,
klass='IPython.core.iplib.InteractiveShell'
)[0]
return shell
def __enter__(self):
self.set()
return self
def __exit__(self, type, value, traceback):
self.unset()
return True
def set(self):
"""Set the hook."""
if sys.displayhook is not self.hook:
self.old_hook = sys.displayhook
sys.displayhook = self.hook
def unset(self):
"""Unset the hook."""
sys.displayhook = self.old_hook