Repository: ipython/ipython

Issue:
crash - ipython qtconsole, %quickref
I start ipython (both  0.11 and cloned from gitub (90e6e7afd62a598b76472862a4eea7a504cbf9b5) )

on Scientific Linux 6.0 as
ipython qtconsole

If I type %quickref, it crashes on the following line, with 

```
   at_end = len(cursor.selectedText().strip()) == 0
```

AttributeError: 'NoneType' object has no attribute 'strip'

after that, I see an infinite set of messages:
QCoreApplication::exec: The event loop is already running

As requested, I've mailed the crash report to fperez.net



Discussion:
What Qt bindings are you using, and how were they installed?  API specs would suggest that selectedText() can only return a string, which is apparently not the case on your system.

Can you check if [this branch](https://github.com/ipython/ipython/pull/725) fixes your issue?

@epatters, do you think this could be another one of those pyside/pyqt inconsistencies?  I don't know for a fact that @vanandel is using pyside, just wondering...

@fperez: It's possible. If it is, I'm surprised we haven't seen it before.

I'd be wary of trying to hack around this in our code because there is nothing about the selectedText method to suggest that it _alone_ would have this kind of type error. My gut response is that he has a bad build of the PySide/PyQt bindings.

@vanandel: Which bindings are using? Where did your build come from? Can you reproduce this reliably? Do you experience other problems if you use the console for an extended period of time?

I'm using PyQt4-4.6.2-8 from an Scientific Linux RPM.  I tried the branch you suggested and now ipython crashes in frontend_widget.py in highlightBlock()

Attribute error:
 54         if string.startswith(prompt):

As my initial report stated, I tried the released ipython 1.11, and cloned from gitub (90e6e7a).

As far as I can tell, this crash happens every time I type '%quickref' in the qtconsole.   I haven't tried the console for anything else.

I have not installed pyside -should I try to install it, and if so, should I remove the PyQt4 bindings?

From what you have described, it sounds like you have a broken build of PyQt.

I would suggest that you either build PyQt (but not Qt!) yourself or obtain PySide and hope that you have better luck. If you go the PySide route, you do not have to remove the PyQt bindings, but you can set the environment variable 'QT_API=pyside' to ensure that PySide is used.

I should add that your version of PyQt is quite old. Perhaps it is simply buggy.

I think the most immediate cause of the issue is clear (and more widespread than just selectedText): Your Qt bindings can return None on empty strings, despite the API requiring that these methods always return strings.  

The PyQt docs explicitly state that, while some Qt methods that would return empty QStrings often return NULL, PyQt forces them to be empty strings.

Could this be something that has changed since 4.6?  I don't think there are many (if any) other users who have been testing against PyQt that old (4.7 is from January 2010).  

I think we either have to change _all_ our string checks to allow None, or we have to depend on a PyQt4 that's relatively recent.

There is _a lot_ of code that assumes that strings are, well, strings. My vote is to have a PyQt version cutoff. 

Between PySide and PyQt, which binding has been tested more with ipython?

(Thanks to all of you for your quick responses on my bug report - I appreciate it!)

A new-ish version of PyQt is likely to be more stable. PySide is still in its infancy at this point.

I tried building the PyQt 4.8.5 bindings, and configure.py complains:
pylupdate4 and pyrcc4 will not be built because the Qt XML module is missing.

In spite of the fact that /usr/lib64/libQtXml.so exists.  

When I build and install PyQt, I'm missing QtGui, so ipython qtconsole won't run.  Once you document the minimum version of PyQt needed by ipython, I guess you can close this.


Resolving patch:
diff --git a/IPython/external/qt.py b/IPython/external/qt.py
--- a/IPython/external/qt.py
+++ b/IPython/external/qt.py
@@ -25,14 +25,21 @@ def prepare_pyqt4():
 if QT_API is None:
     try:
         import PySide
+        if PySide.__version__ < '1.0.3':
+            # old PySide, fallback on PyQt
+            raise ImportError
         QT_API = QT_API_PYSIDE
     except ImportError:
         try:
             prepare_pyqt4()
             import PyQt4
+            from PyQt4 import QtCore
+            if QtCore.PYQT_VERSION_STR < '4.7':
+                # PyQt 4.6 has issues with null strings returning as None
+                raise ImportError
             QT_API = QT_API_PYQT
         except ImportError:
-            raise ImportError('Cannot import PySide or PyQt4')
+            raise ImportError('Cannot import PySide >= 1.0.3 or PyQt4 >= 4.7')
         
 elif QT_API == QT_API_PYQT:
     # Note: This must be called *before* PyQt4 is imported.
@@ -41,12 +48,17 @@ def prepare_pyqt4():
 # Now peform the imports.
 if QT_API == QT_API_PYQT:
     from PyQt4 import QtCore, QtGui, QtSvg
+    if QtCore.PYQT_VERSION_STR < '4.7':
+        raise ImportError("IPython requires PyQt4 >= 4.7, found %s"%QtCore.PYQT_VERSION_STR)
 
     # Alias PyQt-specific functions for PySide compatibility.
     QtCore.Signal = QtCore.pyqtSignal
     QtCore.Slot = QtCore.pyqtSlot
 
 elif QT_API == QT_API_PYSIDE:
+    import PySide
+    if PySide.__version__ < '1.0.3':
+        raise ImportError("IPython requires PySide >= 1.0.3, found %s"%PySide.__version__)
     from PySide import QtCore, QtGui, QtSvg
 
 else:


Test patch: