Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

savefig crash on .eps filename #620

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

Merged
merged 1 commit into from
Dec 13, 2011
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fix usetex with .eps and .ps files in backend_ps.py
  • Loading branch information
mdboom committed Dec 12, 2011
commit 5acabc0cbe17cf9ae31f39caf7faff592c416f82
96 changes: 58 additions & 38 deletions 96 lib/matplotlib/backends/backend_ps.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ def gs_version(self):
from subprocess import Popen, PIPE
pipe = Popen(self.gs_exe + " --version",
shell=True, stdout=PIPE).stdout
gs_version = tuple(map(int, pipe.read().strip().split(".")))
if sys.version_info[0] >= 3:
ver = pipe.read().decode('ascii')
else:
ver = pipe.read()
gs_version = tuple(map(int, ver.strip().split(".")))

self._cached["gs_version"] = gs_version
return gs_version
Expand Down Expand Up @@ -1198,7 +1202,10 @@ def write(self, *kl, **kwargs):

self._pswriter = NullWriter()
else:
self._pswriter = StringIO()
if sys.version_info[0] >= 3:
self._pswriter = io.StringIO()
else:
self._pswriter = cStringIO.StringIO()


# mixed mode rendering
Expand All @@ -1219,7 +1226,11 @@ def write(self, *kl, **kwargs):

# write to a temp file, we'll move it to outfile when done
fd, tmpfile = mkstemp()
with io.fdopen(fd, 'w', encoding='ascii') as fh:
if sys.version_info[0] >= 3:
fh = io.open(fd, 'w', encoding='ascii')
else:
fh = io.open(fd, 'wb')
with fh:
# write the Encapsulated PostScript headers
print("%!PS-Adobe-3.0 EPSF-3.0", file=fh)
if title: print("%%Title: "+title, file=fh)
Expand Down Expand Up @@ -1298,7 +1309,15 @@ def write(self, *kl, **kwargs):
else: gs_distill(tmpfile, isEPSF, ptype=papertype, bbox=bbox,
rotated=psfrag_rotated)

if isinstance(outfile, file):
is_file = False
if sys.version_info[0] >= 3:
if isinstance(outfile, io.IOBase):
is_file = True
else:
if isinstance(outfile, file):
is_file = True

if is_file:
with open(tmpfile, 'rb') as fh:
outfile.write(fh.read())
else:
Expand Down Expand Up @@ -1355,12 +1374,12 @@ def convert_psfrags(tmpfile, psfrags, font_preamble, custom_preamble,
paperWidth, paperHeight,
'\n'.join(psfrags), angle, os.path.split(epsfile)[-1])

with io.open(latexfile, 'w', encoding='ascii') as latexh:
with io.open(latexfile, 'wb') as latexh:
if rcParams['text.latex.unicode']:
latexh.write(s.encode('utf8'))
else:
try:
latexh.write(s)
latexh.write(s.encode('ascii'))
except UnicodeEncodeError:
verbose.report("You are using unicode and latex, but have "
"not enabled the matplotlib 'text.latex.unicode' "
Expand All @@ -1375,7 +1394,7 @@ def convert_psfrags(tmpfile, psfrags, font_preamble, custom_preamble,
%(precmd, tmpdir, latexfile, outfile)
verbose.report(command, 'debug')
exit_status = os.system(command)

with io.open(outfile, 'rb') as fh:
if exit_status:
raise RuntimeError('LaTeX was not able to process your file:\
Expand Down Expand Up @@ -1447,7 +1466,7 @@ def gs_distill(tmpfile, eps=False, ptype='letter', bbox=None, rotated=False):
verbose.report(command, 'debug')
exit_status = os.system(command)

with io.open(outfile, 'rb'):
with io.open(outfile, 'rb') as fh:
if exit_status:
raise RuntimeError('ghostscript was not able to process \
your image.\nHere is the full report generated by ghostscript:\n\n' + fh.read())
Expand Down Expand Up @@ -1597,55 +1616,56 @@ def pstoeps(tmpfile, bbox=None, rotated=False):
bbox_info, rotate = None, None

epsfile = tmpfile + '.eps'
with io.open(epsfile, 'w', encoding='ascii') as epsh:
with io.open(tmpfile, 'r', encoding='ascii') as tmph:
with io.open(epsfile, 'wb') as epsh:
write = epsh.write
with io.open(tmpfile, 'rb') as tmph:
line = tmph.readline()
# Modify the header:
while line:
if line.startswith('%!PS'):
print("%!PS-Adobe-3.0 EPSF-3.0", file=epsh)
if line.startswith(b'%!PS'):
write(b"%!PS-Adobe-3.0 EPSF-3.0\n")
if bbox:
print(bbox_info, file=epsh)
elif line.startswith('%%EndComments'):
epsh.write(line)
print('%%BeginProlog', file=epsh)
print('save', file=epsh)
print('countdictstack', file=epsh)
print('mark', file=epsh)
print('newpath', file=epsh)
print('/showpage {} def', file=epsh)
print('/setpagedevice {pop} def', file=epsh)
print('%%EndProlog', file=epsh)
print('%%Page 1 1', file=epsh)
write(bbox_info.encode('ascii') + b'\n')
elif line.startswith(b'%%EndComments'):
write(line)
write(b'%%BeginProlog\n')
write(b'save\n')
write(b'countdictstack\n')
write(b'mark\n')
write(b'newpath\n')
write(b'/showpage {} def\n')
write(b'/setpagedevice {pop} def\n')
write(b'%%EndProlog\n')
write(b'%%Page 1 1\n')
if rotate:
print(rotate, file=epsh)
write(rotate.encode('ascii') + b'\n')
break
elif bbox and (line.startswith('%%Bound') \
or line.startswith('%%HiResBound') \
or line.startswith('%%DocumentMedia') \
or line.startswith('%%Pages')):
elif bbox and (line.startswith(b'%%Bound') \
or line.startswith(b'%%HiResBound') \
or line.startswith(b'%%DocumentMedia') \
or line.startswith(b'%%Pages')):
pass
else:
epsh.write(line)
write(line)
line = tmph.readline()
# Now rewrite the rest of the file, and modify the trailer.
# This is done in a second loop such that the header of the embedded
# eps file is not modified.
line = tmph.readline()
while line:
if line.startswith('%%Trailer'):
print('%%Trailer', file=epsh)
print('cleartomark', file=epsh)
print('countdictstack', file=epsh)
print('exch sub { end } repeat', file=epsh)
print('restore', file=epsh)
if line.startswith(b'%%Trailer'):
write(b'%%Trailer\n')
write(b'cleartomark\n')
write(b'countdictstack\n')
write(b'exch sub { end } repeat\n')
write(b'restore\n')
if rcParams['ps.usedistiller'] == 'xpdf':
# remove extraneous "end" operator:
line = tmph.readline()
elif line.startswith('%%PageBoundingBox'):
elif line.startswith(b'%%PageBoundingBox'):
pass
else:
epsh.write(line)
write(line)
line = tmph.readline()

os.remove(tmpfile)
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.