Skip to content

Commit bd0065e

Browse files
committed
Replace most uses of getfilesystemencoding by os.fs{en,de}code.
1 parent 7f0632d commit bd0065e

File tree

4 files changed

+23
-46
lines changed

4 files changed

+23
-46
lines changed

lib/matplotlib/__init__.py

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -686,13 +686,6 @@ def _get_cachedir():
686686
get_cachedir = _wrap('CACHEDIR=%s', _get_cachedir, always=False)
687687

688688

689-
def _decode_filesystem_path(path):
690-
if not isinstance(path, str):
691-
return path.decode(sys.getfilesystemencoding())
692-
else:
693-
return path
694-
695-
696689
def _get_data_path():
697690
'get the path to matplotlib data'
698691

@@ -703,35 +696,25 @@ def _get_data_path():
703696
'directory')
704697
return path
705698

706-
_file = _decode_filesystem_path(__file__)
707-
path = os.sep.join([os.path.dirname(_file), 'mpl-data'])
708-
if os.path.isdir(path):
709-
return path
710-
711-
# setuptools' namespace_packages may highjack this init file
712-
# so need to try something known to be in matplotlib, not basemap
713-
import matplotlib.afm
714-
_file = _decode_filesystem_path(matplotlib.afm.__file__)
715-
path = os.sep.join([os.path.dirname(_file), 'mpl-data'])
716-
if os.path.isdir(path):
717-
return path
718-
719-
# py2exe zips pure python, so still need special check
720-
if getattr(sys, 'frozen', None):
721-
exe_path = os.path.dirname(_decode_filesystem_path(sys.executable))
722-
path = os.path.join(exe_path, 'mpl-data')
723-
if os.path.isdir(path):
724-
return path
725-
726-
# Try again assuming we need to step up one more directory
727-
path = os.path.join(os.path.split(exe_path)[0], 'mpl-data')
728-
if os.path.isdir(path):
729-
return path
730-
731-
# Try again assuming sys.path[0] is a dir not a exe
732-
path = os.path.join(sys.path[0], 'mpl-data')
733-
if os.path.isdir(path):
734-
return path
699+
# Yield some filenames next to which we may be able to find mpl-data.
700+
def get_candidate_siblings():
701+
yield __file__
702+
# setuptools' namespace_packages may highjack this init file
703+
# so need to try something known to be in Matplotlib, not basemap.
704+
import matplotlib.afm
705+
yield matplotlib.afm.__file__
706+
# py2exe zips pure python, so still need special check.
707+
if getattr(sys, 'frozen', None):
708+
yield sys.executable
709+
# Try again assuming we need to step up one more directory.
710+
yield os.path.dirname(sys.executable)
711+
# Try again assuming sys.path[0] is a dir not a exe.
712+
yield sys.path[0]
713+
714+
for sibling in get_candidate_siblings():
715+
path = Path(sibling).with_name('mpl-data')
716+
if path.is_dir():
717+
return str(path)
735718

736719
raise RuntimeError('Could not find the matplotlib data files')
737720

lib/matplotlib/backends/backend_pdf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,7 @@ def get_char_width(charcode):
972972
# Make the charprocs array (using ttconv to generate the
973973
# actual outlines)
974974
rawcharprocs = ttconv.get_pdf_charprocs(
975-
filename.encode(sys.getfilesystemencoding()), glyph_ids)
975+
os.fsencode(filename), glyph_ids)
976976
charprocs = {}
977977
for charname in sorted(rawcharprocs):
978978
stream = rawcharprocs[charname]

lib/matplotlib/backends/backend_ps.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,10 +1128,8 @@ def print_figure_impl(fh):
11281128
"time; consider using the Cairo backend")
11291129
else:
11301130
fh.flush()
1131-
convert_ttf_to_ps(
1132-
font_filename.encode(
1133-
sys.getfilesystemencoding()),
1134-
fh, fonttype, glyph_ids)
1131+
convert_ttf_to_ps(os.fsencode(font_filename),
1132+
fh, fonttype, glyph_ids)
11351133
print("end", file=fh)
11361134
print("%%EndProlog", file=fh)
11371135

lib/matplotlib/dviread.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import re
2626
import struct
2727
import subprocess
28-
import sys
2928
import textwrap
3029

3130
import numpy as np
@@ -810,10 +809,7 @@ class PsfontsMap(object):
810809

811810
def __init__(self, filename):
812811
self._font = {}
813-
self._filename = filename
814-
if isinstance(filename, bytes):
815-
encoding = sys.getfilesystemencoding() or 'utf-8'
816-
self._filename = filename.decode(encoding, errors='replace')
812+
self._filename = os.fsdecode(filename)
817813
with open(filename, 'rb') as file:
818814
self._parse(file)
819815

0 commit comments

Comments
 (0)