Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7c12fa0

Browse files
committed
Change to using a helper function and isinstance(path, bytes)
Change to using a helper function and isinstance(path, bytes) to decode __file__ based on file system encoding.
1 parent 9c5cf1b commit 7c12fa0

File tree

1 file changed

+9
-14
lines changed

1 file changed

+9
-14
lines changed

lib/matplotlib/__init__.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,11 @@ def _get_cachedir():
652652

653653
get_cachedir = verbose.wrap('CACHEDIR=%s', _get_cachedir, always=False)
654654

655+
def _decode_filesystem_path(path):
656+
if isinstance(path, bytes):
657+
return path.decode(sys.getfilesystemencoding())
658+
else:
659+
return path
655660

656661
def _get_data_path():
657662
'get the path to matplotlib data'
@@ -662,33 +667,23 @@ def _get_data_path():
662667
raise RuntimeError('Path in environment MATPLOTLIBDATA not a '
663668
'directory')
664669
return path
665-
666-
if hasattr(__file__, 'decode'):
667-
_file = __file__.decode(sys.getfilesystemencoding())
668-
else:
669-
_file = __file__
670+
671+
_file=_decode_filesystem_path(__file__)
670672
path = os.sep.join([os.path.dirname(_file), 'mpl-data'])
671673
if os.path.isdir(path):
672674
return path
673675

674676
# setuptools' namespace_packages may highjack this init file
675677
# so need to try something known to be in matplotlib, not basemap
676678
import matplotlib.afm
677-
if hasattr(matplotlib.afm.__file__, 'decode'):
678-
_file = matplotlib.afm.__file__.decode(sys.getfilesystemencoding())
679-
else:
680-
_file = matplotlib.afm.__file__
679+
_file=_decode_filesystem_path(matplotlib.afm.__file__)
681680
path = os.sep.join([os.path.dirname(_file), 'mpl-data'])
682681
if os.path.isdir(path):
683682
return path
684683

685684
# py2exe zips pure python, so still need special check
686685
if getattr(sys, 'frozen', None):
687-
if hasattr(sys.executable, 'decode'):
688-
_file = sys.executable.decode(sys.getfilesystemencoding())
689-
else:
690-
_file = sys.executable
691-
exe_path = os.path.dirname(_file)
686+
exe_path = os.path.dirname(_decode_filesystem_path(sys.executable))
692687
path = os.path.join(exe_path, 'mpl-data')
693688
if os.path.isdir(path):
694689
return path

0 commit comments

Comments
 (0)