From 86b6e6122a9a50e647c73ab86ed98c0ad1d6c596 2008-07-20 19:21:41
From: Gael Varoquaux <gael.varoquaux@normalesup.org>
Date: 2008-07-20 19:21:41
Subject: [PATCH] Synchronous stdout/stderr output.

---

diff --git a/IPython/frontend/wx/console_widget.py b/IPython/frontend/wx/console_widget.py
index d224207..48900c2 100644
--- a/IPython/frontend/wx/console_widget.py
+++ b/IPython/frontend/wx/console_widget.py
@@ -221,6 +221,7 @@ class ConsoleWidget(editwindow.EditWindow):
                 segments.pop(i)
                 
         self.GotoPos(self.GetLength())
+        wx.Yield()
     
     
     def new_prompt(self, prompt):
@@ -350,6 +351,7 @@ class ConsoleWidget(editwindow.EditWindow):
             if event.KeyCode in (13, wx.WXK_NUMPAD_ENTER) and \
                         event.Modifiers in (wx.MOD_NONE, wx.MOD_WIN):
                 catched = True
+                self.CallTipCancel()
                 self.write('\n')
                 self._on_enter()
 
diff --git a/IPython/frontend/wx/wx_frontend.py b/IPython/frontend/wx/wx_frontend.py
index 9e4d200..1663a68 100644
--- a/IPython/frontend/wx/wx_frontend.py
+++ b/IPython/frontend/wx/wx_frontend.py
@@ -41,7 +41,7 @@ _RUNNING_BUFFER_MARKER = 31
 class IPythonWxController(PrefilterFrontEnd, ConsoleWidget):
 
     output_prompt = \
-    '\n\x01\x1b[0;31m\x02Out[\x01\x1b[1;31m\x02%i\x01\x1b[0;31m\x02]: \x01\x1b[0m\x02'
+    '\x01\x1b[0;31m\x02Out[\x01\x1b[1;31m\x02%i\x01\x1b[0;31m\x02]: \x01\x1b[0m\x02'
   
     #--------------------------------------------------------------------------
     # Public API
@@ -125,9 +125,9 @@ class IPythonWxController(PrefilterFrontEnd, ConsoleWidget):
             self.MarkerAdd(i, 31)
         # Update the display:
         wx.Yield()
-        # Remove the trailing "\n" for cleaner display
-        self.SetSelection(self.GetLength()-1, self.GetLength())
-        self.ReplaceSelection('')
+        ## Remove the trailing "\n" for cleaner display
+        #self.SetSelection(self.GetLength()-1, self.GetLength())
+        #self.ReplaceSelection('')
         self.GotoPos(self.GetLength())
         PrefilterFrontEnd.execute(self, python_string, raw_string=raw_string)
 
diff --git a/IPython/kernel/core/interpreter.py b/IPython/kernel/core/interpreter.py
index 6e1e8cd..f7d906c 100644
--- a/IPython/kernel/core/interpreter.py
+++ b/IPython/kernel/core/interpreter.py
@@ -20,13 +20,11 @@ __docformat__ = "restructuredtext en"
 #-------------------------------------------------------------------------------
 
 # Standard library imports.
-from compiler.ast import Discard
 from types import FunctionType
 
 import __builtin__
 import codeop
 import compiler
-import pprint
 import sys
 import traceback
 
diff --git a/IPython/kernel/core/sync_output_trap.py b/IPython/kernel/core/sync_output_trap.py
new file mode 100644
index 0000000..31b204a
--- /dev/null
+++ b/IPython/kernel/core/sync_output_trap.py
@@ -0,0 +1,67 @@
+# encoding: utf-8
+
+""" Redirects stdout/stderr to given write methods."""
+
+__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 IPython.kernel.core.output_trap import OutputTrap 
+
+class FileLike(object):
+    """ FileLike object that redirects all write to a callback.
+
+        Only the write-related methods are implemented, as well as those
+        required to read a StringIO.
+    """
+    closed = False
+
+    def __init__(self, write):
+        self.write = write
+
+    def flush(self):
+        pass
+
+    def close(self):
+        pass
+
+    def writelines(self, lines):
+        for line in lines:
+            self.write(line)
+
+    def isatty(self):
+        return False
+
+    def getvalue(self):
+        return ''
+
+
+class SyncOutputTrap(OutputTrap):
+    """ Object which redirect text sent to stdout and stderr to write
+        callbacks.
+    """
+    
+    def __init__(self, write_out, write_err):
+        # Store callbacks
+        self.out = FileLike(write_out)
+        self.err = FileLike(write_err)
+
+        # Boolean to check if the stdout/stderr hook is set.
+        self.out_set = False
+        self.err_set = False
+
+    def clear(self):
+        """ Clear out the buffers.
+        """
+        pass
+