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

Skip to content

Commit ba7c407

Browse files
committed
Fix passing Path to ps backend when text.usetex rc is True.
... and misc. cleanups to test_backend_ps (more pathlib + remove an unnecessary rc_context() as tests always reset the rcs at exit).
1 parent a7a3f2a commit ba7c407

2 files changed

Lines changed: 16 additions & 19 deletions

File tree

lib/matplotlib/backends/backend_ps.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,12 +1119,14 @@ def _print_figure_tex(
11191119
the key 'Creator' is used.
11201120
"""
11211121
isEPSF = format == 'eps'
1122-
if isinstance(outfile, str):
1123-
title = outfile
1124-
elif is_writable_file_like(outfile):
1122+
if is_writable_file_like(outfile):
11251123
title = None
11261124
else:
1127-
raise ValueError("outfile must be a path or a file-like object")
1125+
try:
1126+
title = os.fspath(outfile)
1127+
except TypeError:
1128+
raise ValueError(
1129+
"outfile must be a path or a file-like object")
11281130

11291131
self.figure.dpi = 72 # ignore the dpi kwarg
11301132
width, height = self.figure.get_size_inches()

lib/matplotlib/tests/test_backend_ps.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,12 @@ def test_savefig_to_stringio(format, use_log, rcParams):
7171

7272

7373
def test_patheffects():
74-
with mpl.rc_context():
75-
mpl.rcParams['path.effects'] = [
76-
patheffects.withStroke(linewidth=4, foreground='w')]
77-
fig, ax = plt.subplots()
78-
ax.plot([1, 2, 3])
79-
with io.BytesIO() as ps:
80-
fig.savefig(ps, format='ps')
74+
mpl.rcParams['path.effects'] = [
75+
patheffects.withStroke(linewidth=4, foreground='w')]
76+
fig, ax = plt.subplots()
77+
ax.plot([1, 2, 3])
78+
with io.BytesIO() as ps:
79+
fig.savefig(ps, format='ps')
8180

8281

8382
@needs_usetex
@@ -86,18 +85,17 @@ def test_tilde_in_tempfilename(tmpdir):
8685
# Tilde ~ in the tempdir path (e.g. TMPDIR, TMP or TEMP on windows
8786
# when the username is very long and windows uses a short name) breaks
8887
# latex before https://github.com/matplotlib/matplotlib/pull/5928
89-
base_tempdir = Path(str(tmpdir), "short-1")
88+
base_tempdir = Path(tmpdir, "short-1")
9089
base_tempdir.mkdir()
9190
# Change the path for new tempdirs, which is used internally by the ps
9291
# backend to write a file.
9392
with cbook._setattr_cm(tempfile, tempdir=str(base_tempdir)):
9493
# usetex results in the latex call, which does not like the ~
95-
plt.rc('text', usetex=True)
94+
mpl.rcParams['text.usetex'] = True
9695
plt.plot([1, 2, 3, 4])
9796
plt.xlabel(r'\textbf{time} (s)')
98-
output_eps = os.path.join(str(base_tempdir), 'tex_demo.eps')
9997
# use the PS backend to write the file...
100-
plt.savefig(output_eps, format="ps")
98+
plt.savefig(base_tempdir / 'tex_demo.eps', format="ps")
10199

102100

103101
def test_source_date_epoch():
@@ -133,11 +131,8 @@ def test_transparency():
133131
@needs_usetex
134132
def test_failing_latex(tmpdir):
135133
"""Test failing latex subprocess call"""
136-
path = str(tmpdir.join("tmpoutput.ps"))
137-
138134
mpl.rcParams['text.usetex'] = True
139-
140135
# This fails with "Double subscript"
141136
plt.xlabel("$22_2_2$")
142137
with pytest.raises(RuntimeError):
143-
plt.savefig(path)
138+
plt.savefig(Path(tmpdir, "tmpoutput.ps"))

0 commit comments

Comments
 (0)