Skip to content

Commit

Permalink
Merge pull request #1762 from efiring/cbook_safe_import
Browse files Browse the repository at this point in the history
Make cbook safe to import while removing duplicate is_string_like;
  • Loading branch information
efiring committed Feb 19, 2013
2 parents 0ef37a2 + fb56db1 commit 433da1a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 25 deletions.
24 changes: 5 additions & 19 deletions lib/matplotlib/__init__.py
Expand Up @@ -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:
Expand All @@ -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.
Expand Down Expand Up @@ -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):
"""
Expand Down
30 changes: 24 additions & 6 deletions 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
Expand All @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 433da1a

Please sign in to comment.