8000 Replace most uses of getfilesystemencoding by os.fs{en,de}code. · matplotlib/matplotlib@617b002 · GitHub
[go: up one dir, main page]

Skip to content

Commit 617b002

Browse files
committed
Replace most uses of getfilesystemencoding by os.fs{en,de}code.
1 parent b5391ce commit 617b002

File tree

6 files changed

+28
-70
lines changed

6 files changed

+28
-70
lines changed

lib/matplotlib/__init__.py

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -562,10 +562,7 @@ def _get_home():
562562
:see:
563563
http://mail.python.org/pipermail/python-list/2005-February/325395.html
564564
"""
565-
if six.PY2 and sys.platform == 'win32':
566-
path = os.path.expanduser(b"~").decode(sys.getfilesystemencoding())
567-
else:
568-
path = os.path.expanduser("~")
565+
path = os.path.expanduser("~")
569566
if os.path.isdir(path):
570567
return path
571568
for evar in ('HOME', 'USERPROFILE', 'TMP'):
@@ -686,13 +683,6 @@ def _get_cachedir():
686683
get_cachedir = _wrap('CACHEDIR=%s', _get_cachedir, always=False)
687684

688685

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-
696686
def _get_data_path():
697687
'get the path to matplotlib data'
698688

@@ -703,35 +693,25 @@ def _get_data_path():
703693
'directory')
704694
return path
705695

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
696+
# Yield some filenames next to which we may be able to find mpl-data.
697+
def get_candidate_siblings():
698+
yield __file__
699+
# setuptools' namespace_packages may highjack this init file
700+
# so need to try something known to be in Matplotlib, not basemap.
701+
import matplotlib.afm
702+
yield matplotlib.afm.__file__
703+
# py2exe zips pure python, so still need special check.
704+
if getattr(sys, 'frozen', None):
705+
yield sys.executable
706+
# Try again assuming we need to step up one more directory.
707+
yield os.path.dirname(sys.executable)
708+
# Try again assuming sys.path[0] is a dir not a exe.
709+
yield sys.path[0]
710+
711+
for sibling in get_candidate_siblings():
712+
path = Path(sibling).with_name('mpl-data')
713+
if path.is_dir():
714+
return str(path)
735715

736716
raise RuntimeError('Could not find the matplotlib data files')
737717

lib/matplotlib/backends/backend_pdf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -974,7 +974,7 @@ def get_char_width(charcode):
974974
# Make the charprocs array (using ttconv to generate the
975975
# actual outlines)
976976
rawcharprocs = ttconv.get_pdf_charprocs(
977-
filename.encode(sys.getfilesystemencoding()), glyph_ids)
977+
os.fsencode(filename), glyph_ids)
978978
charprocs = {}
979979
for charname in sorted(rawcharprocs):
980980
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):
F438 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

lib/matplotlib/font_manager.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -279,14 +279,7 @@ def _call_fc_list():
279279
return []
280280
finally:
281281
timer.cancel()
282-
fnames = []
283-
for fname in out.split(b'\n'):
284-
try:
285-
fname = six.text_type(fname, sys.getfilesystemencoding())
286-
except UnicodeDecodeError:
287-
continue
288-
fnames.append(fname)
289-
return fnames
282+
return [os.fsdecode(fname) for fname in out.split(b'\n')]
290283

291284

292285
def get_fontconfig_fonts(fontext='ttf'):
@@ -1371,11 +1364,7 @@ def fc_match(pattern, fontext):
13711364
# result in bytes and parse it as bytes, until we extract the
13721365
# filename, which is in sys.filesystemencoding().
13731366
if pipe.returncode == 0:
1374-
for fname in output.split(b'\n'):
1375-
try:
1376-
fname = six.text_type(fname, sys.getfilesystemencoding())
1377-
except UnicodeDecodeError:
1378-
continue
1367+
for fname in map(os.fsdecode, output.split(b'\n')):
13791368
if os.path.splitext(fname)[1][1:] in fontexts:
13801369
return fname
13811370
return None

lib/matplotlib/testing/compare.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -198,15 +198,10 @@ def __call__(self, orig, dest):
198198
if not self._read_to_prompt():
199199
raise OSError("Failed to start Inkscape")
200200

201-
try:
202-
fsencode = os.fsencode
203-
except AttributeError: # Py2.
204-
def fsencode(s):
205-
return s.encode(sys.getfilesystemencoding())
206-
207201
# Inkscape uses glib's `g_shell_parse_argv`, which has a consistent
208202
# behavior across platforms, so we can just use `shlex.quote`.
209-
orig_b, dest_b = map(_shlex_quote_bytes, map(fsencode, [orig, dest]))
203+
orig_b, dest_b = map(_shlex_quote_bytes,
204+
map(os.fsencode, [orig, dest]))
210205
if b"\n" in orig_b or b"\n" in dest_b:
211206
# Who knows whether the current folder name has a newline, or if
212207
# our encoding is even ASCII compatible... Just fall back on the

0 commit comments

Comments
 (0)
0