From a23dbf886569b5f1f382614008d01d824916c91c 2008-08-01 05:20:36 From: Gael Varoquaux Date: 2008-08-01 05:20:36 Subject: [PATCH] Make the OS-level stdout/stderr capture work in the frontends. --- diff --git a/IPython/frontend/prefilterfrontend.py b/IPython/frontend/prefilterfrontend.py index 45532d4..11b7cdd 100644 --- a/IPython/frontend/prefilterfrontend.py +++ b/IPython/frontend/prefilterfrontend.py @@ -21,8 +21,7 @@ from linefrontendbase import LineFrontEndBase, common_prefix from IPython.ipmaker import make_IPython from IPython.ipapi import IPApi -from IPython.kernel.core.file_like import FileLike -from IPython.kernel.core.output_trap import OutputTrap +from IPython.kernel.core.redirector_output_trap import RedirectorOutputTrap from IPython.genutils import Term import pydoc @@ -60,17 +59,9 @@ class PrefilterFrontEnd(LineFrontEndBase): # Make sure the raw system call doesn't get called, as we don't # have a stdin accessible. self._ip.system = xterm_system - # Redefine a serie of magics to avoid os.system: - # FIXME: I am redefining way too much magics. - for alias_name, (_, alias_value) in \ - _ip.IP.shell.alias_table.iteritems(): - magic = lambda s : _ip.magic('sx %s %s' % (alias_value, s)) - setattr(_ip.IP, 'magic_%s' % alias_name, magic) - # FIXME: I should create a real file-like object dedicated to this - # terminal - self.shell.output_trap = OutputTrap( - out=FileLike(write_callback=self.write), - err=FileLike(write_callback=self.write), + self.shell.output_trap = RedirectorOutputTrap( + out_callback=self.write, + err_callback=self.write, ) # Capture and release the outputs, to make sure all the # shadow variables are set diff --git a/IPython/kernel/core/redirector_output_trap.py b/IPython/kernel/core/redirector_output_trap.py index 23fe145..1c42f7d 100644 --- a/IPython/kernel/core/redirector_output_trap.py +++ b/IPython/kernel/core/redirector_output_trap.py @@ -59,7 +59,10 @@ class RedirectorOutputTrap(OutputTrap): redirectors. """ OutputTrap.unset(self) + # Flush the redirectors before stopping them + self.on_out_write('') self.err_redirector.stop() + self.on_err_write('') self.out_redirector.stop() @@ -69,11 +72,22 @@ class RedirectorOutputTrap(OutputTrap): def on_out_write(self, string): """ Callback called when there is some Python output on stdout. """ - self.out_callback(self.out_redirector.getvalue() + string) + try: + self.out_callback(self.out_redirector.getvalue() + string) + except: + # If tracebacks are happening and we can't see them, it is + # quasy impossible to debug + self.unset() + raise def on_err_write(self, string): """ Callback called when there is some Python output on stderr. """ - self.err_callback(self.err_redirector.getvalue() + string) - + try: + self.err_callback(self.err_redirector.getvalue() + string) + except: + # If tracebacks are happening and we can't see them, it is + # quasy impossible to debug + self.unset() + raise