Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 41801a1

Browse files
author
Chris Beaumont
committed
added QT_API_DEFAULT Qt option. Fixed reversed MPL qt logic test.
1 parent 7ec1888 commit 41801a1

2 files changed

Lines changed: 34 additions & 14 deletions

File tree

IPython/external/qt_for_kernel.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
from IPython.utils.warn import warn
3838
from IPython.utils.version import check_version
3939
from IPython.external.qt_loaders import (load_qt, QT_API_PYSIDE,
40-
QT_API_PYQT, QT_API_PYQTv1,
40+
QT_API_PYQT, QT_API_PYQT_DEFAULT,
4141
loaded_api)
4242

4343
#Constraints placed on an imported matplotlib
@@ -50,7 +50,7 @@ def matplotlib_options(mpl):
5050
if mpqt.lower() == 'pyside':
5151
return [QT_API_PYSIDE]
5252
elif mpqt.lower() == 'pyqt4':
53-
return [QT_API_PYQTv1]
53+
return [QT_API_PYQT_DEFAULT]
5454
raise ImportError("unhandled value for backend.qt4 from matplotlib: %r" %
5555
mpqt)
5656

@@ -65,13 +65,13 @@ def get_options():
6565

6666
mpl = sys.modules.get('matplotlib', None)
6767

68-
if mpl is not None and check_version(mpl.__version__, '1.0.2'):
68+
if mpl is not None and not check_version(mpl.__version__, '1.0.2'):
6969
#1.0.1 only supports PyQt4 v1
70-
return [QT_API_PYQTv1]
70+
return [QT_API_PYQT_DEFAULT]
7171

7272
if os.environ.get('QT_API', None) is None:
7373
#no ETS variable. Ask mpl, then use either
74-
return matplotlib_options(mpl) or [QT_API_PYQTv1, QT_API_PYSIDE]
74+
return matplotlib_options(mpl) or [QT_API_PYQT_DEFAULT, QT_API_PYSIDE]
7575

7676
#ETS variable present. Will fallback to external.qt
7777
return None

IPython/external/qt_loaders.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
# Available APIs.
1717
QT_API_PYQT = 'pyqt'
1818
QT_API_PYQTv1 = 'pyqtv1'
19+
QT_API_PYQT_DEFAULT = 'pyqtdefault' # don't set SIP explicitly
1920
QT_API_PYSIDE = 'pyside'
2021

2122

2223
class 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

137138
def 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

143150
def 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

Comments
 (0)