|
13 | 13 | import shutil |
14 | 14 | import subprocess |
15 | 15 | import sys |
16 | | -from tempfile import mkstemp |
| 16 | +from tempfile import TemporaryDirectory |
17 | 17 | import time |
18 | 18 |
|
19 | 19 | import numpy as np |
@@ -1150,19 +1150,15 @@ def print_figure_impl(fh): |
1150 | 1150 | if rcParams['ps.usedistiller']: |
1151 | 1151 | # We are going to use an external program to process the output. |
1152 | 1152 | # 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: |
1156 | 1156 | print_figure_impl(fh) |
1157 | 1157 | if rcParams['ps.usedistiller'] == 'ghostscript': |
1158 | 1158 | gs_distill(tmpfile, isEPSF, ptype=papertype, bbox=bbox) |
1159 | 1159 | elif rcParams['ps.usedistiller'] == 'xpdf': |
1160 | 1160 | xpdf_distill(tmpfile, isEPSF, ptype=papertype, bbox=bbox) |
1161 | | - |
1162 | 1161 | _move_path_to_path_or_stream(tmpfile, outfile) |
1163 | | - finally: |
1164 | | - if os.path.isfile(tmpfile): |
1165 | | - os.unlink(tmpfile) |
1166 | 1162 |
|
1167 | 1163 | else: |
1168 | 1164 | # Write directly to outfile. |
@@ -1255,9 +1251,9 @@ def write(self, *kl, **kwargs): |
1255 | 1251 |
|
1256 | 1252 | # write to a temp file, we'll move it to outfile when done |
1257 | 1253 |
|
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: |
1261 | 1257 | # write the Encapsulated PostScript headers |
1262 | 1258 | print("%!PS-Adobe-3.0 EPSF-3.0", file=fh) |
1263 | 1259 | if title: |
@@ -1344,9 +1340,6 @@ def write(self, *kl, **kwargs): |
1344 | 1340 | rotated=psfrag_rotated) |
1345 | 1341 |
|
1346 | 1342 | _move_path_to_path_or_stream(tmpfile, outfile) |
1347 | | - finally: |
1348 | | - if os.path.isfile(tmpfile): |
1349 | | - os.unlink(tmpfile) |
1350 | 1343 |
|
1351 | 1344 |
|
1352 | 1345 | def convert_psfrags(tmpfile, psfrags, font_preamble, custom_preamble, |
|
0 commit comments