8000 Fix image fmt detection for Path input. · matplotlib/matplotlib@19d4e8c · GitHub
[go: up one dir, main page]

Skip to content

Commit 19d4e8c

Browse files
committed
Fix image fmt detection for Path input.
1 parent aadd0e0 commit 19d4e8c

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2130,6 +2130,8 @@ def print_figure(self, filename, dpi=None, facecolor=None, edgecolor=None,
21302130

21312131
if format is None:
21322132
# get format from filename, or from backend's default filetype
2133+
if isinstance(filename, getattr(os, "PathLike", ())):
2134+
filename = os.fspath(filename)
21332135
if isinstance(filename, six.string_types):
21342136
format = os.path.splitext(filename)[1][1:]
21352137
if format is None or format == '':

lib/matplotlib/tests/test_figure.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
from __future__ import absolute_import, division, print_function
2-
3-
import os
1+
from pathlib import Path
42
import sys
53
import warnings
64

@@ -379,6 +377,10 @@ def test_figure_repr():
379377

380378
@pytest.mark.skipif(sys.version_info < (3, 6), reason="requires Python 3.6+")
381379
@pytest.mark.parametrize("fmt", ["png", "pdf", "ps", "eps", "svg"])
382-
def test_fspath(fmt):
383-
from pathlib import Path
384-
plt.savefig(Path(os.devnull), format=fmt)
380+
def test_fspath(fmt, tmpdir):
381+
out = Path(tmpdir, "test.{}".format(fmt))
382+
plt.savefig(out)
383+
with out.open("rb") as file:
384+
# All the supported formats include the format name (case-insensitive)
385+
# in the first 100 bytes.
386+
assert fmt.encode("ascii") in file.read(100).lower()

0 commit comments

Comments
 (0)
0