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

Skip to content

Commit 99d8900

Browse files
authored
Merge pull request #12935 from anntzer/pathlike
os.PathLike exists on all supported Pythons now.
2 parents 94ae1e0 + 18f4df3 commit 99d8900

File tree

4 files changed

+32
-9
lines changed

4 files changed

+32
-9
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1957,7 +1957,7 @@ def print_figure(self, filename, dpi=None, facecolor=None, edgecolor=None,
19571957
"""
19581958
if format is None:
19591959
# get format from filename, or from backend's default filetype
1960-
if isinstance(filename, getattr(os, "PathLike", ())):
1960+
if isinstance(filename, os.PathLike):
19611961
filename = os.fspath(filename)
19621962
if isinstance(filename, str):
19631963
format = os.path.splitext(filename)[1][1:]

lib/matplotlib/backends/backend_ps.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -968,8 +968,8 @@ def _print_figure(
968968
the key 'Creator' is used.
969969
"""
970970
isEPSF = format == 'eps'
971-
if isinstance(outfile, (str, getattr(os, "PathLike", ()),)):
972-
outfile = title = getattr(os, "fspath", lambda obj: obj)(outfile)
971+
if isinstance(outfile, (str, os.PathLike)):
972+
outfile = title = os.fspath(outfile)
973973
title = title.encode("ascii", "replace").decode("ascii")
974974
passed_in_file_object = False
975975
elif is_writable_file_like(outfile):

lib/matplotlib/cbook/__init__.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -367,13 +367,36 @@ def is_numlike(obj):
367367
return isinstance(obj, (numbers.Number, np.number))
368368

369369

370-
def to_filehandle(fname, flag='rU', return_opened=False, encoding=None):
370+
def to_filehandle(fname, flag='r', return_opened=False, encoding=None):
371371
"""
372-
*fname* can be an `os.PathLike` or a file handle. Support for gzipped
373-
files is automatic, if the filename ends in .gz. *flag* is a
374-
read/write flag for :func:`file`
372+
Convert a path to an open file handle or pass-through a file-like object.
373+
374+
Consider using `open_file_cm` instead, as it allows one to properly close
375+
newly created file objects more easily.
376+
377+
Parameters
378+
----------
379+
fname : str or PathLike or file-like object
380+
If `str` or `os.PathLike`, the file is opened using the flags specified
381+
by *flag* and *encoding*. If a file-like object, it is passed through.
382+
flag : str, default 'r'
383+
Passed as the *mode* argument to `open` when *fname* is `str` or
384+
`os.PathLike`; ignored if *fname* is file-like.
385+
return_opened : bool, default False
386+
If True, return both the file object and a boolean indicating whether
387+
this was a new file (that the caller needs to close). If False, return
388+
only the new file.
389+
encoding : str or None, default None
390+
Passed as the *mode* argument to `open` when *fname* is `str` or
391+
`os.PathLike`; ignored if *fname* is file-like.
392+
393+
Returns
394+
-------
395+
fh : file-like
396+
opened : bool
397+
*opened* is only returned if *return_opened* is True.
375398
"""
376-
if isinstance(fname, getattr(os, "PathLike", ())):
399+
if isinstance(fname, os.PathLike):
377400
fname = os.fspath(fname)
378401
if isinstance(fname, str):
379402
if fname.endswith('.gz'):

lib/matplotlib/image.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1425,7 +1425,7 @@ def imsave(fname, arr, vmin=None, vmax=None, cmap=None, format=None,
14251425
"""
14261426
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
14271427
from matplotlib.figure import Figure
1428-
if isinstance(fname, getattr(os, "PathLike", ())):
1428+
if isinstance(fname, os.PathLike):
14291429
fname = os.fspath(fname)
14301430
if (format == 'png'
14311431
or (format is None

0 commit comments

Comments
 (0)