Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backend_ps: Do not write to a temporary file unless using an external distiller #1826

Merged
merged 1 commit into from Mar 19, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 30 additions & 15 deletions lib/matplotlib/backends/backend_ps.py
Expand Up @@ -5,6 +5,7 @@
# PY3KTODO: Get rid of "print >>fh" syntax

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

fd, tmpfile = mkstemp()
with io.open(fd, 'wb') as raw_fh:
if rcParams['ps.usedistiller']:
# We are going to use an external program to process the output.
# Write to a temporary file.
fd, tmpfile = mkstemp()
context_manager = io.open(fd, 'wb')
else:
# Write directly to outfile.
if passed_in_file_object:
@contextlib.contextmanager
def null_context(value):
yield value
context_manager = null_context(outfile)
else:
context_manager = open(outfile, 'wb')
with context_manager as raw_fh:
if sys.version_info[0] >= 3:
fh = io.TextIOWrapper(raw_fh, encoding="ascii")
else:
Expand Down Expand Up @@ -1181,20 +1195,21 @@ def write(self, *kl, **kwargs):
if not isEPSF: print("%%EOF", file=fh)
fh.flush()

if rcParams['ps.usedistiller'] == 'ghostscript':
gs_distill(tmpfile, isEPSF, ptype=papertype, bbox=bbox)
elif rcParams['ps.usedistiller'] == 'xpdf':
xpdf_distill(tmpfile, isEPSF, ptype=papertype, bbox=bbox)
if rcParams['ps.usedistiller']:
if rcParams['ps.usedistiller'] == 'ghostscript':
gs_distill(tmpfile, isEPSF, ptype=papertype, bbox=bbox)
elif rcParams['ps.usedistiller'] == 'xpdf':
xpdf_distill(tmpfile, isEPSF, ptype=papertype, bbox=bbox)

if passed_in_file_object:
with open(tmpfile, 'rb') as fh:
outfile.write(fh.read())
else:
with open(outfile, 'w') as fh:
pass
mode = os.stat(outfile).st_mode
shutil.move(tmpfile, outfile)
os.chmod(outfile, mode)
if passed_in_file_object:
with open(tmpfile, 'rb') as fh:
outfile.write(fh.read())
else:
with open(outfile, 'w') as fh:
pass
mode = os.stat(outfile).st_mode
shutil.move(tmpfile, outfile)
os.chmod(outfile, mode)

def _print_figure_tex(self, outfile, format, dpi, facecolor, edgecolor,
orientation, isLandscape, papertype,
Expand Down