1616# Available APIs.
1717QT_API_PYQT = 'pyqt'
1818QT_API_PYQTv1 = 'pyqtv1'
19+ QT_API_PYQT_DEFAULT = 'pyqtdefault' # don't set SIP explicitly
1920QT_API_PYSIDE = 'pyside'
2021
2122
2223class ImportDenier (object ):
2324 """Import Hook that will guard against bad Qt imports
2425 once IPython commits to a specific binding
2526 """
26- __forbidden = set ()
2727
2828 def __init__ (self ):
2929 self .__forbidden = None
@@ -84,7 +84,7 @@ def has_binding(api):
8484
8585 Parameters
8686 ----------
87- api : str [ 'pyqtv1' | 'pyqt' | 'pyside']
87+ api : str [ 'pyqtv1' | 'pyqt' | 'pyside' | 'pyqtdefault' ]
8888 Which module to check for
8989
9090 Returns
@@ -96,7 +96,8 @@ def has_binding(api):
9696 # check for complete presence before importing
9797 module_name = {QT_API_PYSIDE : 'PySide' ,
9898 QT_API_PYQT : 'PyQt4' ,
99- QT_API_PYQTv1 : 'PyQt4' }
99+ QT_API_PYQTv1 : 'PyQt4' ,
100+ QT_API_PYQT_DEFAULT : 'PyQt4' }
100101 module_name = module_name [api ]
101102
102103 import imp
@@ -136,22 +137,36 @@ def qtapi_version():
136137
137138def can_import (api ):
138139 """Safely query whether an API is importable, without importing it"""
140+ if not has_binding (api ):
141+ return False
142+
139143 current = loaded_api ()
140- return has_binding (api ) and current in [api , None ]
144+ if api == QT_API_PYQT_DEFAULT :
145+ return current in [QT_API_PYQT , QT_API_PYQTv1 , None ]
146+ else :
147+ return current in [api , None ]
141148
142149
143150def import_pyqt4 (version = 2 ):
144151 """
145152 Import PyQt4
146153
154+ Parameters
155+ ----------
156+ version : 1, 2, or None
157+ Which QString/QVariant API to use. Set to None to use the system
158+ default
159+
147160 ImportErrors rasied within this function are non-recoverable
148161 """
149162 # The new-style string API (version=2) automatically
150163 # converts QStrings to Unicode Python strings. Also, automatically unpacks
151164 # QVariants to their underlying objects.
152165 import sip
153- sip .setapi ('QString' , version )
154- sip .setapi ('QVariant' , version )
166+
167+ if version is not None :
168+ sip .setapi ('QString' , version )
169+ sip .setapi ('QVariant' , version )
155170
156171 from PyQt4 import QtGui , QtCore , QtSvg
157172
@@ -163,6 +178,8 @@ def import_pyqt4(version=2):
163178 QtCore .Signal = QtCore .pyqtSignal
164179 QtCore .Slot = QtCore .pyqtSlot
165180
181+ # query for the API version (in case version == None)
182+ version = sip .getapi ('QString' )
166183 api = QT_API_PYQTv1 if version == 1 else QT_API_PYQT
167184 return QtCore , QtGui , QtSvg , api
168185
@@ -205,21 +222,24 @@ def load_qt(api_options):
205222 """
206223 loaders = {QT_API_PYSIDE : import_pyside ,
207224 QT_API_PYQT : import_pyqt4 ,
208- QT_API_PYQTv1 : partial (import_pyqt4 , version = 1 )
225+ QT_API_PYQTv1 : partial (import_pyqt4 , version = 1 ),
226+ QT_API_PYQT_DEFAULT : partial (import_pyqt4 , version = None )
209227 }
210228
211229 for api in api_options :
212230
213231 if api not in loaders :
214232 raise RuntimeError (
215- "Invalid Qt API %r, valid values are: %r, %r, %r" %
216- (api , QT_API_PYSIDE , QT_API_PYQT , QT_API_PYQTv1 ))
233+ "Invalid Qt API %r, valid values are: %r, %r, %r, %r" %
234+ (api , QT_API_PYSIDE , QT_API_PYQT ,
235+ QT_API_PYQTv1 , QT_API_PYQT_DEFAULT ))
217236
218237 if not can_import (api ):
219238 continue
220239
221240 #cannot safely recover from an ImportError during this
222241 result = loaders [api ]()
242+ api = result [- 1 ] # changed if api = QT_API_PYQT_DEFAULT
223243 commit_api (api )
224244 return result
225245 else :
0 commit comments