Skip to content

Commit 433da1a

Browse files
committed
Merge pull request matplotlib#1762 from efiring/cbook_safe_import
Make cbook safe to import while removing duplicate is_string_like;
2 parents 0ef37a2 + fb56db1 commit 433da1a

File tree

2 files changed

+29
-25
lines changed

2 files changed

+29
-25
lines changed

lib/matplotlib/__init__.py

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@
106106
import distutils.sysconfig
107107
import distutils.version
108108

109+
# cbook must import matplotlib only within function
110+
# definitions, so it is safe to import from it here.
111+
from matplotlib.cbook import MatplotlibDeprecationWarning
112+
from matplotlib.cbook import is_string_like
113+
109114
try:
110115
reload
111116
except NameError:
@@ -123,19 +128,6 @@
123128
sys.argv = ['modpython']
124129

125130

126-
class MatplotlibDeprecationWarning(UserWarning):
127-
"""
128-
A class for issuing deprecation warnings for Matplotlib users.
129-
130-
In light of the fact that Python builtin DeprecationWarnings are ignored
131-
by default as of Python 2.7 (see link below), this class was put in to
132-
allow for the signaling of deprecation, but via UserWarnings which are not
133-
ignored by default.
134-
135-
http://docs.python.org/dev/whatsnew/2.7.html#the-future-for-python-2-x
136-
"""
137-
pass
138-
139131
"""
140132
Manage user customizations through a rc file.
141133
@@ -192,12 +184,6 @@ def byte2str(b): return b
192184
__version__numpy__, numpy.__version__))
193185
del version
194186

195-
def is_string_like(obj):
196-
if hasattr(obj, 'shape'): return 0
197-
try: obj + ''
198-
except (TypeError, ValueError): return 0
199-
return 1
200-
201187

202188
def _is_writable_dir(p):
203189
"""

lib/matplotlib/cbook.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
"""
2-
A collection of utility functions and classes. Many (but not all)
3-
from the Python Cookbook -- hence the name cbook
2+
A collection of utility functions and classes. Originally, many
3+
(but not all) were from the Python Cookbook -- hence the name cbook.
4+
5+
This module is safe to import from anywhere within matplotlib;
6+
it imports matplotlib only at runtime.
47
"""
8+
59
from __future__ import print_function
610

711
import datetime
@@ -18,18 +22,28 @@
1822
import threading
1923
import time
2024
import traceback
25+
import types
2126
import warnings
2227
from weakref import ref, WeakKeyDictionary
2328

24-
import matplotlib
25-
from matplotlib import MatplotlibDeprecationWarning as mplDeprecation
26-
2729
import numpy as np
2830
import numpy.ma as ma
2931

3032

31-
import types
33+
class MatplotlibDeprecationWarning(UserWarning):
34+
"""
35+
A class for issuing deprecation warnings for Matplotlib users.
3236
37+
In light of the fact that Python builtin DeprecationWarnings are ignored
38+
by default as of Python 2.7 (see link below), this class was put in to
39+
allow for the signaling of deprecation, but via UserWarnings which are not
40+
ignored by default.
41+
42+
http://docs.python.org/dev/whatsnew/2.7.html#the-future-for-python-2-x
43+
"""
44+
pass
45+
46+
mplDeprecation = MatplotlibDeprecationWarning
3347

3448
# On some systems, locale.getpreferredencoding returns None,
3549
# which can break unicode; and the sage project reports that
@@ -42,6 +56,8 @@
4256

4357
if sys.version_info[0] >= 3:
4458
def unicode_safe(s):
59+
import matplotlib
60+
4561
try:
4662
preferredencoding = locale.getpreferredencoding(
4763
matplotlib.rcParams['axes.formatter.use_locale']).strip()
@@ -576,6 +592,8 @@ def get_sample_data(fname, asfileobj=True):
576592
577593
If the filename ends in .gz, the file is implicitly ungzipped.
578594
"""
595+
import matplotlib
596+
579597
if matplotlib.rcParams['examples.directory']:
580598
root = matplotlib.rcParams['examples.directory']
581599
else:

0 commit comments

Comments
 (0)