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

Skip to content

Commit 4d49e44

Browse files
committed
Various backend cleanups.
Split out from the qt5cairo and wxcairo PRs. - GTK3: is_drawable is the same as mapped & visible (https://developer.gnome.org/gtk3/stable/GtkWidget.html#gtk-widget-is-drawable) - Synchronous draw appears unnecessary on GTK3 based on tests (probably was only needed in early animation code). - Most of the Wx printout code has been removed in 84ffb60; this PR just deprecates some remnants of it. MenuButtonWx is likewise unused since the removal of "classic" toolbars in 4243470; this PR deprecates it. - Don't initialize the wx statusbar to "None" (which just looks strange) -- other backends just start with an empty status text. - _convert_agg_to_wx_image is unused; _WX28_clipped_agg_as_bitmap can reasonably be inlined into _convert_agg_to_wx_bitmap. - Deprecate some unused tk apis. - Simplify toolkit mocking in the docs. In particular pycairo is Py3-compatible only since 1.11.0 so the version_check is unneeded.
1 parent f4b5618 commit 4d49e44

11 files changed

+63
-133
lines changed

.flake8

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ per-file-ignores =
3131
matplotlib/_cm.py: E202, E203
3232
matplotlib/_mathtext_data.py: E203, E261
3333
matplotlib/backend_bases.py: E225
34-
matplotlib/backends/_backend_tk.py: E203, E222, E225, E231, E271, E301, E303, E401, E501, E701
34+
matplotlib/backends/_backend_tk.py: E203, E222, E225, E231, E271, E301, E303, E501, E701
3535
matplotlib/backends/backend_agg.py: E261, E302, E303, E701
3636
matplotlib/backends/backend_cairo.py: E203, E221, E261, E303, E402, E711
3737
matplotlib/backends/backend_gtk3.py: E203, E221, E222, E225, E251, E261, E501

doc/api/backend_wxagg_api.rst

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
:mod:`matplotlib.backends.backend_wxagg`
33
========================================
44

5-
.. automodule:: matplotlib.backends.backend_wxagg
6-
:members:
7-
:undoc-members:
8-
:show-inheritance:
5+
**NOTE** Not included, to avoid adding a dependency to building the docs.
6+
7+
.. .. automodule:: matplotlib.backends.backend_wxagg
8+
.. :members:
9+
.. :undoc-members:
10+
.. :show-inheritance:

doc/api/next_api_changes/2018-02-15-AL-deprecations.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@ The following classes, methods, functions, and attributes are deprecated:
1414
``RcParams.msg_depr_set``, ``RcParams.msg_obsolete``,
1515
``RcParams.msg_backend_obsolete``,
1616
- ``afm.parse_afm``,
17+
- ``backend_gtk3cairo.FigureManagerGTK3Cairo``,
1718
- ``backend_pgf.get_texcommand``,
1819
- ``backend_ps.get_bbox``,
1920
- ``backend_qt5.error_msg_qt``, ``backend_qt5.exception_handler``,
20-
- ``backend_wx.FigureCanvasWx.macros``,
21+
- ``backend_tk.raise_msg_to_str``, ``backend_tk.error_msg_tkpaint``,
22+
- ``backend_wx.FigureCanvasWx.macros``, ``backend_wx.MenuButtonWx``,
23+
``backend_wx.PrintoutWx``, ``backend_wx.debug_on_error``,
24+
``backend_wx.fake_stderr``, ``backend_wx.raise_msg_to_str``,
2125
- ``cbook.GetRealpathAndStat``, ``cbook.Locked``,
2226
- ``cbook.is_numlike`` (use ``isinstance(..., numbers.Number)`` instead),
2327
``cbook.listFiles``, ``cbook.unicode_safe``,

doc/sphinxext/mock_gui_toolkits.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -108,25 +108,10 @@ def getapi(*args):
108108
return 1
109109

110110

111-
class MyWX(MagicMock):
112-
class Panel(object):
113-
pass
114-
115-
class ToolBar(object):
116-
pass
117-
118-
class Frame(object):
119-
pass
120-
121-
class StatusBar(object):
122-
pass
123-
124-
125111
def setup(app):
126112
sys.modules.update(
127113
cairocffi=MyCairoCffi(),
128114
PyQt4=MyPyQt4(),
129115
sip=MySip(),
130-
wx=MyWX(),
131116
)
132117
return {'parallel_read_safe': True, 'parallel_write_safe': True}

lib/matplotlib/backends/_backend_tk.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
1-
import math
1+
from contextlib import contextmanager
22
import logging
3+
import math
34
import os.path
45
import sys
56
import tkinter as Tk
67
from tkinter.simpledialog import SimpleDialog
7-
from contextlib import contextmanager
8+
import tkinter.filedialog
9+
import tkinter.messagebox
810

911
import numpy as np
1012

11-
from . import _tkagg
12-
1313
import matplotlib
14-
from matplotlib import backend_tools, rcParams
14+
from matplotlib import backend_tools, cbook, rcParams
1515
from matplotlib.backend_bases import (
1616
_Backend, FigureCanvasBase, FigureManagerBase, NavigationToolbar2,
1717
StatusbarBase, TimerBase, ToolContainerBase, cursors)
1818
from matplotlib.backend_managers import ToolManager
1919
from matplotlib._pylab_helpers import Gcf
2020
from matplotlib.figure import Figure
2121
from matplotlib.widgets import SubplotTool
22+
from . import _tkagg
2223

2324
try:
2425
from matplotlib._windowing import GetForegroundWindow, SetForegroundWindow
@@ -54,15 +55,16 @@ def _restore_foreground_window_at_end():
5455
}
5556

5657

58+
@cbook.deprecated("3.0")
5759
def raise_msg_to_str(msg):
5860
"""msg is a return arg from a raise. Join with new lines"""
5961
if not isinstance(msg, str):
6062
msg = '\n'.join(map(str, msg))
6163
return msg
6264

6365

66+
@cbook.deprecated("3.0")
6467
def error_msg_tkpaint(msg, parent=None):
65-
import tkinter.messagebox
6668
tkinter.messagebox.showerror("matplotlib", msg)
6769

6870

@@ -685,7 +687,6 @@ def configure_subplots(self):
685687
window.grab_set()
686688

687689
def save_figure(self, *args):
688-
import tkinter.filedialog, tkinter.messagebox
689690
filetypes = self.canvas.get_supported_filetypes().copy()
690691
default_filetype = self.canvas.get_default_filetype()
691692

@@ -905,7 +906,6 @@ def set_message(self, s):
905906

906907
class SaveFigureTk(backend_tools.SaveFigureBase):
907908
def trigger(self, *args):
908-
import tkinter.filedialog, tkinter.messagebox
909909
filetypes = self.figure.canvas.get_supported_filetypes().copy()
910910
default_filetype = self.figure.canvas.get_default_filetype()
911911

lib/matplotlib/backends/backend_agg.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -409,21 +409,13 @@ def restore_region(self, region, bbox=None, xy=None):
409409
return renderer.restore_region(region, bbox, xy)
410410

411411
def draw(self):
412-
"""
413-
Draw the figure using the renderer
414-
"""
412+
"""Draw the figure using the renderer."""
415413
self.renderer = self.get_renderer(cleared=True)
416-
# acquire a lock on the shared font cache
417-
RendererAgg.lock.acquire()
418-
419-
toolbar = self.toolbar
420-
try:
414+
with RendererAgg.lock:
421415
self.figure.draw(self.renderer)
422-
# A GUI class may be need to update a window using this draw, so
423-
# don't forget to call the superclass.
416+
# A GUI class may be need to update a window using this draw,
417+
# so don't forget to call the superclass.
424418
super().draw()
425-
finally:
426-
RendererAgg.lock.release()
427419

428420
def get_renderer(self, cleared=False):
429421
l, b, w, h = self.figure.bbox.bounds

lib/matplotlib/backends/backend_gtk3.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,8 @@ def on_draw_event(self, widget, ctx):
278278
pass
279279

280280
def draw(self):
281-
if self.get_visible() and self.get_mapped():
281+
if self.is_drawable():
282282
self.queue_draw()
283-
# do a synchronous draw (its less efficient than an async draw,
284-
# but is required if/when animation is used)
285-
self.get_property("window").process_updates(False)
286283

287284
def draw_idle(self):
288285
if self._idle_draw_id != 0:

lib/matplotlib/backends/backend_gtk3cairo.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from . import backend_cairo, backend_gtk3
22
from ._gtk3_compat import gi
33
from .backend_gtk3 import Gtk, _BackendGTK3
4+
from matplotlib import cbook
45
from matplotlib.backend_bases import cursors
56

67

@@ -36,11 +37,11 @@ def on_draw_event(self, widget, ctx):
3637
return False # finish event propagation?
3738

3839

40+
@cbook.deprecated("3.0", "backend_gtk3.FigureManagerGTK3")
3941
class FigureManagerGTK3Cairo(backend_gtk3.FigureManagerGTK3):
4042
pass
4143

4244

4345
@_BackendGTK3.export
4446
class _BackendGTK3Cairo(_BackendGTK3):
4547
FigureCanvas = FigureCanvasGTK3Cairo
46-
FigureManager = FigureManagerGTK3Cairo

lib/matplotlib/backends/backend_qt5agg.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Render to qt from agg
2+
Render to qt from agg.
33
"""
44

55
import ctypes
@@ -30,8 +30,8 @@ def paintEvent(self, e):
3030
return
3131
self._draw_idle() # Only does something if a draw is pending.
3232

33-
# if the canvas does not have a renderer, then give up and wait for
34-
# FigureCanvasAgg.draw(self) to be called
33+
# If the canvas does not have a renderer, then give up and wait for
34+
# FigureCanvasAgg.draw(self) to be called.
3535
if not hasattr(self, 'renderer'):
3636
return
3737

lib/matplotlib/backends/backend_wx.py

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020
StatusbarBase)
2121
from matplotlib.backend_bases import _has_pil
2222

23+
from matplotlib import cbook, rcParams, backend_tools
2324
from matplotlib._pylab_helpers import Gcf
25+
from matplotlib.backend_managers import ToolManager
2426
from matplotlib.cbook import is_writable_file_like, warn_deprecated
2527
from matplotlib.figure import Figure
2628
from matplotlib.path import Path
2729
from matplotlib.transforms import Affine2D
2830
from matplotlib.widgets import SubplotTool
29-
from matplotlib.backend_managers import ToolManager
30-
from matplotlib import cbook, rcParams, backend_tools
3131

3232
import wx
3333

@@ -37,30 +37,25 @@
3737
# traceback is performed, and pdb activated, for all uncaught exceptions in
3838
# this case
3939
_DEBUG = 5
40-
if _DEBUG < 5:
41-
import traceback
42-
import pdb
4340
_DEBUG_lvls = {1: 'Low ', 2: 'Med ', 3: 'High', 4: 'Error'}
4441

4542

4643
def DEBUG_MSG(string, lvl=3, o=None):
4744
if lvl >= _DEBUG:
48-
cls = o.__class__
49-
# Jeremy, often times the commented line won't print but the
50-
# one below does. I think WX is redefining stderr, damned
51-
# beast
52-
# print("%s- %s in %s" % (_DEBUG_lvls[lvl], string, cls),
53-
# file=sys.stderr)
54-
print("%s- %s in %s" % (_DEBUG_lvls[lvl], string, cls))
45+
print("%s- %s in %s" % (_DEBUG_lvls[lvl], string, type(o)))
5546

5647

48+
@cbook.deprecated("3.0")
5749
def debug_on_error(type, value, tb):
5850
"""Code due to Thomas Heller - published in Python Cookbook (O'Reilley)"""
51+
import pdb
52+
import traceback
5953
traceback.print_exception(type, value, tb)
6054
print()
61-
pdb.pm() # jdh uncomment
55+
pdb.pm()
6256

6357

58+
@cbook.deprecated("3.0")
6459
class fake_stderr(object):
6560
"""
6661
Wx does strange things with stderr, as it makes the assumption that
@@ -95,6 +90,7 @@ def error_msg_wx(msg, parent=None):
9590
return None
9691

9792

93+
@cbook.deprecated("3.0")
9894
def raise_msg_to_str(msg):
9995
"""msg is a return arg from a raise. Join with new lines."""
10096
if not isinstance(msg, str):
@@ -1099,22 +1095,16 @@ def _print_image(self, filename, filetype, *args, **kwargs):
10991095
image = self.bitmap.ConvertToImage()
11001096
image.SetOption(wx.IMAGE_OPTION_QUALITY, str(jpeg_quality))
11011097

1102-
# Now that we have rendered into the bitmap, save it
1103-
# to the appropriate file type and clean up
1098+
# Now that we have rendered into the bitmap, save it to the appropriate
1099+
# file type and clean up.
11041100
if isinstance(filename, str):
11051101
if not image.SaveFile(filename, filetype):
1106-
DEBUG_MSG('print_figure() file save error', 4, self)
1107-
raise RuntimeError(
1108-
'Could not save figure to %s\n' %
1109-
(filename))
1102+
raise RuntimeError('Could not save figure to %s' % filename)
11101103
elif is_writable_file_like(filename):
11111104
if not isinstance(image, wx.Image):
11121105
image = image.ConvertToImage()
11131106
if not image.SaveStream(filename, filetype):
1114-
DEBUG_MSG('print_figure() file save error', 4, self)
1115-
raise RuntimeError(
1116-
'Could not save figure to %s\n' %
1117-
(filename))
1107+
raise RuntimeError('Could not save figure to %s' % filename)
11181108

11191109
# Restore everything to normal
11201110
self.bitmap = origBitmap
@@ -1300,11 +1290,7 @@ def resize(self, width, height):
13001290
self.canvas.SetInitialSize(wx.Size(width, height))
13011291
self.window.GetSizer().Fit(self.window)
13021292

1303-
# Identifiers for toolbar controls - images_wx contains bitmaps for the images
1304-
# used in the controls. wxWindows does not provide any stock images, so I've
1305-
# 'stolen' those from GTK2, and transformed them into the appropriate format.
1306-
# import images_wx
1307-
1293+
# Identifiers for toolbar controls.
13081294

13091295
_NTB_AXISMENU = wx.NewId()
13101296
_NTB_AXISMENU_BUTTON = wx.NewId()
@@ -1316,7 +1302,6 @@ def resize(self, width, height):
13161302
_NTB_Y_PAN_DOWN = wx.NewId()
13171303
_NTB_Y_ZOOMIN = wx.NewId()
13181304
_NTB_Y_ZOOMOUT = wx.NewId()
1319-
# _NTB_SUBPLOT =wx.NewId()
13201305
_NTB_SAVE = wx.NewId()
13211306
_NTB_CLOSE = wx.NewId()
13221307

@@ -1354,6 +1339,7 @@ def _set_frame_icon(frame):
13541339
frame.SetIcons(bundle)
13551340

13561341

1342+
@cbook.deprecated("3.0")
13571343
class MenuButtonWx(wx.Button):
13581344
"""
13591345
wxPython does not permit a menu to be incorporated directly into a toolbar.
@@ -1431,7 +1417,7 @@ def updateAxes(self, maxAxis):
14311417
"""Ensures that there are entries for max_axis axes in the menu
14321418
(selected by default)."""
14331419
if maxAxis > len(self._axisId):
1434-
for i in range(len(self._axisId) + 1, maxAxis + 1, 1):
1420+
for i in range(len(self._axisId) + 1, maxAxis + 1):
14351421
menuId = wx.NewId()
14361422
self._axisId.append(menuId)
14371423
self._menu.Append(menuId, "Axis %d" % i,
@@ -1705,16 +1691,10 @@ class StatusBarWx(wx.StatusBar):
17051691
def __init__(self, parent, *args, **kwargs):
17061692
wx.StatusBar.__init__(self, parent, -1)
17071693
self.SetFieldsCount(2)
1708-
self.SetStatusText("None", 1)
1709-
# self.SetStatusText("Measurement: None", 2)
1710-
# self.Reposition()
17111694

17121695
def set_function(self, string):
17131696
self.SetStatusText("%s" % string, 1)
17141697

1715-
# def set_measurement(self, string):
1716-
# self.SetStatusText("Measurement: %s" % string, 2)
1717-
17181698

17191699
# tools for matplotlib.backend_managers.ToolManager:
17201700

@@ -2024,6 +2004,7 @@ def trigger(self, *args, **kwargs):
20242004

20252005
# < Additions for printing support: Matt Newville
20262006

2007+
@cbook.deprecated("3.0")
20272008
class PrintoutWx(wx.Printout):
20282009
"""
20292010
Simple wrapper around wx Printout class -- all the real work
@@ -2105,7 +2086,6 @@ def OnPrintPage(self, page):
21052086
self.canvas.figure.dpi = fig_dpi
21062087
self.canvas.draw()
21072088
return True
2108-
# >
21092089

21102090

21112091
@_Backend.export

0 commit comments

Comments
 (0)