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

Skip to content

Commit 2c85945

Browse files
anntzerandrew-fennell
authored andcommitted
Simplify qt_compat, in particular post-removal of qt4 support.
Also note that `None` is never keyed off `_ETS`, which allows removing the `None: None` entry have some more simplifications. Also avoid the need for exception chaining suppression (`raise ... from None`). Also clarify the error message when no qt binding can be imported.
1 parent f13e98f commit 2c85945

File tree

1 file changed

+12
-21
lines changed

1 file changed

+12
-21
lines changed

lib/matplotlib/backends/qt_compat.py

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
- if any of PyQt6, PySide6, PyQt5, or PySide2 have already been
66
imported (checked in that order), use it;
77
- otherwise, if the QT_API environment variable (used by Enthought) is set, use
8-
it to determine which binding to use (but do not change the backend based on
9-
it; i.e. if the Qt5Agg backend is requested but QT_API is set to "pyqt4",
10-
then actually use Qt5 with PyQt5 or PySide2 (whichever can be imported);
8+
it to determine which binding to use;
119
- otherwise, use whatever the rcParams indicate.
1210
"""
1311

@@ -31,18 +29,12 @@
3129
QT_API_PYSIDE6 = "PySide6"
3230
QT_API_PYQT5 = "PyQt5"
3331
QT_API_PYSIDE2 = "PySide2"
34-
QT_API_PYQTv2 = "PyQt4v2"
35-
QT_API_PYSIDE = "PySide"
36-
QT_API_PYQT = "PyQt4" # Use the old sip v1 API (Py3 defaults to v2).
3732
QT_API_ENV = os.environ.get("QT_API")
3833
if QT_API_ENV is not None:
3934
QT_API_ENV = QT_API_ENV.lower()
40-
# Mapping of QT_API_ENV to requested binding. ETS does not support PyQt4v1.
41-
# (https://github.com/enthought/pyface/blob/master/pyface/qt/__init__.py)
42-
_ETS = {
35+
_ETS = { # Mapping of QT_API_ENV to requested binding.
4336
"pyqt6": QT_API_PYQT6, "pyside6": QT_API_PYSIDE6,
4437
"pyqt5": QT_API_PYQT5, "pyside2": QT_API_PYSIDE2,
45-
None: None
4638
}
4739
# First, check if anything is already imported.
4840
if sys.modules.get("PyQt6.QtCore"):
@@ -68,15 +60,12 @@
6860
# fully manually embedding Matplotlib in a Qt app without using pyplot).
6961
elif QT_API_ENV is None:
7062
QT_API = None
63+
elif QT_API_ENV in _ETS:
64+
QT_API = _ETS[QT_API_ENV]
7165
else:
72-
try:
73-
QT_API = _ETS[QT_API_ENV]
74-
except KeyError:
75-
raise RuntimeError(
76-
"The environment variable QT_API has the unrecognized value "
77-
f"{QT_API_ENV!r}; "
78-
f"valid values are {set(k for k in _ETS if k is not None)}"
79-
) from None
66+
raise RuntimeError(
67+
"The environment variable QT_API has the unrecognized value {!r}; "
68+
"valid values are {}".format(QT_API_ENV, ", ".join(_ETS)))
8069

8170

8271
def _setup_pyqt5plus():
@@ -134,7 +123,9 @@ def _isdeleted(obj):
134123
continue
135124
break
136125
else:
137-
raise ImportError("Failed to import any qt binding")
126+
raise ImportError(
127+
"Failed to import any of the following Qt binding modules: {}"
128+
.format(", ".join(_ETS.values())))
138129
else: # We should not get there.
139130
raise AssertionError(f"Unexpected QT_API: {QT_API}")
140131

@@ -181,7 +172,7 @@ def _devicePixelRatioF(obj):
181172
except AttributeError:
182173
pass
183174
try:
184-
# Not available on Qt4 or some older Qt5.
175+
# Not available on older Qt5.
185176
# self.devicePixelRatio() returns 0 in rare cases
186177
return obj.devicePixelRatio() or 1
187178
except AttributeError:
@@ -195,7 +186,7 @@ def _setDevicePixelRatio(obj, val):
195186
This can be replaced by the direct call when we require Qt>=5.6.
196187
"""
197188
if hasattr(obj, 'setDevicePixelRatio'):
198-
# Not available on Qt4 or some older Qt5.
189+
# Not available on older Qt5.
199190
obj.setDevicePixelRatio(val)
200191

201192

0 commit comments

Comments
 (0)