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

Skip to content

Commit c4b9f4e

Browse files
committed
Merge pull request #5195 from tacaswell/fix_backend_qt_test
TST: properly fence-post qt{4,5} backend tests
2 parents 110e1ed + ada2f99 commit c4b9f4e

File tree

5 files changed

+47
-26
lines changed

5 files changed

+47
-26
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/backends/backend_qt4agg.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from matplotlib.figure import Figure
1616

1717

18-
from .backend_qt5agg import FigureCanvasQTAggBase
18+
from .backend_qt5agg import FigureCanvasQTAggBase as _FigureCanvasQTAggBase
1919

2020
from .backend_agg import FigureCanvasAgg
2121
from .backend_qt4 import QtCore
@@ -54,6 +54,11 @@ def new_figure_manager_given_figure(num, figure):
5454
return FigureManagerQT(canvas, num)
5555

5656

57+
class FigureCanvasQTAggBase(_FigureCanvasQTAggBase):
58+
def __init__(self, figure):
59+
self._agg_draw_pending = False
60+
61+
5762
class FigureCanvasQTAgg(FigureCanvasQTAggBase,
5863
FigureCanvasQT, FigureCanvasAgg):
5964
"""

lib/matplotlib/backends/qt_compat.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -139,19 +139,22 @@ def _getSaveFileName(*args, **kwargs):
139139
# call to getapi() can fail in older versions of sip
140140
def _getSaveFileName(*args, **kwargs):
141141
return QtGui.QFileDialog.getSaveFileName(*args, **kwargs), None
142-
143-
# Alias PyQt-specific functions for PySide compatibility.
144-
QtCore.Signal = QtCore.pyqtSignal
145142
try:
146-
QtCore.Slot = QtCore.pyqtSlot
147-
except AttributeError:
148-
# Not a perfect match but works in simple cases
149-
QtCore.Slot = QtCore.pyqtSignature
150-
151-
QtCore.Property = QtCore.pyqtProperty
152-
__version__ = QtCore.PYQT_VERSION_STR
143+
# Alias PyQt-specific functions for PySide compatibility.
144+
QtCore.Signal = QtCore.pyqtSignal
145+
try:
146+
QtCore.Slot = QtCore.pyqtSlot
147+
except AttributeError:
148+
# Not a perfect match but works in simple cases
149+
QtCore.Slot = QtCore.pyqtSignature
150+
151+
QtCore.Property = QtCore.pyqtProperty
152+
__version__ = QtCore.PYQT_VERSION_STR
153+
except NameError:
154+
# QtCore did not get imported, fall back to pyside
155+
QT_API = QT_API_PYSIDE
153156

154-
else: # try importing pyside
157+
if QT_API == QT_API_PYSIDE: # try importing pyside
155158
try:
156159
from PySide import QtCore, QtGui, __version__, __version_info__
157160
except ImportError:

lib/matplotlib/tests/test_backend_qt4.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from matplotlib.testing.decorators import cleanup, switch_backend
88
from matplotlib.testing.decorators import knownfailureif
99
from matplotlib._pylab_helpers import Gcf
10+
import matplotlib.style as mstyle
1011
import copy
1112

1213
try:
@@ -16,15 +17,24 @@
1617
import mock
1718

1819
try:
19-
from matplotlib.backends.qt_compat import QtCore
20+
with mstyle.context({'backend': 'Qt4Agg'}):
21+
from matplotlib.backends.qt_compat import QtCore
22+
2023
from matplotlib.backends.backend_qt4 import (MODIFIER_KEYS,
2124
SUPER, ALT, CTRL, SHIFT)
2225

2326
_, ControlModifier, ControlKey = MODIFIER_KEYS[CTRL]
2427
_, AltModifier, AltKey = MODIFIER_KEYS[ALT]
2528
_, SuperModifier, SuperKey = MODIFIER_KEYS[SUPER]
2629
_, ShiftModifier, ShiftKey = MODIFIER_KEYS[SHIFT]
27-
HAS_QT = True
30+
31+
try:
32+
py_qt_ver = int(QtCore.PYQT_VERSION_STR.split('.')[0])
33+
except AttributeError:
34+
py_qt_ver = QtCore.__version_info__[0]
35+
print(py_qt_ver)
36+
HAS_QT = py_qt_ver == 4
37+
2838
except ImportError:
2939
HAS_QT = False
3040

@@ -33,7 +43,7 @@
3343
@knownfailureif(not HAS_QT)
3444
@switch_backend('Qt4Agg')
3545
def test_fig_close():
36-
#save the state of Gcf.figs
46+
# save the state of Gcf.figs
3747
init_figs = copy.copy(Gcf.figs)
3848

3949
# make a figure using pyplot interface

lib/matplotlib/tests/test_backend_qt5.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
from __future__ import (absolute_import, division, print_function,
22
unicode_literals)
3-
43
from matplotlib.externals import six
54

65
from matplotlib import pyplot as plt
7-
from matplotlib.testing.decorators import cleanup
6+
from matplotlib.testing.decorators import cleanup, switch_backend
87
from matplotlib.testing.decorators import knownfailureif
98
from matplotlib._pylab_helpers import Gcf
9+
import matplotlib.style as mstyle
1010
import copy
1111

1212
try:
@@ -16,25 +16,27 @@
1616
import mock
1717

1818
try:
19-
from matplotlib.backends.qt_compat import QtCore
19+
with mstyle.context({'backend': 'Qt5Agg'}):
20+
from matplotlib.backends.qt_compat import QtCore, __version__
2021
from matplotlib.backends.backend_qt5 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+
29+
py_qt_ver = int(__version__.split('.')[0])
30+
HAS_QT = py_qt_ver == 5
31+
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+
six.unichr(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+
six.unichr(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+' + six.unichr(225))
137139

138140

139141
@cleanup

0 commit comments

Comments
 (0)