Repository: ipython/ipython

Issue:
Qt Console output spacing
In IPython (=0.13) Qt Console, there seems to be a missing newline before the Out parameter in the first plot command after running `pylab`.  See, for example `In [2]` and `Out[2]`:

```
Python 2.7.3 (default, Apr 20 2012, 22:39:59) 
Type "copyright", "credits" or "license" for more information.

IPython 0.13 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.
%guiref   -> A brief reference about the graphical user interface.

In [1]: pylab

Welcome to pylab, a matplotlib-based Python environment [backend: TkAgg].
For more information, type 'help(pylab)'.

In [2]: plot([1,2,3])Out[2]: [<matplotlib.lines.Line2D at 0x3e40210>]

In [3]: plot([1,2,3])
Out[3]: [<matplotlib.lines.Line2D at 0x3e34d10>]

In [4]: 
```



Discussion:
`git bisect` points the origin of the issue to 1422d277055ed26b7a367e6ce2c1649d32548ee9, but I haven't investigated further.

```
1422d277055ed26b7a367e6ce2c1649d32548ee9 is the first bad commit
commit 1422d277055ed26b7a367e6ce2c1649d32548ee9
Author: Matthias BUSSONNIER <bussonniermatthias@gmail.com>
Date:   Fri May 25 17:15:55 2012 +0200

    new completer for qtconsole.

    add a completer to the qtconsole that is navigable by arraow keys and
    tab. One need to call it twice to get it on focus and be able to select
    completion with Return. looks like zsh completer, not the gui drop down
    list of --gui-completer.

    This also try to split the completion logic from console_widget, and try
    to keep the old completer qui around. The plain completer that never
    takes focus back, and the QlistWidget completer.

    to switch between the 3, the --gui-completion flag as been changed to
    take an argument (plain, droplist, ncurses).

```

Confirming, though I see slightly different behavior: I simply don't see the first `Out` after plotting for the first time.  This is what it looks like to me:

```
In [1]: pylab

Welcome to pylab, a matplotlib-based Python environment [backend: Qt4Agg].
For more information, type 'help(pylab)'.

In [2]: plot([1,2,3,4])

In [3]: plot([1,2,3,4])
Out[3]: [<matplotlib.lines.Line2D at 0x30a6490>]
```

In any case, bug indeed.  Bummer.

hum.. I know a commit of mine introduced the bug, but i'll have difficulty tracking it down as I can't reproduce it...
Does invoking the completer once (`plo<tab>`..) change the behavior ? 

No @Carreau, I get the same thing even if I invoke the completer.  Do you not see any problem at all?

No, I don't get it at all. On neither of my 2 computers. 
I don't see any reason why it should do that, except a race condition in PyQt/PySide event handeling that erase the temporary buffer after a newline is printed, but even thought, the buffer is zero len, so it should not do this, and the clearing of the buffer is not made by bindings event...

I'm thinking on what can be done. 
I would try do make  `_clear_temporary_buffer` no-op in `IPython/frontend/qt/console/console_widget.py`, (be carefull not to use the completer) and try again. Also look at wether or not the old completer (`--gui-completion=plain`) does the same.

i'll see if I can get this behavior on a VM...


Resolving patch:
diff --git a/IPython/frontend/qt/console/console_widget.py b/IPython/frontend/qt/console/console_widget.py
--- a/IPython/frontend/qt/console/console_widget.py
+++ b/IPython/frontend/qt/console/console_widget.py
@@ -884,15 +884,16 @@ def _clear_temporary_buffer(self):
             the prompt region.
         """
         # Select and remove all text below the input buffer.
-        _temp_buffer_filled = False
         cursor = self._get_prompt_cursor()
         prompt = self._continuation_prompt.lstrip()
-        while cursor.movePosition(QtGui.QTextCursor.NextBlock):
-            temp_cursor = QtGui.QTextCursor(cursor)
-            temp_cursor.select(QtGui.QTextCursor.BlockUnderCursor)
-            text = temp_cursor.selection().toPlainText().lstrip()
-            if not text.startswith(prompt):
-                break
+        if(self._temp_buffer_filled):
+            self._temp_buffer_filled = False
+            while cursor.movePosition(QtGui.QTextCursor.NextBlock):
+                temp_cursor = QtGui.QTextCursor(cursor)
+                temp_cursor.select(QtGui.QTextCursor.BlockUnderCursor)
+                text = temp_cursor.selection().toPlainText().lstrip()
+                if not text.startswith(prompt):
+                    break
         else:
             # We've reached the end of the input buffer and no text follows.
             return
@@ -945,7 +946,7 @@ def _fill_temporary_buffer(self, cursor, text, html=False):
         self._control.moveCursor(QtGui.QTextCursor.End)
         self._control.setTextCursor(cursor)
 
-        _temp_buffer_filled = True
+        self._temp_buffer_filled = True
 
 
     def _context_menu_make(self, pos):


Test patch: