From 4d1958c717e7e3abab00c1e501f7e8d31897a773 2008-08-09 23:56:53 From: gvaroquaux Date: 2008-08-09 23:56:53 Subject: [PATCH] BUG: redirector_output_trap was not closing properly the stdout. --- diff --git a/IPython/kernel/core/redirector_output_trap.py b/IPython/kernel/core/redirector_output_trap.py index 1c42f7d..fb396ce 100644 --- a/IPython/kernel/core/redirector_output_trap.py +++ b/IPython/kernel/core/redirector_output_trap.py @@ -50,7 +50,7 @@ class RedirectorOutputTrap(OutputTrap): """ Set the hooks: set the redirectors and call the base class. """ self.out_redirector.start() - #self.err_redirector.start() + self.err_redirector.start() OutputTrap.set(self) @@ -60,9 +60,9 @@ class RedirectorOutputTrap(OutputTrap): """ OutputTrap.unset(self) # Flush the redirectors before stopping them - self.on_out_write('') - self.err_redirector.stop() self.on_err_write('') + self.err_redirector.stop() + self.on_out_write('') self.out_redirector.stop() diff --git a/IPython/kernel/core/tests/test_redirectors.py b/IPython/kernel/core/tests/test_redirectors.py index 85310bd..e5bc24e 100644 --- a/IPython/kernel/core/tests/test_redirectors.py +++ b/IPython/kernel/core/tests/test_redirectors.py @@ -1,14 +1,31 @@ +# encoding: utf-8 + """ Test the output capture at the OS level, using file descriptors. """ +__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. +#--------------------------------------------------------------------------- + + import os from cStringIO import StringIO +import sys def test_redirector(): """ Checks that the redirector can be used to do synchronous capture. """ + # Flush the stdout, so as not to have side effects between + # tests. + sys.stdout.flush() + sys.stderr.flush() from IPython.kernel.core.fd_redirector import FDRedirector r = FDRedirector() out = StringIO() @@ -23,13 +40,19 @@ def test_redirector(): raise r.stop() assert out.getvalue() == "".join("%ic\n%i\n" %(i, i) for i in range(10)) + sys.stdout.flush() + sys.stderr.flush() def test_redirector_output_trap(): """ This test check not only that the redirector_output_trap does trap the output, but also that it does it in a gready way, that - is by calling the callabck ASAP. + is by calling the callback ASAP. """ + # Flush the stdout, so as not to have side effects between + # tests. + sys.stdout.flush() + sys.stderr.flush() from IPython.kernel.core.redirector_output_trap import RedirectorOutputTrap out = StringIO() trap = RedirectorOutputTrap(out.write, out.write) @@ -43,8 +66,16 @@ def test_redirector_output_trap(): trap.unset() raise trap.unset() + sys.stdout.flush() + sys.stderr.flush() assert out.getvalue() == "".join("%ic\n%ip\n%i\n" %(i, i, i) for i in range(10)) - + + +if __name__ == '__main__': + print "Testing redirector...", + test_redirector() + test_redirector_output_trap() + print "Done."