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

Skip to content

Commit ce2b1fd

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

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
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 & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
from __future__ import absolute_import, division, print_function
2-
31
import os
2+
from pathlib import Path
43
import sys
54
import warnings
65

@@ -379,6 +378,10 @@ def test_figure_repr():
379378

380379
@pytest.mark.skipif(sys.version_info < (3, 6), reason="requires Python 3.6+")
381380
@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)
381+
def test_fspath(fmt, tmpdir):
382+
out = Path(tmpdir, "test.{}".format(fmt))
383+
plt.savefig(out)
384+
with out.open("rb") as file:
385+
# All the supported formats include the format name (case-insensitive)
386+
# in the first 100 bytes.
387+
assert fmt.encode("ascii") in file.read(100).lower()

0 commit comments

Comments
 (0)
0