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

Skip to content

Commit 5229d4a

Browse files
authored
Merge pull request #12163 from QuLogic/defer-qt-loading
TST: Defer loading Qt framework until test is run.
2 parents 19420b9 + c741f44 commit 5229d4a

File tree

2 files changed

+140
-56
lines changed

2 files changed

+140
-56
lines changed

lib/matplotlib/tests/test_backend_qt4.py

Lines changed: 73 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,73 @@
77

88
import pytest
99

10-
try:
11-
import PyQt4
12-
except (ImportError, RuntimeError): # RuntimeError if PyQt5 already imported.
10+
11+
@pytest.fixture(autouse=True)
12+
def mpl_test_settings(qt4_module, mpl_test_settings):
13+
"""
14+
Ensure qt4_module fixture is *first* fixture.
15+
16+
We override the `mpl_test_settings` fixture and depend on the `qt4_module`
17+
fixture first. It is very important that it is first, because it skips
18+
tests when Qt4 is not available, and if not, then the main
19+
`mpl_test_settings` fixture will try to switch backends before the skip can
20+
be triggered.
21+
"""
22+
pass
23+
24+
25+
@pytest.fixture
26+
def qt4_module():
1327
try:
14-
import PySide
15-
except ImportError:
16-
pytestmark = pytest.mark.skip("Failed to import a Qt4 binding.")
28+
import PyQt4
29+
# RuntimeError if PyQt5 already imported.
30+
except (ImportError, RuntimeError):
31+
try:
32+
import PySide
33+
except ImportError:
34+
pytest.skip("Failed to import a Qt4 binding.")
35+
36+
qt_compat = pytest.importorskip('matplotlib.backends.qt_compat')
37+
QtCore = qt_compat.QtCore
38+
39+
try:
40+
py_qt_ver = int(QtCore.PYQT_VERSION_STR.split('.')[0])
41+
except AttributeError:
42+
py_qt_ver = QtCore.__version_info__[0]
43+
44+
if py_qt_ver != 4:
45+
pytest.skip(reason='Qt4 is not available')
46+
47+
from matplotlib.backends.backend_qt4 import (
48+
MODIFIER_KEYS, SUPER, ALT, CTRL, SHIFT) # noqa
49+
50+
mods = {}
51+
keys = {}
52+
for name, index in zip(['Alt', 'Control', 'Shift', 'Super'],
53+
[ALT, CTRL, SHIFT, SUPER]):
54+
_, mod, key = MODIFIER_KEYS[index]
55+
mods[name + 'Modifier'] = mod
56+
keys[name + 'Key'] = key
1757

18-
qt_compat = pytest.importorskip('matplotlib.backends.qt_compat')
19-
QtCore = qt_compat.QtCore
58+
return QtCore, mods, keys
2059

21-
from matplotlib.backends.backend_qt4 import (
22-
MODIFIER_KEYS, SUPER, ALT, CTRL, SHIFT) # noqa
2360

24-
_, ControlModifier, ControlKey = MODIFIER_KEYS[CTRL]
25-
_, AltModifier, AltKey = MODIFIER_KEYS[ALT]
26-
_, SuperModifier, SuperKey = MODIFIER_KEYS[SUPER]
27-
_, ShiftModifier, ShiftKey = MODIFIER_KEYS[SHIFT]
61+
@pytest.fixture
62+
def qt_key(request):
63+
QtCore, _, keys = request.getfixturevalue('qt4_module')
64+
if request.param.startswith('Key'):
65+
return getattr(QtCore.Qt, request.param)
66+
else:
67+
return keys[request.param]
2868

29-
try:
30-
py_qt_ver = int(QtCore.PYQT_VERSION_STR.split('.')[0])
31-
except AttributeError:
32-
py_qt_ver = QtCore.__version_info__[0]
3369

34-
if py_qt_ver != 4:
35-
pytestmark = pytest.mark.skipif(reason='Qt4 is not available')
70+
@pytest.fixture
71+
def qt_mods(request):
72+
QtCore, mods, _ = request.getfixturevalue('qt4_module')
73+
result = QtCore.Qt.NoModifier
74+
for mod in request.param:
75+
result |= mods[mod]
76+
return result
3677

3778

3879
@pytest.mark.backend('Qt4Agg')
@@ -55,21 +96,22 @@ def test_fig_close():
5596
@pytest.mark.parametrize(
5697
'qt_key, qt_mods, answer',
5798
[
58-
(QtCore.Qt.Key_A, ShiftModifier, 'A'),
59-
(QtCore.Qt.Key_A, QtCore.Qt.NoModifier, 'a'),
60-
(QtCore.Qt.Key_A, ControlModifier, 'ctrl+a'),
61-
(QtCore.Qt.Key_Aacute, ShiftModifier,
99+
('Key_A', ['ShiftModifier'], 'A'),
100+
('Key_A', [], 'a'),
101+
('Key_A', ['ControlModifier'], 'ctrl+a'),
102+
('Key_Aacute', ['ShiftModifier'],
62103
'\N{LATIN CAPITAL LETTER A WITH ACUTE}'),
63-
(QtCore.Qt.Key_Aacute, QtCore.Qt.NoModifier,
104+
('Key_Aacute', [],
64105
'\N{LATIN SMALL LETTER A WITH ACUTE}'),
65-
(ControlKey, AltModifier, 'alt+control'),
66-
(AltKey, ControlModifier, 'ctrl+alt'),
67-
(QtCore.Qt.Key_Aacute, (ControlModifier | AltModifier | SuperModifier),
106+
('ControlKey', ['AltModifier'], 'alt+control'),
107+
('AltKey', ['ControlModifier'], 'ctrl+alt'),
108+
('Key_Aacute', ['ControlModifier', 'AltModifier', 'SuperModifier'],
68109
'ctrl+alt+super+\N{LATIN SMALL LETTER A WITH ACUTE}'),
69-
(QtCore.Qt.Key_Backspace, QtCore.Qt.NoModifier, 'backspace'),
70-
(QtCore.Qt.Key_Backspace, ControlModifier, 'ctrl+backspace'),
71-
(QtCore.Qt.Key_Play, QtCore.Qt.NoModifier, None),
110+
('Key_Backspace', [], 'backspace'),
111+
('Key_Backspace', ['ControlModifier'], 'ctrl+backspace'),
112+
('Key_Play', [], None),
72113
],
114+
indirect=['qt_key', 'qt_mods'],
73115
ids=[
74116
'shift',
75117
'lower',

lib/matplotlib/tests/test_backend_qt5.py

Lines changed: 67 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,65 @@
77

88
import pytest
99

10-
try:
11-
import PyQt5
12-
except (ImportError, RuntimeError): # RuntimeError if PyQt4 already imported.
10+
11+
@pytest.fixture(autouse=True)
12+
def mpl_test_settings(qt5_module, mpl_test_settings):
13+
"""
14+
Ensure qt5_module fixture is *first* fixture.
15+
16+
We override the `mpl_test_settings` fixture and depend on the `qt5_module`
17+
fixture first. It is very important that it is first, because it skips
18+
tests when Qt5 is not available, and if not, then the main
19+
`mpl_test_settings` fixture will try to switch backends before the skip can
20+
be triggered.
21+
"""
22+
pass
23+
24+
25+
@pytest.fixture
26+
def qt5_module():
1327
try:
14-
import PySide2
15-
except ImportError:
16-
pytestmark = pytest.mark.skip("Failed to import a Qt5 binding.")
28+
import PyQt5
29+
# RuntimeError if PyQt4 already imported.
30+
except (ImportError, RuntimeError):
31+
try:
32+
import PySide2
33+
except ImportError:
34+
pytest.skip("Failed to import a Qt5 binding.")
35+
36+
qt_compat = pytest.importorskip('matplotlib.backends.qt_compat')
37+
QtCore = qt_compat.QtCore
38+
39+
from matplotlib.backends.backend_qt5 import (
40+
MODIFIER_KEYS, SUPER, ALT, CTRL, SHIFT) # noqa
41+
42+
mods = {}
43+
keys = {}
44+
for name, index in zip(['Alt', 'Control', 'Shift', 'Super'],
45+
[ALT, CTRL, SHIFT, SUPER]):
46+
_, mod, key = MODIFIER_KEYS[index]
47+
mods[name + 'Modifier'] = mod
48+
keys[name + 'Key'] = key
49+
50+
return QtCore, mods, keys
51+
1752

18-
qt_compat = pytest.importorskip('matplotlib.backends.qt_compat')
19-
QtCore = qt_compat.QtCore
53+
@pytest.fixture
54+
def qt_key(request):
55+
QtCore, _, keys = request.getfixturevalue('qt5_module')
56+
if request.param.startswith('Key'):
57+
return getattr(QtCore.Qt, request.param)
58+
else:
59+
return keys[request.param]
2060

21-
from matplotlib.backends.backend_qt5 import (
22-
MODIFIER_KEYS, SUPER, ALT, CTRL, SHIFT) # noqa
2361

24-
_, ControlModifier, ControlKey = MODIFIER_KEYS[CTRL]
25-
_, AltModifier, AltKey = MODIFIER_KEYS[ALT]
26-
_, SuperModifier, SuperKey = MODIFIER_KEYS[SUPER]
27-
_, ShiftModifier, ShiftKey = MODIFIER_KEYS[SHIFT]
62+
@pytest.fixture
63+
def qt_mods(request):
64+
QtCore, mods, _ = request.getfixturevalue('qt5_module')
65+
result = QtCore.Qt.NoModifier
66+
for mod in request.param:
67+
result |= mods[mod]
68+
return result
2869

2970

3071
@pytest.mark.backend('Qt5Agg')
@@ -47,21 +88,22 @@ def test_fig_close():
4788
@pytest.mark.parametrize(
4889
'qt_key, qt_mods, answer',
4990
[
50-
(QtCore.Qt.Key_A, ShiftModifier, 'A'),
51-
(QtCore.Qt.Key_A, QtCore.Qt.NoModifier, 'a'),
52-
(QtCore.Qt.Key_A, ControlModifier, 'ctrl+a'),
53-
(QtCore.Qt.Key_Aacute, ShiftModifier,
91+
('Key_A', ['ShiftModifier'], 'A'),
92+
('Key_A', [], 'a'),
93+
('Key_A', ['ControlModifier'], 'ctrl+a'),
94+
('Key_Aacute', ['ShiftModifier'],
5495
'\N{LATIN CAPITAL LETTER A WITH ACUTE}'),
55-
(QtCore.Qt.Key_Aacute, QtCore.Qt.NoModifier,
96+
('Key_Aacute', [],
5697
'\N{LATIN SMALL LETTER A WITH ACUTE}'),
57-
(ControlKey, AltModifier, 'alt+control'),
58-
(AltKey, ControlModifier, 'ctrl+alt'),
59-
(QtCore.Qt.Key_Aacute, (ControlModifier | AltModifier | SuperModifier),
98+
('ControlKey', ['AltModifier'], 'alt+control'),
99+
('AltKey', ['ControlModifier'], 'ctrl+alt'),
100+
('Key_Aacute', ['ControlModifier', 'AltModifier', 'SuperModifier'],
60101
'ctrl+alt+super+\N{LATIN SMALL LETTER A WITH ACUTE}'),
61-
(QtCore.Qt.Key_Backspace, QtCore.Qt.NoModifier, 'backspace'),
62-
(QtCore.Qt.Key_Backspace, ControlModifier, 'ctrl+backspace'),
63-
(QtCore.Qt.Key_Play, QtCore.Qt.NoModifier, None),
102+
('Key_Backspace', [], 'backspace'),
103+
('Key_Backspace', ['ControlModifier'], 'ctrl+backspace'),
104+
('Key_Play', [], None),
64105
],
106+
indirect=['qt_key', 'qt_mods'],
65107
ids=[
66108
'shift',
67109
'lower',

0 commit comments

Comments
 (0)