8000 Make cbook safe to import while removing duplicate is_string_like; · matplotlib/matplotlib@f58da0d · GitHub
[go: up one dir, main page]

Skip to content

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f58da0d

Browse files
committed
Make cbook safe to import while removing duplicate is_string_like;
closes #1019, replaces #1662.
1 parent dfd0345 commit f58da0d

File tree

2 files changed

+32
-24
lines changed

2 files changed

+32
-24
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: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
"""
22
A collection of utility functions and classes. Many (but not all)
3-
from the Python Cookbook -- hence the name cbook
3+
from the Python Cookbook -- hence the name cbook.
4+
5+
This module is in transition, with the intention of migrating
6+
functionality used in matplotlib to a new module, possibly called
7+
"utils". Functionality not needed in matplotlib might then be
8+
deprecated and eventually removed.
9+
10+
This module is safe to import from anywhere within matplotlib;
11+
it imports matplotlib only at runtime.
412
"""
513
from __future__ import print_function
614

@@ -18,18 +26,28 @@
1826
import threading
1927
import time
2028
import traceback
29+
import types
2130
import warnings
2231
from weakref import ref, WeakKeyDictionary
2332

24-
import matplotlib
25-
from matplotlib import MatplotlibDeprecationWarning as mplDeprecation
26-
2733
import numpy as np
2834
import numpy.ma as ma
2935

3036

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

3452
# On some systems, locale.getpreferredencoding returns None,
3553
# which can break unicode; and the sage project reports that
@@ -42,6 +60,8 @@
4260

4361
if sys.version_info[0] >= 3:
4462
def unicode_safe(s):
63+
import matplotlib
64+
4565
try:
4666
preferredencoding = locale.getpreferredencoding(
4767
matplotlib.rcParams['axes.formatter.use_locale']).strip()
@@ -576,6 +596,8 @@ def get_sample_data(fname, asfileobj=True):
576596
577597
If the filename ends in .gz, the file is implicitly ungzipped.
578598
"""
599+
import matplotlib
600+
579601
if matplotlib.rcParams['examples.directory']:
580602
root = matplotlib.rcParams['examples.directory']
581603
else:

0 commit comments

Comments
 (0)
0