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

Skip to content

Matplotlib savefig() closes BytesIO object when saving in postscript format #1862

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
Mar 27, 2013
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
40 changes: 19 additions & 21 deletions lib/matplotlib/backends/backend_ps.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
# 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 @@ -974,11 +973,6 @@ def print_ps(self, outfile, *args, **kwargs):
def print_eps(self, outfile, *args, **kwargs):
return self._print_ps(outfile, 'eps', *args, **kwargs)






def _print_ps(self, outfile, format, *args, **kwargs):
papertype = kwargs.pop("papertype", rcParams['ps.papersize'])
papertype = papertype.lower()
Expand Down Expand Up @@ -1106,21 +1100,7 @@ def write(self, *kl, **kwargs):
self.figure.set_facecolor(origfacecolor)
self.figure.set_edgecolor(origedgecolor)

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:
def print_figure_impl():
if sys.version_info[0] >= 3:
fh = io.TextIOWrapper(raw_fh, encoding="ascii")
else:
Expand Down Expand Up @@ -1195,6 +1175,24 @@ def null_context(value):
if not isEPSF: print("%%EOF", file=fh)
fh.flush()

if sys.version_info[0] >= 3:
fh.detach()

if rcParams['ps.usedistiller']:
# We are going to use an external program to process the output.
# Write to a temporary file.
fd, tmpfile = mkstemp()
with io.open(fd, 'wb') as raw_fh:
print_figure_impl()
else:
# Write directly to outfile.
if passed_in_file_object:
raw_fh = outfile
print_figure_impl()
else:
with open(outfile, 'wb') as raw_fh:
print_figure_impl()

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