8000 Give a better error message on missing PostScript fonts by jkseppan · Pull Request #6428 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Give a better error message on missing PostScript fonts #6428

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 2 commits into from
Dec 19, 2016
Merged
Changes from 1 commit
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
Next Next commit
Separate pdf finalization from closing
If an error is raised and the object state is inconsistent,
we shouldn't try to finalize the file, just close all resources.
The idea is to support the following idiom:

    try:
        figure.draw(file)  # write pdf file, can raise
        file.finalize()    # do this if everything went well
    finally:
        file.close()       # do this in any case
  • Loading branch information
jkseppan committed May 23, 2016
commit 433b8996347dad2c144000a1d1e7b6b1e50f7cbe
17 changes: 12 additions & 5 deletions lib/matplotlib/backends/backend_pdf.py
39B9
Original file line number Diff line number Diff line change
Expand Up @@ -552,9 +552,10 @@ def newTextnote(self, text, positionRect=[-100, -100, 0, 0]):
self.writeObject(annotObject, theNote)
self.pageAnnotations.append(annotObject)

def close(self):
def finalize(self):
"Write out the various deferred objects and the pdf end matter."

self.endStream()
# Write out the various deferred objects
self.writeFonts()
self.writeObject(self.alphaStateObject,
dict([(val[0], val[1])
Expand Down Expand Up @@ -582,12 +583,16 @@ def close(self):
# Finalize the file
self.writeXref()
self.writeTrailer()

def close(self):
"Flush all buffers and free all resources."

self.endStream()
if self.passed_in_file_object:
self.fh.flush()
elif self.original_file_like is not None:
self.original_file_like.write(self.fh.getvalue())
self.fh.close()
else:
if self.original_file_like is not None:
self.original_file_like.write(self.fh.getvalue())
self.fh.close()

def write(self, data):
Expand Down Expand Up @@ -2438,6 +2443,7 @@ def close(self):
Finalize this object, making the underlying file a complete
PDF file.
"""
self._file.finalize()
self._file.close()
if (self.get_pagecount() == 0 and not self.keep_empty and
not self._file.passed_in_file_object):
Expand Down Expand Up @@ -2534,6 +2540,7 @@ def print_pdf(self, filename, **kwargs):
bbox_inches_restore=_bbox_inches_restore)
self.figure.draw(renderer)
renderer.finalize()
file.finalize()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did this get added here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file.close() call in the finally section no longer does the finalization. The finalization is the part that cannot be reasonably done if something has raised an exception (in this case, the missing font) so it belongs in the try section.

finally:
if isinstance(filename, PdfPages): # finish off this page
file.endStream()
Expand Down
0