diff --git a/lib/matplotlib/__init__.py b/lib/matplotlib/__init__.py index f3c5d87d1280..e8273d8adcb4 100644 --- a/lib/matplotlib/__init__.py +++ b/lib/matplotlib/__init__.py @@ -106,6 +106,11 @@ import distutils.sysconfig import distutils.version +# cbook must import matplotlib only within function +# definitions, so it is safe to import from it here. +from matplotlib.cbook import MatplotlibDeprecationWarning +from matplotlib.cbook import is_string_like + try: reload except NameError: @@ -123,19 +128,6 @@ sys.argv = ['modpython'] -class MatplotlibDeprecationWarning(UserWarning): - """ - A class for issuing deprecation warnings for Matplotlib users. - - In light of the fact that Python builtin DeprecationWarnings are ignored - by default as of Python 2.7 (see link below), this class was put in to - allow for the signaling of deprecation, but via UserWarnings which are not - ignored by default. - - http://docs.python.org/dev/whatsnew/2.7.html#the-future-for-python-2-x - """ - pass - """ Manage user customizations through a rc file. @@ -192,12 +184,6 @@ def byte2str(b): return b __version__numpy__, numpy.__version__)) del version -def is_string_like(obj): - if hasattr(obj, 'shape'): return 0 - try: obj + '' - except (TypeError, ValueError): return 0 - return 1 - def _is_writable_dir(p): """ diff --git a/lib/matplotlib/cbook.py b/lib/matplotlib/cbook.py index 37ebb0d1d4b3..0616782d79a6 100644 --- a/lib/matplotlib/cbook.py +++ b/lib/matplotlib/cbook.py @@ -1,7 +1,11 @@ """ -A collection of utility functions and classes. Many (but not all) -from the Python Cookbook -- hence the name cbook +A collection of utility functions and classes. Originally, many +(but not all) were from the Python Cookbook -- hence the name cbook. + +This module is safe to import from anywhere within matplotlib; +it imports matplotlib only at runtime. """ + from __future__ import print_function import datetime @@ -18,18 +22,28 @@ import threading import time import traceback +import types import warnings from weakref import ref, WeakKeyDictionary -import matplotlib -from matplotlib import MatplotlibDeprecationWarning as mplDeprecation - import numpy as np import numpy.ma as ma -import types +class MatplotlibDeprecationWarning(UserWarning): + """ + A class for issuing deprecation warnings for Matplotlib users. + In light of the fact that Python builtin DeprecationWarnings are ignored + by default as of Python 2.7 (see link below), this class was put in to + allow for the signaling of deprecation, but via UserWarnings which are not + ignored by default. + + http://docs.python.org/dev/whatsnew/2.7.html#the-future-for-python-2-x + """ + pass + +mplDeprecation = MatplotlibDeprecationWarning # On some systems, locale.getpreferredencoding returns None, # which can break unicode; and the sage project reports that @@ -42,6 +56,8 @@ if sys.version_info[0] >= 3: def unicode_safe(s): + import matplotlib + try: preferredencoding = locale.getpreferredencoding( matplotlib.rcParams['axes.formatter.use_locale']).strip() @@ -576,6 +592,8 @@ def get_sample_data(fname, asfileobj=True): If the filename ends in .gz, the file is implicitly ungzipped. """ + import matplotlib + if matplotlib.rcParams['examples.directory']: root = matplotlib.rcParams['examples.directory'] else: