##// END OF EJS Templates
New implementation of has_binding Qt check for Python 3.4+...
Thomas Kluyver -
Show More
@@ -21,6 +21,15 b" QT_API_PYQT5 = 'pyqt5'"
21 QT_API_PYQTv1 = 'pyqtv1' # Force version 2
21 QT_API_PYQTv1 = 'pyqtv1' # Force version 2
22 QT_API_PYQT_DEFAULT = 'pyqtdefault' # use system default for version 1 vs. 2
22 QT_API_PYQT_DEFAULT = 'pyqtdefault' # use system default for version 1 vs. 2
23 QT_API_PYSIDE = 'pyside'
23 QT_API_PYSIDE = 'pyside'
24 QT_API_PYSIDE2 = 'pyside2'
25
26 api_to_module = {QT_API_PYSIDE2: 'PySide2',
27 QT_API_PYSIDE: 'PySide',
28 QT_API_PYQT: 'PyQt4',
29 QT_API_PYQTv1: 'PyQt4',
30 QT_API_PYQT5: 'PyQt5',
31 QT_API_PYQT_DEFAULT: 'PyQt4',
32 }
24
33
25
34
26 class ImportDenier(object):
35 class ImportDenier(object):
@@ -89,8 +98,9 b' def loaded_api():'
89
98
90
99
91 def has_binding(api):
100 def has_binding(api):
92 """Safely check for PyQt4/5 or PySide, without importing
101 """Safely check for PyQt4/5 or PySide, without importing submodules
93 submodules
102
103 Supports Python <= 3.3
94
104
95 Parameters
105 Parameters
96 ----------
106 ----------
@@ -104,12 +114,7 b' def has_binding(api):'
104 # we can't import an incomplete pyside and pyqt4
114 # we can't import an incomplete pyside and pyqt4
105 # this will cause a crash in sip (#1431)
115 # this will cause a crash in sip (#1431)
106 # check for complete presence before importing
116 # check for complete presence before importing
107 module_name = {QT_API_PYSIDE: 'PySide',
117 module_name = api_to_module[api]
108 QT_API_PYQT: 'PyQt4',
109 QT_API_PYQTv1: 'PyQt4',
110 QT_API_PYQT5: 'PyQt5',
111 QT_API_PYQT_DEFAULT: 'PyQt4'}
112 module_name = module_name[api]
113
118
114 import imp
119 import imp
115 try:
120 try:
@@ -131,6 +136,48 b' def has_binding(api):'
131 except ImportError:
136 except ImportError:
132 return False
137 return False
133
138
139 def has_binding_new(api):
140 """Safely check for PyQt4/5 or PySide, without importing submodules
141
142 Supports Python >= 3.4
143
144 Parameters
145 ----------
146 api : str [ 'pyqtv1' | 'pyqt' | 'pyqt5' | 'pyside' | 'pyqtdefault']
147 Which module to check for
148
149 Returns
150 -------
151 True if the relevant module appears to be importable
152 """
153 module_name = api_to_module[api]
154 from importlib.util import find_spec
155
156 required = ['QtCore', 'QtGui', 'QtSvg']
157 if api in (QT_API_PYQT5, QT_API_PYSIDE2):
158 # QT5 requires QtWidgets too
159 required.append('QtWidgets')
160
161 for submod in required:
162 try:
163 spec = find_spec('%s.%s' % (module_name, submod))
164 except ImportError:
165 # Package (e.g. PyQt5) not found
166 return False
167 else:
168 if spec is None:
169 # Submodule (e.g. PyQt5.QtCore) not found
170 return False
171
172 if api == QT_API_PYSIDE:
173 # We can also safely check PySide version
174 import PySide
175 return check_version(PySide.__version__, '1.0.3')
176
177 return True
178
179 if sys.version_info >= (3, 4):
180 has_binding = has_binding_new
134
181
135 def qtapi_version():
182 def qtapi_version():
136 """Return which QString API has been set, if any
183 """Return which QString API has been set, if any
General Comments 0
You need to be logged in to leave comments. Login now