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

Skip to content

Commit 84aa9f7

Browse files
committed
TST: properly fence-post qt{4,5} backend tests
This issue is that the new 'default' qt version is 5 if it is available. In the tests the backend is Agg so we are hitting the fall through behavior. If both pyqt4 and pyqt5 are installed, then `QtCore` will be from PyQt5, but the backend_qt4Agg code assumes that it is seeing the PyQt4 version of the classes. This results in error on `__init__` due to the change between PyQt4 and PyQt5. Added test_backend_qt5.py to white listed tests. closes #5194
1 parent 522c31c commit 84aa9f7

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

lib/matplotlib/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,6 +1429,7 @@ def tk_window_focus():
14291429
'matplotlib.tests.test_backend_pgf',
14301430
'matplotlib.tests.test_backend_ps',
14311431
'matplotlib.tests.test_backend_qt4',
1432+
'matplotlib.tests.test_backend_qt5',
14321433
'matplotlib.tests.test_backend_svg',
14331434
'matplotlib.tests.test_basic',
14341435
'matplotlib.tests.test_bbox_tight',

lib/matplotlib/tests/test_backend_qt4.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,19 @@
1717

1818
try:
1919
from matplotlib.backends.qt_compat import QtCore
20+
2021
from matplotlib.backends.backend_qt4 import (MODIFIER_KEYS,
2122
SUPER, ALT, CTRL, SHIFT)
2223

2324
_, ControlModifier, ControlKey = MODIFIER_KEYS[CTRL]
2425
_, AltModifier, AltKey = MODIFIER_KEYS[ALT]
2526
_, SuperModifier, SuperKey = MODIFIER_KEYS[SUPER]
2627
_, ShiftModifier, ShiftKey = MODIFIER_KEYS[SHIFT]
27-
HAS_QT = True
28+
py_qt_ver = int(QtCore.PYQT_VERSION_STR.split('.')[0])
29+
if py_qt_ver != 4:
30+
HAS_QT = False
31+
else:
32+
HAS_QT = True
2833
except ImportError:
2934
HAS_QT = False
3035

@@ -33,7 +38,7 @@
3338
@knownfailureif(not HAS_QT)
3439
@switch_backend('Qt4Agg')
3540
def test_fig_close():
36-
#save the state of Gcf.figs
41+
# save the state of Gcf.figs
3742
init_figs = copy.copy(Gcf.figs)
3843

3944
# make a figure using pyplot interface

lib/matplotlib/tests/test_backend_qt5.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from matplotlib.externals import six
55

66
from matplotlib import pyplot as plt
7-
from matplotlib.testing.decorators import cleanup
7+
from matplotlib.testing.decorators import cleanup, switch_backend
88
from matplotlib.testing.decorators import knownfailureif
99
from matplotlib._pylab_helpers import Gcf
1010
import copy
@@ -24,17 +24,19 @@
2424
_, AltModifier, AltKey = MODIFIER_KEYS[ALT]
2525
_, SuperModifier, SuperKey = MODIFIER_KEYS[SUPER]
2626
_, ShiftModifier, ShiftKey = MODIFIER_KEYS[SHIFT]
27-
HAS_QT = True
27+
py_qt_ver = int(QtCore.PYQT_VERSION_STR.split('.')[0])
28+
if py_qt_ver != 5:
29+
HAS_QT = False
30+
else:
31+
HAS_QT = True
2832
except ImportError:
2933
HAS_QT = False
3034

3135

3236
@cleanup
3337
@knownfailureif(not HAS_QT)
38+
@switch_backend('Qt5Agg')
3439
def test_fig_close():
35-
# force switch to the Qt5 backend
36-
plt.switch_backend('Qt5Agg')
37-
3840
# save the state of Gcf.figs
3941
init_figs = copy.copy(Gcf.figs)
4042

@@ -50,14 +52,14 @@ def test_fig_close():
5052
assert(init_figs == Gcf.figs)
5153

5254

55+
@switch_backend('Qt5Agg')
5356
def assert_correct_key(qt_key, qt_mods, answer):
5457
"""
5558
Make a figure
5659
Send a key_press_event event (using non-public, qt5 backend specific api)
5760
Catch the event
5861
Assert sent and caught keys are the same
5962
"""
60-
plt.switch_backend('Qt5Agg')
6163
qt_canvas = plt.figure().canvas
6264

6365
event = mock.Mock()
@@ -101,15 +103,15 @@ def test_control():
101103
def test_unicode_upper():
102104
assert_correct_key(QtCore.Qt.Key_Aacute,
103105
ShiftModifier,
104-
unichr(193))
106+
chr(193))
105107

106108

107109
@cleanup
108110
@knownfailureif(not HAS_QT)
109111
def test_unicode_lower():
110112
assert_correct_key(QtCore.Qt.Key_Aacute,
111113
QtCore.Qt.NoModifier,
112-
unichr(225))
114+
chr(225))
113115

114116

115117
@cleanup
@@ -133,7 +135,7 @@ def test_control_alt():
133135
def test_modifier_order():
134136
assert_correct_key(QtCore.Qt.Key_Aacute,
135137
(ControlModifier | AltModifier | SuperModifier),
136-
'ctrl+alt+super+' + unichr(225))
138+
'ctrl+alt+super+' + chr(225))
137139

138140

139141
@cleanup

0 commit comments

Comments
 (0)