From c38137a3991f13cb7c1c5e1ccc4bee09c5a16cbf Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Tue, 16 Feb 2016 23:14:58 -0500 Subject: [PATCH 1/6] MNT/API: remove __all__ from backends.__init__.py This would be an API break, however at some point most of these attributes were removed from the module so trying to bulk import would raise an AttributeError. --- lib/matplotlib/backends/__init__.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/lib/matplotlib/backends/__init__.py b/lib/matplotlib/backends/__init__.py index 86999e9ebe3..8bec338eae6 100644 --- a/lib/matplotlib/backends/__init__.py +++ b/lib/matplotlib/backends/__init__.py @@ -10,9 +10,6 @@ # ipython relies on interactive_bk being defined here from matplotlib.rcsetup import interactive_bk -__all__ = ['backend','show','draw_if_interactive', - 'new_figure_manager', 'backend_version'] - backend = matplotlib.get_backend() # validates, to match all_backends def pylab_setup(): @@ -51,12 +48,6 @@ def do_nothing(*args, **kwargs): pass show = getattr(backend_mod, 'show', do_nothing_show) draw_if_interactive = getattr(backend_mod, 'draw_if_interactive', do_nothing) - # Additional imports which only happen for certain backends. This section - # should probably disappear once all backends are uniform. - if backend.lower() in ['wx','wxagg']: - Toolbar = backend_mod.Toolbar - __all__.append('Toolbar') - matplotlib.verbose.report('backend %s version %s' % (backend,backend_version)) return backend_mod, new_figure_manager, draw_if_interactive, show From b67ee9e46c549aff54696c173920ec494f677cf5 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Tue, 16 Feb 2016 23:16:29 -0500 Subject: [PATCH 2/6] MNT: move backend lookup inside pylab_setup closes #5513 --- lib/matplotlib/backends/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/backends/__init__.py b/lib/matplotlib/backends/__init__.py index 8bec338eae6..50d4edaa61e 100644 --- a/lib/matplotlib/backends/__init__.py +++ b/lib/matplotlib/backends/__init__.py @@ -10,11 +10,11 @@ # ipython relies on interactive_bk being defined here from matplotlib.rcsetup import interactive_bk -backend = matplotlib.get_backend() # validates, to match all_backends def pylab_setup(): 'return new_figure_manager, draw_if_interactive and show for pylab' # Import the requested backend into a generic module object + backend = matplotlib.get_backend() # validates, to match all_backends if backend.startswith('module://'): backend_name = backend[9:] From d2c9e35eeeb0b294685c822c7cbea446c4d5a8f8 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Tue, 16 Feb 2016 23:19:14 -0500 Subject: [PATCH 3/6] MNT: remove unused import The comment says that this is used by IPython (circa 2008) however it's use was removed from the IPython code base in 770734dc47d57cdebc365fcbcd313e76a22be89d Author: Brian Granger 2009-07-21 15:28:37 which Precedes: rel-0.11 so no supported version of IPython still has this code. --- lib/matplotlib/backends/__init__.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/matplotlib/backends/__init__.py b/lib/matplotlib/backends/__init__.py index 50d4edaa61e..4d716da36ac 100644 --- a/lib/matplotlib/backends/__init__.py +++ b/lib/matplotlib/backends/__init__.py @@ -7,9 +7,6 @@ import inspect import warnings -# ipython relies on interactive_bk being defined here -from matplotlib.rcsetup import interactive_bk - def pylab_setup(): 'return new_figure_manager, draw_if_interactive and show for pylab' From b0db83ee8d004475b4b38772a7e7a7c1b660b660 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Tue, 16 Feb 2016 23:29:48 -0500 Subject: [PATCH 4/6] MNT: move pylab_setup with rest of imports Now that the backend resolution is done in `pylab_setup` instead of at module import time this does not need to be imported after the call to _backend_selection. --- lib/matplotlib/pyplot.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index 23feaf7f57b..b492227476e 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -65,7 +65,7 @@ Locator, IndexLocator, FixedLocator, NullLocator,\ LinearLocator, LogLocator, AutoLocator, MultipleLocator,\ MaxNLocator - +from matplotlib.backends import pylab_setup ## Backend detection ## def _backend_selection(): @@ -110,7 +110,6 @@ def _backend_selection(): ## Global ## -from matplotlib.backends import pylab_setup _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup() _IP_REGISTERED = None From 35b80cc33f819315ccff088df98a64767f69a1e6 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Tue, 16 Feb 2016 23:40:07 -0500 Subject: [PATCH 5/6] MNT/DOC: add input to pylab_setup + docs - add backend as an input (maybe useful?) - add numpydoc style docstring --- lib/matplotlib/backends/__init__.py | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/backends/__init__.py b/lib/matplotlib/backends/__init__.py index 4d716da36ac..fa0c0808e9e 100644 --- a/lib/matplotlib/backends/__init__.py +++ b/lib/matplotlib/backends/__init__.py @@ -8,10 +8,36 @@ import warnings -def pylab_setup(): - 'return new_figure_manager, draw_if_interactive and show for pylab' +def pylab_setup(backend=None): + '''return new_figure_manager, draw_if_interactive and show for pyplot + + This provides the backend-specific functions that are used by + pyplot to abstract away the difference between interactive backends. + + Parameters + ---------- + backend : str, optional + The name of the backend to use. If `None`, falls back to + ``matplotlib.get_backend()`` (which return ``rcParams['backend']``) + + Returns + ------- + backend_mod : module + The module which contains the backend of choice + + new_figure_manager : function + Create a new figure manage (roughly maps to GUI window) + + draw_if_interactive : function + Redraw the current figure if pyplot is interactive + + show : function + Show (and possible block) any unshown figures. + + ''' # Import the requested backend into a generic module object - backend = matplotlib.get_backend() # validates, to match all_backends + if backend is None: + backend = matplotlib.get_backend() # validates, to match all_backends if backend.startswith('module://'): backend_name = backend[9:] From b3c4e0a994726e618740d33f1886af227f7c3b1a Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Tue, 16 Feb 2016 23:43:04 -0500 Subject: [PATCH 6/6] STY: style cleanup --- lib/matplotlib/backends/__init__.py | 26 +++++++++++++++++--------- lib/matplotlib/tests/test_coding_standards.py | 1 - 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/lib/matplotlib/backends/__init__.py b/lib/matplotlib/backends/__init__.py index fa0c0808e9e..334113708fb 100644 --- a/lib/matplotlib/backends/__init__.py +++ b/lib/matplotlib/backends/__init__.py @@ -42,14 +42,14 @@ def pylab_setup(backend=None): if backend.startswith('module://'): backend_name = backend[9:] else: - backend_name = 'backend_'+backend - backend_name = backend_name.lower() # until we banish mixed case - backend_name = 'matplotlib.backends.%s'%backend_name.lower() + backend_name = 'backend_' + backend + backend_name = backend_name.lower() # until we banish mixed case + backend_name = 'matplotlib.backends.%s' % backend_name.lower() # the last argument is specifies whether to use absolute or relative # imports. 0 means only perform absolute imports. - backend_mod = __import__(backend_name, - globals(),locals(),[backend_name],0) + backend_mod = __import__(backend_name, globals(), locals(), + [backend_name], 0) # Things we pull in from all backends new_figure_manager = backend_mod.new_figure_manager @@ -66,11 +66,19 @@ def do_nothing_show(*args, **kwargs): Please select a GUI backend in your matplotlibrc file ('%s') or with matplotlib.use()""" % (backend, matplotlib.matplotlib_fname())) - def do_nothing(*args, **kwargs): pass - backend_version = getattr(backend_mod,'backend_version', 'unknown') + + def do_nothing(*args, **kwargs): + pass + + backend_version = getattr(backend_mod, 'backend_version', + 'unknown') + show = getattr(backend_mod, 'show', do_nothing_show) - draw_if_interactive = getattr(backend_mod, 'draw_if_interactive', do_nothing) - matplotlib.verbose.report('backend %s version %s' % (backend,backend_version)) + draw_if_interactive = getattr(backend_mod, 'draw_if_interactive', + do_nothing) + + matplotlib.verbose.report('backend %s version %s' % + (backend, backend_version)) return backend_mod, new_figure_manager, draw_if_interactive, show diff --git a/lib/matplotlib/tests/test_coding_standards.py b/lib/matplotlib/tests/test_coding_standards.py index 6734fde7b6e..8a1a2c96a74 100644 --- a/lib/matplotlib/tests/test_coding_standards.py +++ b/lib/matplotlib/tests/test_coding_standards.py @@ -216,7 +216,6 @@ def test_pep8_conformance_installed_files(): 'tests/test_tightlayout.py', 'tests/test_triangulation.py', 'compat/subprocess.py', - 'backends/__init__.py', 'backends/backend_agg.py', 'backends/backend_cairo.py', 'backends/backend_cocoaagg.py',