Repository: spyder-ide/spyder

Issue:
Exceptions swallow the prompt!
*Sometimes* when an exception occurs the prompt doesn't come back until you specifically press the `ESC` button. 

**What steps will reproduce the problem?**

1. Try to execute some code which throws an excpetion.
2. *somethimes* you might not get the prompt back :disappointed: 


**What is the expected output? What do you see instead?**
I expect to immediately get the next input prompt but no prompt is shown until you press `ESC`.


**Please provide any additional information below**
In the GIF below the "hanging" behaviour is seen the last time I execute the incorrect code when the prompt isn't returned until I press `ESC`

![exception](https://cloud.githubusercontent.com/assets/881019/26334785/e3d514d2-3faa-11e7-9752-91c220f43bb9.gif)


## Versions and main components
This is on `master`
![image](https://cloud.githubusercontent.com/assets/881019/26334828/250fddec-3fab-11e7-9546-48fc468880a1.png)

## Dependencies
```
cython >=0.21    :  0.25.2 (OK)
jedi >=0.9.0     :  0.10.2 (OK)
matplotlib >=1.0 :  2.0.2 (OK)
nbconvert >=4.0  :  5.1.1 (OK)
numpy >=1.7      :  1.12.1 (OK)
pandas >=0.13.1  :  0.20.1 (OK)
pycodestyle >=2.3:  None (NOK)
pyflakes >=0.6.0 :  1.5.0 (OK)
pygments >=2.0   :  2.2.0 (OK)
pylint >=0.25    :  1.6.5 (OK)
qtconsole >=4.2.0:  4.3.0 (OK)
rope >=0.9.4     :  0.10.5 (OK)
sphinx >=0.6.6   :  1.6.1 (OK)
sympy >=0.7.3    :  1.0 (OK)
```


Discussion:
I see the same thing but I would not say that I don't get the prompt back. It is just that sometimes exceptions are written in the next input. In dhirschfeld's gif, the exception thrown in `In[32]` is displayed as text after `In[33]`. It should therefore be removed by either `esc` or removing the characters one by one. If I had to guess I would say that the prompt is coming back before the exception  is displayed.  
I tried to implement `_handle_error` in `NamepaceBrowserWidget` and it sometimes gets called after `_handle_execute_reply`, which causes this issue.
@impact27, thanks for looking into this. I don't exactly understand what we should do about it.
My guess is that this is a bug with ipykernel. I will try a workaround.

Resolving patch:
diff --git a/spyder/widgets/ipythonconsole/shell.py b/spyder/widgets/ipythonconsole/shell.py
--- a/spyder/widgets/ipythonconsole/shell.py
+++ b/spyder/widgets/ipythonconsole/shell.py
@@ -14,6 +14,7 @@
 
 from qtpy.QtCore import Signal
 from qtpy.QtWidgets import QMessageBox
+from qtpy import PYQT4
 from spyder.config.base import _
 from spyder.config.gui import config_shortcut
 from spyder.py3compat import PY2, to_text_string
@@ -403,6 +404,17 @@ def change_mpl_backend(self, command):
                 self.silent_execute(command)
 
     #---- Private methods (overrode by us) ---------------------------------
+    def _handle_error(self, msg):
+        """
+        Reimplemented to reset the prompt if the error comes after the reply
+        """
+        # In pyqt4, if super does not has _handle_error, disregard the error
+        if PYQT4:
+            if not hasattr(super(ShellWidget, self), '_handle_error'):
+                return
+        super(ShellWidget, self)._handle_error(msg)
+        self._show_interpreter_prompt()
+
     def _context_menu_make(self, pos):
         """Reimplement the IPython context menu"""
         menu = super(ShellWidget, self)._context_menu_make(pos)


Test patch: