diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index 24db628bdced..aa07b81687ff 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -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:] diff --git a/lib/matplotlib/backends/backend_ps.py b/lib/matplotlib/backends/backend_ps.py index 471b7cc071ba..62edbb919fc2 100644 --- a/lib/matplotlib/backends/backend_ps.py +++ b/lib/matplotlib/backends/backend_ps.py @@ -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): diff --git a/lib/matplotlib/cbook/__init__.py b/lib/matplotlib/cbook/__init__.py index 125b0fe57ee8..f8eede700035 100644 --- a/lib/matplotlib/cbook/__init__.py +++ b/lib/matplotlib/cbook/__init__.py @@ -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'): diff --git a/lib/matplotlib/image.py b/lib/matplotlib/image.py index d3ce8f4bd15e..512f769f91c5 100644 --- a/lib/matplotlib/image.py +++ b/lib/matplotlib/image.py @@ -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