8000 os.PathLike exists on all supported Pythons now. by anntzer · Pull Request #12935 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

os.PathLike exists on all supported Pythons now. #12935

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -1957,7 +1957,7 @@ def print_figure(self, filename, dpi=None, facecolor=None, edgecolor=None,
"""
if format is None:
# get format from filename, or from backend's default filetype
if isinstance(filename, getattr(os, "PathLike", ())):
if isinstance(filename, os.PathLike):
filename = os.fspath(filename)
if isinstance(filename, str):
format = os.path.splitext(filename)[1][1:]
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/backends/backend_ps.py
Original file line number Diff line number Diff line change
Expand Up @@ -968,8 +968,8 @@ def _print_figure(
the key 'Creator' is used.
"""
isEPSF = format == 'eps'
if isinstance(outfile, (str, getattr(os, "PathLike", ()),)):
outfile = title = getattr(os, "fspath", lambda obj: obj)(outfile)
if isinstance(outfile, (str, os.PathLike)):
outfile = title = os.fspath(outfile)
title = title.encode("ascii", "replace").decode("ascii")
passed_in_file_object = False
elif is_writable_file_like(outfile):
Expand Down
33 changes: 28 additions & 5 deletions lib/matplotlib/cbook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,13 +367,36 @@ def is_numlike(obj):
return isinstance(obj, (numbers.Number, np.number))


def to_filehandle(fname, flag='rU', return_opened=False, encoding=None):
def to_filehandle(fname, flag='r', return_opened=False, encoding=None):
"""
*fname* can be an `os.PathLike` or a file handle. Support for gzipped
files is automatic, if the filename ends in .gz. *flag* is a
read/write flag for :func:`file`
Convert a path to an open file handle or pass-through a file-like object.

Consider using `open_file_cm` instead, as it allows one to properly close
newly created file objects more easily.

Parameters
----------
fname : str or PathLike or file-like object
If `str` or `os.PathLike`, the file is opened using the flags specified
by *flag* and *encoding*. If a file-like object, it is passed through.
flag : str, default 'r'
Passed as the *mode* argument to `open` when *fname* is `str` or
`os.PathLike`; ignored if *fname* is file-like.
return_opened : bool, default False
If True, return both the file object and a boolean indicating whether
this was a new file (that the caller needs to close). If False, return
only the new file.
encoding : str or None, default None
Passed as the *mode* argument to `open` when *fname* is `str` or
`os.PathLike`; ignored if *fname* is file-like.

Returns
-------
fh : file-like
opened : bool
*opened* is only returned if *return_opened* is True.
"""
if isinstance(fname, getattr(os, "PathLike", ())):
if isinstance(fname, os.PathLike):
fname = os.fspath(fname)
if isinstance(fname, str):
if fname.endswith('.gz'):
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -1425,7 +1425,7 @@ def imsave(fname, arr, vmin=None, vmax=None, cmap=None, format=None,
"""
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
if isinstance(fname, getattr(os, "PathLike", ())):
if isinstance(fname, os.PathLike):
fname = os.fspath(fname)
if (format == 'png'
or (format is None
Expand Down
0