/usr/lib/python3/dist-packages/pytestqt/qt_compat.py is in python3-pytestqt 2.3.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | """
Provide a common way to import Qt classes used by pytest-qt in a unique manner,
abstracting API differences between PyQt4, PyQt5, PySide and PySide2.
.. note:: This module is not part of pytest-qt public API, hence its interface
may change between releases and users should not rely on it.
Based on from https://github.com/epage/PythonUtils.
"""
from __future__ import with_statement, division
from collections import namedtuple
import os
VersionTuple = namedtuple('VersionTuple', 'qt_api, qt_api_version, runtime, compiled')
class _QtApi:
"""
Interface to the underlying Qt API currently configured for pytest-qt.
This object lazily loads all class references and other objects when the ``set_qt_api`` method
gets called, providing a uniform way to access the Qt classes.
"""
def _get_qt_api_from_env(self):
api = os.environ.get('PYTEST_QT_API')
if api is not None:
api = api.lower()
if api not in ('pyside', 'pyside2', 'pyqt4', 'pyqt4v2', 'pyqt5'): # pragma: no cover
msg = 'Invalid value for $PYTEST_QT_API: %s'
raise RuntimeError(msg % api)
return api
def _guess_qt_api(self): # pragma: no cover
def _can_import(name):
try:
__import__(name)
return True
except ImportError:
return False
# Note, not importing only the root namespace because when uninstalling from conda,
# the namespace can still be there.
if _can_import('PySide2.QtCore'):
return 'pyside2'
elif _can_import('PyQt5.QtCore'):
return 'pyqt5'
elif _can_import('PySide.QtCore'):
return 'pyside'
elif _can_import('PyQt4.QtCore'):
return 'pyqt4'
return None
def set_qt_api(self, api):
self.pytest_qt_api = self._get_qt_api_from_env() or api or self._guess_qt_api()
if not self.pytest_qt_api: # pragma: no cover
msg = 'pytest-qt requires either PySide, PySide2, PyQt4 or PyQt5 to be installed'
raise RuntimeError(msg)
_root_modules = {
'pyside': 'PySide',
'pyside2': 'PySide2',
'pyqt4': 'PyQt4',
'pyqt4v2': 'PyQt4',
'pyqt5': 'PyQt5',
}
_root_module = _root_modules[self.pytest_qt_api]
def _import_module(module_name):
m = __import__(_root_module, globals(), locals(), [module_name], 0)
return getattr(m, module_name)
if self.pytest_qt_api == 'pyqt4v2': # pragma: no cover
# the v2 api in PyQt4
# http://pyqt.sourceforge.net/Docs/PyQt4/incompatible_apis.html
import sip
sip.setapi("QDate", 2)
sip.setapi("QDateTime", 2)
sip.setapi("QString", 2)
sip.setapi("QTextStream", 2)
sip.setapi("QTime", 2)
sip.setapi("QUrl", 2)
sip.setapi("QVariant", 2)
self.QtCore = QtCore = _import_module('QtCore')
self.QtGui = QtGui = _import_module('QtGui')
self.QtTest = _import_module('QtTest')
self.Qt = QtCore.Qt
self.QEvent = QtCore.QEvent
self.qDebug = QtCore.qDebug
self.qWarning = QtCore.qWarning
self.qCritical = QtCore.qCritical
self.qFatal = QtCore.qFatal
self.QtDebugMsg = QtCore.QtDebugMsg
self.QtWarningMsg = QtCore.QtWarningMsg
self.QtCriticalMsg = QtCore.QtCriticalMsg
self.QtFatalMsg = QtCore.QtFatalMsg
# Qt4 and Qt5 have different functions to install a message handler;
# the plugin will try to use the one that is not None
self.qInstallMsgHandler = None
self.qInstallMessageHandler = None
if self.pytest_qt_api.startswith('pyside'):
self.Signal = QtCore.Signal
self.Slot = QtCore.Slot
self.Property = QtCore.Property
self.QStringListModel = QtGui.QStringListModel
self.QStandardItem = QtGui.QStandardItem
self.QStandardItemModel = QtGui.QStandardItemModel
self.QAbstractListModel = QtCore.QAbstractListModel
self.QAbstractTableModel = QtCore.QAbstractTableModel
self.QStringListModel = QtGui.QStringListModel
if self.pytest_qt_api == 'pyside2':
_QtWidgets = _import_module('QtWidgets')
self.QApplication = _QtWidgets.QApplication
self.QWidget = _QtWidgets.QWidget
self.QLineEdit = _QtWidgets.QLineEdit
self.qInstallMessageHandler = QtCore.qInstallMessageHandler
self.QSortFilterProxyModel = QtCore.QSortFilterProxyModel
else:
self.QApplication = QtGui.QApplication
self.QWidget = QtGui.QWidget
self.QLineEdit = QtGui.QLineEdit
self.qInstallMsgHandler = QtCore.qInstallMsgHandler
self.QSortFilterProxyModel = QtGui.QSortFilterProxyModel
def extract_from_variant(variant):
"""PySide does not expose QVariant API"""
return variant
def make_variant(value=None):
"""PySide does not expose QVariant API"""
return value
self.extract_from_variant = extract_from_variant
self.make_variant = make_variant
elif self.pytest_qt_api in ('pyqt4', 'pyqt4v2', 'pyqt5'):
self.Signal = QtCore.pyqtSignal
self.Slot = QtCore.pyqtSlot
self.Property = QtCore.pyqtProperty
if self.pytest_qt_api == 'pyqt5':
_QtWidgets = _import_module('QtWidgets')
self.QApplication = _QtWidgets.QApplication
self.QWidget = _QtWidgets.QWidget
self.qInstallMessageHandler = QtCore.qInstallMessageHandler
self.QStringListModel = QtCore.QStringListModel
self.QSortFilterProxyModel = QtCore.QSortFilterProxyModel
def extract_from_variant(variant):
"""not needed in PyQt5: Qt API always returns pure python objects"""
return variant
def make_variant(value=None):
"""Return a QVariant object from the given Python builtin"""
# PyQt4 doesn't allow one to instantiate any QVariant at all:
# QVariant represents a mapped type and cannot be instantiated
return QtCore.QVariant(value)
else:
self.QApplication = QtGui.QApplication
self.QWidget = QtGui.QWidget
self.qInstallMsgHandler = QtCore.qInstallMsgHandler
self.QStringListModel = QtGui.QStringListModel
self.QSortFilterProxyModel = QtGui.QSortFilterProxyModel
def extract_from_variant(variant):
"""returns python object from the given QVariant"""
if isinstance(variant, QtCore.QVariant):
return variant.toPyObject()
return variant
def make_variant(value=None):
"""Return a QVariant object from the given Python builtin"""
# PyQt4 doesn't allow one to instantiate any QVariant at all:
# QVariant represents a mapped type and cannot be instantiated
return value
self.QStandardItem = QtGui.QStandardItem
self.QStandardItemModel = QtGui.QStandardItemModel
self.QAbstractListModel = QtCore.QAbstractListModel
self.QAbstractTableModel = QtCore.QAbstractTableModel
self.extract_from_variant = extract_from_variant
self.make_variant = make_variant
def get_versions(self):
if self.pytest_qt_api in ('pyside', 'pyside2'):
qt_api_name = 'PySide2' if self.pytest_qt_api == 'pyside2' else 'PySide'
if self.pytest_qt_api == 'pyside2':
import PySide2
version = PySide2.__version__
else:
import PySide
version = PySide.__version__
return VersionTuple(qt_api_name, version, self.QtCore.qVersion(),
self.QtCore.__version__)
else:
qt_api_name = 'PyQt5' if self.pytest_qt_api == 'pyqt5' else 'PyQt4'
return VersionTuple(qt_api_name, self.QtCore.PYQT_VERSION_STR,
self.QtCore.qVersion(), self.QtCore.QT_VERSION_STR)
qt_api = _QtApi()
|