def unset(self): """ Remove the hooks: call the base class and stop the redirectors. """ OutputTrap.unset(self) # Flush the redirectors before stopping them self.on_err_write('') self.err_redirector.stop() self.on_out_write('') self.out_redirector.stop()
def __init__(self, out_callback, err_callback): """ out_callback : callable called when there is output in the stdout err_callback : callable called when there is output in the stderr """ # Callback invoked on write to stdout and stderr self.out_callback = out_callback self.err_callback = err_callback # File descriptor redirectors, to capture non-Python # output. self.out_redirector = FDRedirector(STDOUT) self.err_redirector = FDRedirector(STDERR) # Call the base class with file like objects that will trigger # our callbacks OutputTrap.__init__(self, out=FileLike(self.on_out_write), err=FileLike(self.on_err_write), )
def __init__(self, out_callback, err_callback): """ out_callback : callable called when there is output in the stdout err_callback : callable called when there is output in the stderr """ # Callback invoked on write to stdout and stderr self.out_callback = out_callback self.err_callback = err_callback # File descriptor redirectors, to capture non-Python # output. self.out_redirector = FDRedirector(STDOUT) self.err_redirector = FDRedirector(STDERR) # Call the base class with file like objects that will trigger # our callbacks OutputTrap.__init__( self, out=FileLike(self.on_out_write), err=FileLike(self.on_err_write), )
def set(self): """ Set the hooks: set the redirectors and call the base class. """ self.out_redirector.start() self.err_redirector.start() OutputTrap.set(self)
def __init__(self, user_ns=None, global_ns=None, translator=None, magic=None, display_formatters=None, traceback_formatters=None, output_trap=None, history=None, message_cache=None, filename='<string>', config=None): # The local/global namespaces for code execution local_ns = user_ns # compatibility name if local_ns is None: local_ns = {} self.user_ns = local_ns # The local namespace if global_ns is None: global_ns = {} self.user_global_ns = global_ns # An object that will translate commands into executable Python. # The current translator does not work properly so for now we are going # without! # if translator is None: # from IPython.kernel.core.translator import IPythonTranslator # translator = IPythonTranslator() self.translator = translator # An object that maintains magic commands. if magic is None: from IPython.kernel.core.magic import Magic magic = Magic(self) self.magic = magic # A list of formatters for the displayhook. if display_formatters is None: display_formatters = default_display_formatters() self.display_formatters = display_formatters # A list of formatters for tracebacks. if traceback_formatters is None: traceback_formatters = default_traceback_formatters() self.traceback_formatters = traceback_formatters # The object trapping stdout/stderr. if output_trap is None: from IPython.kernel.core.output_trap import OutputTrap output_trap = OutputTrap() self.output_trap = output_trap # An object that manages the history. if history is None: from IPython.kernel.core.history import InterpreterHistory history = InterpreterHistory() self.history = history self.get_history_item = history.get_history_item self.get_history_input_cache = history.get_input_cache self.get_history_input_after = history.get_input_after # An object that caches all of the return messages. if message_cache is None: from IPython.kernel.core.message_cache import SimpleMessageCache message_cache = SimpleMessageCache() self.message_cache = message_cache # The "filename" of the code that is executed in this interpreter. self.filename = filename # An object that contains much configuration information. if config is None: # fixme: Move this constant elsewhere! config = Bunch(ESC_MAGIC='%') self.config = config # Hook managers. # fixme: make the display callbacks configurable. In the meantime, # enable macros. self.display_trap = DisplayTrap( formatters=self.display_formatters, callbacks=[self._possible_macro], ) self.traceback_trap = TracebackTrap( formatters=self.traceback_formatters) # This is used temporarily for reformating exceptions in certain # cases. It will go away once the ultraTB stuff is ported # to ipython1 self.tbHandler = ultraTB.FormattedTB(color_scheme='NoColor', mode='Context', tb_offset=2) # An object that can compile commands and remember __future__ # statements. self.command_compiler = codeop.CommandCompiler() # A replacement for the raw_input() and input() builtins. Change these # attributes later to configure them. self.raw_input_builtin = raw_input self.input_builtin = input # The number of the current cell. self.current_cell_number = 1 # Initialize cache, set in/out prompts and printing system self.outputcache = CachedOutput(self, rc.cache_size, rc.pprint, input_sep=rc.separate_in, output_sep=rc.separate_out, output_sep2=rc.separate_out2, ps1=rc.prompt_in1, ps2=rc.prompt_in2, ps_out=rc.prompt_out, pad_left=rc.prompts_pad_left) # Need to decide later if this is the right approach, but clients # commonly use sys.ps1/2, so it may be best to just set them here sys.ps1 = self.outputcache.prompt1.p_str sys.ps2 = self.outputcache.prompt2.p_str # This is the message dictionary assigned temporarily when running the # code. self.message = None self.setup_namespace()