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

Skip to content

Commit 4703f2f

Browse files
committed
Merge pull request #1826 from mgiuca-google/backend_ps-no-temp-file
backend_ps: Do not write to a temporary file unless using an external distiller
2 parents 9159f11 + 8d224d3 commit 4703f2f

File tree

1 file changed

+30
-15
lines changed

1 file changed

+30
-15
lines changed

lib/matplotlib/backends/backend_ps.py

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# PY3KTODO: Get rid of "print >>fh" syntax
66

77
from __future__ import division, print_function
8+
import contextlib
89
import glob, math, os, shutil, sys, time
910
def _fn_name(): return sys._getframe(1).f_code.co_name
1011
import io
@@ -1105,8 +1106,21 @@ def write(self, *kl, **kwargs):
11051106
self.figure.set_facecolor(origfacecolor)
11061107
self.figure.set_edgecolor(origedgecolor)
11071108

1108-
fd, tmpfile = mkstemp()
1109-
with io.open(fd, 'wb') as raw_fh:
1109+
if rcParams['ps.usedistiller']:
1110+
# We are going to use an external program to process the output.
1111+
# Write to a temporary file.
1112+
fd, tmpfile = mkstemp()
1113+
context_manager = io.open(fd, 'wb')
1114+
else:
1115+
# Write directly to outfile.
1116+
if passed_in_file_object:
1117+
@contextlib.contextmanager
1118+
def null_context(value):
1119+
yield value
1120+
context_manager = null_context(outfile)
1121+
else:
1122+
context_manager = open(outfile, 'wb')
1123+
with context_manager as raw_fh:
11101124
if sys.version_info[0] >= 3:
11111125
fh = io.TextIOWrapper(raw_fh, encoding="ascii")
11121126
else:
@@ -1181,20 +1195,21 @@ def write(self, *kl, **kwargs):
11811195
if not isEPSF: print("%%EOF", file=fh)
11821196
fh.flush()
11831197

1184-
if rcParams['ps.usedistiller'] == 'ghostscript':
1185-
gs_distill(tmpfile, isEPSF, ptype=papertype, bbox=bbox)
1186-
elif rcParams['ps.usedistiller'] == 'xpdf':
1187-
xpdf_distill(tmpfile, isEPSF, ptype=papertype, bbox=bbox)
1198+
if rcParams['ps.usedistiller']:
1199+
if rcParams['ps.usedistiller'] == 'ghostscript':
1200+
gs_distill(tmpfile, isEPSF, ptype=papertype, bbox=bbox)
1201+
elif rcParams['ps.usedistiller'] == 'xpdf':
1202+
xpdf_distill(tmpfile, isEPSF, ptype=papertype, bbox=bbox)
11881203

1189-
if passed_in_file_object:
1190-
with open(tmpfile, 'rb') as fh:
1191-
outfile.write(fh.read())
1192-
else:
1193-
with open(outfile, 'w') as fh:
1194-
pass
1195-
mode = os.stat(outfile).st_mode
1196-
shutil.move(tmpfile, outfile)
1197-
os.chmod(outfile, mode)
1204+
if passed_in_file_object:
1205+
with open(tmpfile, 'rb') as fh:
1206+
outfile.write(fh.read())
1207+
else:
1208+
with open(outfile, 'w') as fh:
1209+
pass
1210+
mode = os.stat(outfile).st_mode
1211+
shutil.move(tmpfile, outfile)
1212+
os.chmod(outfile, mode)
11981213

11991214
def _print_figure_tex(self, outfile, format, dpi, facecolor, edgecolor,
12001215
orientation, isLandscape, papertype,

0 commit comments

Comments
 (0)