From 10e0c2bead7db5b0c85511e5791a2ce1e0c6745c Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 26 Aug 2012 13:41:10 +0530 Subject: [PATCH] ... --- src/calibre/ebooks/pdf/manipulate/info.py | 7 +++---- src/calibre/utils/podofo/__init__.py | 16 ++++++++-------- src/calibre/utils/podofo/doc.cpp | 10 ++++++---- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/calibre/ebooks/pdf/manipulate/info.py b/src/calibre/ebooks/pdf/manipulate/info.py index 9b6e0312d0..ee71dac71d 100644 --- a/src/calibre/ebooks/pdf/manipulate/info.py +++ b/src/calibre/ebooks/pdf/manipulate/info.py @@ -13,8 +13,9 @@ import os, sys from calibre.utils.config import OptionParser from calibre.utils.logging import Log -from calibre.constants import preferred_encoding, plugins +from calibre.constants import preferred_encoding from calibre.ebooks.pdf.verify import is_valid_pdfs, is_encrypted +from calibre.utils.podofo import get_podofo from calibre import prints USAGE = '\n%prog %%name ' + _('''\ @@ -32,9 +33,7 @@ def option_parser(name): return OptionParser(usage=usage) def print_info(pdf_path): - podofo, podofo_err = plugins['podofo'] - if not podofo: - raise RuntimeError('Failed to load PoDoFo with error:'+podofo_err) + podofo = get_podofo() p = podofo.PDFDoc() p.open(pdf_path) diff --git a/src/calibre/utils/podofo/__init__.py b/src/calibre/utils/podofo/__init__.py index 232b6536af..eb1d22d3e2 100644 --- a/src/calibre/utils/podofo/__init__.py +++ b/src/calibre/utils/podofo/__init__.py @@ -13,6 +13,12 @@ from calibre.ebooks.metadata import authors_to_string from calibre.ptempfile import TemporaryDirectory from calibre.utils.ipc.simple_worker import fork_job, WorkerError +def get_podofo(): + podofo, podofo_err = plugins['podofo'] + if podofo is None: + raise RuntimeError('Failed to load podofo: %s'%podofo_err) + return podofo + def prep(val): if not val: return u'' @@ -41,10 +47,7 @@ def set_metadata(stream, mi): stream.seek(0) def set_metadata_(tdir, title, authors, bkp, tags): - podofo, podofo_err = plugins['podofo'] - if podofo is None: - raise RuntimeError('Failed to load podofo: %s'%podofo_err) - + podofo = get_podofo() os.chdir(tdir) p = podofo.PDFDoc() p.open(u'input.pdf') @@ -80,10 +83,7 @@ def set_metadata_(tdir, title, authors, bkp, tags): def delete_all_but(path, pages): ''' Delete all the pages in the pdf except for the specified ones. Negative numbers are counted from the end of the PDF. ''' - podofo, podofo_err = plugins['podofo'] - if podofo is None: - raise RuntimeError('Failed to load podofo: %s'%podofo_err) - + podofo = get_podofo() p = podofo.PDFDoc() with open(path, 'rb') as f: raw = f.read() diff --git a/src/calibre/utils/podofo/doc.cpp b/src/calibre/utils/podofo/doc.cpp index 72180bb55f..6f462b252a 100644 --- a/src/calibre/utils/podofo/doc.cpp +++ b/src/calibre/utils/podofo/doc.cpp @@ -88,18 +88,20 @@ PDFDoc_save(PDFDoc *self, PyObject *args, PyObject *kwargs) { static PyObject * PDFDoc_write(PDFDoc *self, PyObject *args, PyObject *kwargs) { PyObject *ans; - PdfRefCountedBuffer buffer(1*1024*1024); - PdfOutputDevice out(&buffer); try { + PdfRefCountedBuffer buffer(1*1024*1024); + PdfOutputDevice out(&buffer); self->doc->Write(&out); + ans = PyBytes_FromStringAndSize(buffer.GetBuffer(), out.Tell()); + if (ans == NULL) PyErr_NoMemory(); } catch(const PdfError &err) { podofo_set_exception(err); return NULL; + } catch (...) { + return PyErr_NoMemory(); } - ans = PyBytes_FromStringAndSize(buffer.GetBuffer(), out.Tell()); - if (ans == NULL) PyErr_NoMemory(); return ans; } // }}}