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

Skip to content

TST: Defer loading Qt framework until test is run. #12163

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 73 additions & 31 deletions lib/matplotlib/tests/test_backend_qt4.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,73 @@

import pytest

try:
import PyQt4
except (ImportError, RuntimeError): # RuntimeError if PyQt5 already imported.

@pytest.fixture(autouse=True)
def mpl_test_settings(qt4_module, mpl_test_settings):
"""
Ensure qt4_module fixture is *first* fixture.

We override the `mpl_test_settings` fixture and depend on the `qt4_module`
fixture first. It is very important that it is first, because it skips
tests when Qt4 is not available, and if not, then the main
`mpl_test_settings` fixture will try to switch backends before the skip can
be triggered.
"""
pass


@pytest.fixture
def qt4_module():
try:
import PySide
except ImportError:
pytestmark = pytest.mark.skip("Failed to import a Qt4 binding.")
import PyQt4
# RuntimeError if PyQt5 already imported.
except (ImportError, RuntimeError):
try:
import PySide
except ImportError:
pytest.skip("Failed to import a Qt4 binding.")

qt_compat = pytest.importorskip('matplotlib.backends.qt_compat')
QtCore = qt_compat.QtCore

try:
py_qt_ver = int(QtCore.PYQT_VERSION_STR.split('.')[0])
except AttributeError:
py_qt_ver = QtCore.__version_info__[0]

if py_qt_ver != 4:
pytest.skip(reason='Qt4 is not available')

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

mods = {}
keys = {}
for name, index in zip(['Alt', 'Control', 'Shift', 'Super'],
[ALT, CTRL, SHIFT, SUPER]):
_, mod, key = MODIFIER_KEYS[index]
mods[name + 'Modifier'] = mod
keys[name + 'Key'] = key

qt_compat = pytest.importorskip('matplotlib.backends.qt_compat')
QtCore = qt_compat.QtCore
return QtCore, mods, keys

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

_, ControlModifier, ControlKey = MODIFIER_KEYS[CTRL]
_, AltModifier, AltKey = MODIFIER_KEYS[ALT]
_, SuperModifier, SuperKey = MODIFIER_KEYS[SUPER]
_, ShiftModifier, ShiftKey = MODIFIER_KEYS[SHIFT]
@pytest.fixture
def qt_key(request):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can these just take qt4_module as arg? (and so on for the following ones)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They can; I did not do that yet as I was trying to figure out if we can combine these two files somehow.

QtCore, _, keys = request.getfixturevalue('qt4_module')
if request.param.startswith('Key'):
return getattr(QtCore.Qt, request.param)
else:
return keys[request.param]

try:
py_qt_ver = int(QtCore.PYQT_VERSION_STR.split('.')[0])
except AttributeError:
py_qt_ver = QtCore.__version_info__[0]

if py_qt_ver != 4:
pytestmark = pytest.mark.skipif(reason='Qt4 is not available')
@pytest.fixture
def qt_mods(request):
QtCore, mods, _ = request.getfixturevalue('qt4_module')
result = QtCore.Qt.NoModifier
for mod in request.param:
result |= mods[mod]
return result


@pytest.mark.backend('Qt4Agg')
Expand All @@ -55,21 +96,22 @@ def test_fig_close():
@pytest.mark.parametrize(
'qt_key, qt_mods, answer',
[
(QtCore.Qt.Key_A, ShiftModifier, 'A'),
(QtCore.Qt.Key_A, QtCore.Qt.NoModifier, 'a'),
(QtCore.Qt.Key_A, ControlModifier, 'ctrl+a'),
(QtCore.Qt.Key_Aacute, ShiftModifier,
('Key_A', ['ShiftModifier'], 'A'),
('Key_A', [], 'a'),
('Key_A', ['ControlModifier'], 'ctrl+a'),
('Key_Aacute', ['ShiftModifier'],
'\N{LATIN CAPITAL LETTER A WITH ACUTE}'),
(QtCore.Qt.Key_Aacute, QtCore.Qt.NoModifier,
('Key_Aacute', [],
'\N{LATIN SMALL LETTER A WITH ACUTE}'),
(ControlKey, AltModifier, 'alt+control'),
(AltKey, ControlModifier, 'ctrl+alt'),
(QtCore.Qt.Key_Aacute, (ControlModifier | AltModifier | SuperModifier),
('ControlKey', ['AltModifier'], 'alt+control'),
('AltKey', ['ControlModifier'], 'ctrl+alt'),
('Key_Aacute', ['ControlModifier', 'AltModifier', 'SuperModifier'],
'ctrl+alt+super+\N{LATIN SMALL LETTER A WITH ACUTE}'),
(QtCore.Qt.Key_Backspace, QtCore.Qt.NoModifier, 'backspace'),
(QtCore.Qt.Key_Backspace, ControlModifier, 'ctrl+backspace'),
(QtCore.Qt.Key_Play, QtCore.Qt.NoModifier, None),
('Key_Backspace', [], 'backspace'),
('Key_Backspace', ['ControlModifier'], 'ctrl+backspace'),
('Key_Play', [], None),
],
indirect=['qt_key', 'qt_mods'],
ids=[
'shift',
'lower',
Expand Down
92 changes: 67 additions & 25 deletions lib/matplotlib/tests/test_backend_qt5.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,65 @@

import pytest

try:
import PyQt5
except (ImportError, RuntimeError): # RuntimeError if PyQt4 already imported.

@pytest.fixture(autouse=True)
def mpl_test_settings(qt5_module, mpl_test_settings):
"""
Ensure qt5_module fixture is *first* fixture.

We override the `mpl_test_settings` fixture and depend on the `qt5_module`
fixture first. It is very important that it is first, because it skips
tests when Qt5 is not available, and if not, then the main
`mpl_test_settings` fixture will try to switch backends before the skip can
be triggered.
"""
pass


@pytest.fixture
def qt5_module():
try:
import PySide2
except ImportError:
pytestmark = pytest.mark.skip("Failed to import a Qt5 binding.")
import PyQt5
# RuntimeError if PyQt4 already imported.
except (ImportError, RuntimeError):
try:
import PySide2
except ImportError:
pytest.skip("Failed to import a Qt5 binding.")

qt_compat = pytest.importorskip('matplotlib.backends.qt_compat')
QtCore = qt_compat.QtCore

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

mods = {}
keys = {}
for name, index in zip(['Alt', 'Control', 'Shift', 'Super'],
[ALT, CTRL, SHIFT, SUPER]):
_, mod, key = MODIFIER_KEYS[index]
mods[name + 'Modifier'] = mod
keys[name + 'Key'] = key

return QtCore, mods, keys


qt_compat = pytest.importorskip('matplotlib.backends.qt_compat')
QtCore = qt_compat.QtCore
@pytest.fixture
def qt_key(request):
QtCore, _, keys = request.getfixturevalue('qt5_module')
if request.param.startswith('Key'):
return getattr(QtCore.Qt, request.param)
else:
return keys[request.param]

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

_, ControlModifier, ControlKey = MODIFIER_KEYS[CTRL]
_, AltModifier, AltKey = MODIFIER_KEYS[ALT]
_, SuperModifier, SuperKey = MODIFIER_KEYS[SUPER]
_, ShiftModifier, ShiftKey = MODIFIER_KEYS[SHIFT]
@pytest.fixture
def qt_mods(request):
QtCore, mods, _ = request.getfixturevalue('qt5_module')
result = QtCore.Qt.NoModifier
for mod in request.param:
result |= mods[mod]
return result


@pytest.mark.backend('Qt5Agg')
Expand All @@ -47,21 +88,22 @@ def test_fig_close():
@pytest.mark.parametrize(
'qt_key, qt_mods, answer',
[
(QtCore.Qt.Key_A, ShiftModifier, 'A'),
(QtCore.Qt.Key_A, QtCore.Qt.NoModifier, 'a'),
(QtCore.Qt.Key_A, ControlModifier, 'ctrl+a'),
(QtCore.Qt.Key_Aacute, ShiftModifier,
('Key_A', ['ShiftModifier'], 'A'),
('Key_A', [], 'a'),
('Key_A', ['ControlModifier'], 'ctrl+a'),
('Key_Aacute', ['ShiftModifier'],
'\N{LATIN CAPITAL LETTER A WITH ACUTE}'),
(QtCore.Qt.Key_Aacute, QtCore.Qt.NoModifier,
('Key_Aacute', [],
'\N{LATIN SMALL LETTER A WITH ACUTE}'),
(ControlKey, AltModifier, 'alt+control'),
(AltKey, ControlModifier, 'ctrl+alt'),
(QtCore.Qt.Key_Aacute, (ControlModifier | AltModifier | SuperModifier),
('ControlKey', ['AltModifier'], 'alt+control'),
('AltKey', ['ControlModifier'], 'ctrl+alt'),
('Key_Aacute', ['ControlModifier', 'AltModifier', 'SuperModifier'],
'ctrl+alt+super+\N{LATIN SMALL LETTER A WITH ACUTE}'),
(QtCore.Qt.Key_Backspace, QtCore.Qt.NoModifier, 'backspace'),
(QtCore.Qt.Key_Backspace, ControlModifier, 'ctrl+backspace'),
(QtCore.Qt.Key_Play, QtCore.Qt.NoModifier, None),
('Key_Backspace', [], 'backspace'),
('Key_Backspace', ['ControlModifier'], 'ctrl+backspace'),
('Key_Play', [], None),
],
indirect=['qt_key', 'qt_mods'],
ids=[
'shift',
'lower',
Expand Down