8000 Also use TemporaryDirectory instead of mkstemp in backend_ps. · matplotlib/matplotlib@33a2624 · GitHub
[go: up one dir, main page]

Skip to content

Commit 33a2624

Browse files
committed
Also use TemporaryDirectory instead of mkstemp in backend_ps.
1 parent f434d6d commit 33a2624

File tree

2 files changed

+8
-15
lines changed

2 files changed

+8
-15
lines changed

lib/matplotlib/backends/backend_ps.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import shutil
1414
import subprocess
1515
import sys
16-
from tempfile import mkstemp
16+
from tempfile import TemporaryDirectory
1717
import time
1818

1919
import numpy as np
@@ -1150,19 +1150,15 @@ def print_figure_impl(fh):
11501150
if rcParams['ps.usedistiller']:
11511151
# We are going to use an external program to process the output.
11521152
# Write to a temporary file.
1153-
fd, tmpfile = mkstemp()
1154-
try:
1155-
with open(fd, 'w', encoding='latin-1') as fh:
1153+
with TemporaryDirectory() as tmpdir:
1154+
tmpfile = os.path.join(tmpdir, "tmp.ps")
1155+
with open(tmpfile, 'w', encoding='latin-1') as fh:
11561156
print_figure_impl(fh)
11571157
if rcParams['ps.usedistiller'] == 'ghostscript':
11581158
gs_distill(tmpfile, isEPSF, ptype=papertype, bbox=bbox)
11591159
elif rcParams['ps.usedistiller'] == 'xpdf':
11601160
xpdf_distill(tmpfile, isEPSF, ptype=papertype, bbox=bbox)
1161-
11621161
_move_path_to_path_or_stream(tmpfile, outfile)
1163-
finally:
1164-
if os.path.isfile(tmpfile):
1165-
os.unlink(tmpfile)
11661162

11671163
else:
11681164
# Write directly to outfile.
@@ -1255,9 +1251,9 @@ def write(self, *kl, **kwargs):
12551251

12561252
# write to a temp file, we'll move it to outfile when done
12571253

1258-
fd, tmpfile = mkstemp()
1259-
try:
1260-
with open(fd, 'w', encoding='latin-1') as fh:
1254+
with TemporaryDirectory() as tmpdir:
1255+
tmpfile = os.path.join(tmpdir, "tmp.ps")
1256+
with open(tmpfile, 'w', encoding='latin-1') as fh:
12611257
# write the Encapsulated PostScript headers
12621258
print("%!PS-Adobe-3.0 EPSF-3.0", file=fh)
12631259
if title:
@@ -1344,9 +1340,6 @@ def write(self, *kl, **kwargs):
13441340
rotated=psfrag_rotated)
13451341

13461342
_move_path_to_path_or_stream(tmpfile, outfile)
1347-
finally:
1348-
if os.path.isfile(tmpfile):
1349-
os.unlink(tmpfile)
13501343

13511344

13521345
def convert_psfrags(tmpfile, psfrags, font_preamble, custom_preamble,

lib/matplotlib/tests/test_style.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def temp_style(style_name, settings=None):
2929
with TemporaryDirectory() as tmpdir:
3030
# Write style settings to file in the tmpdir.
3131
Path(tmpdir, temp_file).write_text(
32-
"\n".join(map("{0[0]}: {0[1]}".format, settings.items())))
32+
"\n".join("{}: {}".format(k, v) for k, v in settings.items()))
3333
# Add tmpdir to style path and reload so we can access this style.
3434
USER_LIBRARY_PATHS.append(tmpdir)
3535
style.reload_library()

0 commit comments

Comments
 (0)
0