8000 Add PEP 519 support · matplotlib/matplotlib@6563aa3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6563aa3

Browse files
committed
Add PEP 519 support
1 parent 747b1a7 commit 6563aa3

File tree

5 files changed

+63
-11
lines changed

5 files changed

+63
-11
lines changed

lib/matplotlib/animation.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
import contextlib
3838
import tempfile
3939
import warnings
40-
from matplotlib.cbook import iterable, is_string_like
40+
from matplotlib.cbook import iterable, is_string_like, fspath_no_except
4141
from matplotlib.compat import subprocess
4242
from matplotlib import verbose
4343
from matplotlib import rcParams, rcParamsDefault, rc_context
@@ -281,6 +281,7 @@ def _run(self):
281281
output = sys.stdout
282282
else:
283283
output = subprocess.PIPE
284+
command = [fspath_no_except(cmd) for cmd in command]
284285
verbose.report('MovieWriter.run: running command: %s' %
285286
' '.join(command))
286287
self._proc = subprocess.Popen(command, shell=False,

lib/matplotlib/backends/backend_agg.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
from matplotlib import verbose, rcParams
3131
from matplotlib.backend_bases import (RendererBase, FigureManagerBase,
3232
FigureCanvasBase)
33-
from matplotlib.cbook import is_string_like, maxdict, restrict_dict
33+
from matplotlib.cbook import (is_string_like, maxdict, restrict_dict,
34+
fspath_no_except)
3435
from matplotlib.figure import Figure
3536
from matplotlib.font_manager import findfont, get_font
3637
from matplotlib.ft2font import (LOAD_FORCE_AUTOHINT, LOAD_NO_HINTING,
@@ -525,7 +526,10 @@ def print_png(self, filename_or_obj, *args, **kwargs):
525526
close = False
526527

527528
try:
528-
_png.write_png(renderer< 8000 /span>._renderer, filename_or_obj, self.figure.dpi)
529+
_png.write_png(
530+
renderer._renderer, fspath_no_except(filename_or_obj),
531+
self.figure.dpi
532+
)
529533
finally:
530534
if close:
531535
filename_or_obj.close()
@@ -578,7 +582,8 @@ def print_jpg(self, filename_or_obj, *args, **kwargs):
578582
if 'quality' not in options:
579583
options['quality'] = rcParams['savefig.jpeg_quality']
580584

581-
return background.save(filename_or_obj, format='jpeg', **options)
585+
return background.save(to_filehandle(filename_or_obj), format='jpeg',
586+
**options)
582587
print_jpeg = print_jpg
583588

584589
# add TIFF support
@@ -588,7 +593,7 @@ def print_tif(self, filename_or_obj, *args, **kwargs):
588593
return
589594
image = Image.frombuffer('RGBA', size, buf, 'raw', 'RGBA', 0, 1)
590595
dpi = (self.figure.dpi, self.figure.dpi)
591-
return image.save(filename_or_obj, format='tiff',
596+
return image.save(to_filehandle(filename_or_obj), format='tiff',
592597
dpi=dpi)
593598
print_tiff = print_tif
594599

lib/matplotlib/backends/backend_pdf.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
FigureManagerBase, FigureCanvasBase)
3535
from matplotlib.backends.backend_mixed import MixedModeRenderer
3636
from matplotlib.cbook import (Bunch, is_string_like, get_realpath_and_stat,
37-
is_writable_file_like, maxdict)
37+
is_writable_file_like, maxdict, fspath_no_except)
3838
from matplotlib.figure import Figure
3939
from matplotlib.font_manager import findfont, is_opentype_cff_font, get_font
4040
from matplotlib.afm import AFM
@@ -429,6 +429,7 @@ def __init__(self, filename):
429429
self.passed_in_file_object = False
430430
self.original_file_like = None
431431
self.tell_base = 0
432+
filename = fspath_no_except(filename)
432433
if is_string_like(filename):
433434
fh = open(filename, 'wb')
434435
elif is_writable_file_like(filename):

lib/matplotlib/cbook.py

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,7 @@ def to_filehandle(fname, flag='rU', return_opened=False):
756756
files is automatic, if the filename ends in .gz. *flag* is a
757757
read/write flag for :func:`file`
758758
"""
759+
fname = fspath_no_except(fname)
759760
if is_string_like(fname):
760761
if fname.endswith('.gz'):
761762
# get rid of 'U' in flag for gzipped files.
@@ -815,6 +816,7 @@ def get_sample_data(fname, asfileobj=True):
815816
root = matplotlib.rcParams['examples.directory']
816817
else:
817818
root = os.path.join(matplotlib._get_data_path(), 'sample_data')
819+
fname = fspath_no_except(fname)
818820
path = os.path.join(root, fname)
819821

820822
if asfileobj:
@@ -1019,6 +1021,7 @@ def __init__(self):
10191021
self._cache = {}
10201022

10211023
def __call__(self, path):
1024+
path = fspath_no_except(path)
10221025
result = self._cache.get(path)
10231026
if result is None:
10241027
realpath = os.path.realpath(path)
@@ -1173,7 +1176,7 @@ def listFiles(root, patterns='*', recurse=1, return_folders=0):
11731176
pattern_list = patterns.split(';')
11741177
results = []
11751178

1176-
for dirname, dirs, files in os.walk(root):
1179+
for dirname, dirs, files in os.walk(fspath_no_except(root)):
11771180
# Append to results all relevant files (and perhaps folders)
11781181
for name in files:
11791182
fullname = os.path.normpath(os.path.join(dirname, name))
@@ -1197,10 +1200,10 @@ def get_recursive_filelist(args):
11971200
files = []
11981201

11991202
for arg in args:
1200-
if os.path.isfile(arg):
1203+
if os.path.isfile(fspath_no_except(arg)):
12011204
files.append(arg)
12021205
continue
1203-
if os.path.isdir(arg):
1206+
if os.path.isdir(fspath_no_except(arg)):
12041207
newfiles = listFiles(arg, recurse=1, return_folders=1)
12051208
files.extend(newfiles)
12061209

@@ -1680,6 +1683,7 @@ def simple_linear_interpolation(a, steps):
16801683

16811684

16821685
def recursive_remove(path):
1686+
path = fspath_no_except(path)
16831687
if os.path.isdir(path):
16841688
for fname in (glob.glob(os.path.join(path, '*')) +
16851689
glob.glob(os.path.join(path, '.*'))):
@@ -2599,7 +2603,7 @@ class Locked(object):
25992603
LOCKFN = '.matplotlib_lock'
26002604

26012605
def __init__(self, path):
2602-
self.path = path
2606+
self.path = fspath_no_except(path)
26032607
self.end = "-" + str(os.getpid())
26042608
self.lock_path = os.path.join(self.path, self.LOCKFN + self.end)
26052609
self.pattern = os.path.join(self.path, self.LOCKFN + '-*')
@@ -2635,3 +2639,44 @@ def __exit__(self, exc_type, exc_value, traceback):
26352639
os.rmdir(path)
26362640
except OSError:
26372641
pass
2642+
2643+
2644+
try:
2645+
from os import fspath
2646+
except ImportError:
2647+
def fspath(path):
2648+
"""
2649+
Return the string representation of the path.
2650+
If str or bytes is passed in, it is returned unchanged.
2651+
This code comes from PEP 519, modified to support earlier versions of
2652+
python.
2653+
2654+
This is required for python < 3.6.
2655+
"""
2656+
if isinstance(path, (six.text_type, six.binary_type)):
2657+
return path
2658+
2659+
# Work from the object's type to match method resolution of other magic
2660+
# methods.
2661+
path_type = type(path)
2662+
try:
2663+
return path_type.__fspath__(path)
2664+
except AttributeError:
2665+
if hasattr(path_type, '__fspath__'):
2666+
raise
2667+
try:
2668+
import pathlib
2669+
except ImportError:
2670+
pass
2671+
else:
2672+
if isinstance(path, pathlib.PurePath):
2673+
return six.text_type(path)
2674+
2675+
raise TypeError("expected str, bytes or os.PathLike object, not "
2676+
+ path_type.__name__)
2677+
2678+
def fspath_no_except(path):
2679+
try:
2680+
return fspath(path)
2681+
except TypeError:
2682+
return path

lib/matplotlib/image.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ def contains(self, mouseevent):
525525
def write_png(self, fname):
526526
"""Write the image to png file with fname"""
527527
im = self.to_rgba(self._A, bytes=True, norm=False)
528-
_png.write_png(im, fname)
528+
_png.write_png(im, cbook.fspath_no_except(fname))
529529

530530
def set_data(self, A):
531531
"""

0 commit comments

Comments
 (0)
0