mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Cleaner exception handling
This commit is contained in:
parent
157c5a1cb7
commit
32d483b3ce
@ -744,16 +744,13 @@ static PyMethodDef PDFDoc_methods[] = {
|
|||||||
{"alter_links", (PyCFunction)PDFDoc_alter_links, METH_VARARGS,
|
{"alter_links", (PyCFunction)PDFDoc_alter_links, METH_VARARGS,
|
||||||
"alter_links() -> Change links in the document."
|
"alter_links() -> Change links in the document."
|
||||||
},
|
},
|
||||||
{"list_fonts", (PyCFunction)list_fonts, METH_VARARGS,
|
{"list_fonts", (PyCFunction)py_list_fonts, METH_VARARGS,
|
||||||
"list_fonts() -> Get list of fonts in document"
|
"list_fonts() -> Get list of fonts in document"
|
||||||
},
|
},
|
||||||
{"remove_fonts", (PyCFunction)remove_fonts, METH_VARARGS,
|
{"remove_unused_fonts", (PyCFunction)py_remove_unused_fonts, METH_NOARGS,
|
||||||
"remove_fonts() -> Remove the specified font objects."
|
|
||||||
},
|
|
||||||
{"remove_unused_fonts", (PyCFunction)remove_unused_fonts, METH_NOARGS,
|
|
||||||
"remove_unused_fonts() -> Remove unused font objects."
|
"remove_unused_fonts() -> Remove unused font objects."
|
||||||
},
|
},
|
||||||
{"merge_fonts", (PyCFunction)merge_fonts, METH_VARARGS,
|
{"merge_fonts", (PyCFunction)py_merge_fonts, METH_VARARGS,
|
||||||
"merge_fonts() -> Merge the specified fonts."
|
"merge_fonts() -> Merge the specified fonts."
|
||||||
},
|
},
|
||||||
{"delete_pages", (PyCFunction)PDFDoc_delete_pages, METH_VARARGS,
|
{"delete_pages", (PyCFunction)PDFDoc_delete_pages, METH_VARARGS,
|
||||||
|
@ -11,6 +11,14 @@
|
|||||||
|
|
||||||
using namespace pdf;
|
using namespace pdf;
|
||||||
|
|
||||||
|
#define PYWRAP(name) extern "C" PyObject* py_##name(PDFDoc *self, PyObject *args) { \
|
||||||
|
try { \
|
||||||
|
return name(self, args); \
|
||||||
|
} catch (const PdfError &err) { podofo_set_exception(err); return NULL; \
|
||||||
|
} catch (const std::exception &err) { PyErr_Format(Error, "Error in %s(): %s", #name, err.what()); return NULL; \
|
||||||
|
} catch (...) { PyErr_SetString(Error, "An unknown error occurred in " #name); return NULL; } \
|
||||||
|
}
|
||||||
|
|
||||||
static inline PyObject*
|
static inline PyObject*
|
||||||
ref_as_tuple(const PdfReference &ref) {
|
ref_as_tuple(const PdfReference &ref) {
|
||||||
unsigned long num = ref.ObjectNumber(), generation = ref.GenerationNumber();
|
unsigned long num = ref.ObjectNumber(), generation = ref.GenerationNumber();
|
||||||
@ -149,14 +157,12 @@ convert_w_array(PyObject *src, PdfArray &dest) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" {
|
static PyObject*
|
||||||
PyObject*
|
|
||||||
list_fonts(PDFDoc *self, PyObject *args) {
|
list_fonts(PDFDoc *self, PyObject *args) {
|
||||||
int get_font_data = 0;
|
int get_font_data = 0;
|
||||||
if (!PyArg_ParseTuple(args, "|i", &get_font_data)) return NULL;
|
if (!PyArg_ParseTuple(args, "|i", &get_font_data)) return NULL;
|
||||||
pyunique_ptr ans(PyList_New(0));
|
pyunique_ptr ans(PyList_New(0));
|
||||||
if (!ans) return NULL;
|
if (!ans) return NULL;
|
||||||
try {
|
|
||||||
const PdfVecObjects &objects = self->doc->GetObjects();
|
const PdfVecObjects &objects = self->doc->GetObjects();
|
||||||
for (TCIVecObjects it = objects.begin(); it != objects.end(); it++) {
|
for (TCIVecObjects it = objects.begin(); it != objects.end(); it++) {
|
||||||
if ((*it)->IsDictionary()) {
|
if ((*it)->IsDictionary()) {
|
||||||
@ -223,36 +229,14 @@ list_fonts(PDFDoc *self, PyObject *args) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (const PdfError &err) {
|
|
||||||
podofo_set_exception(err);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
return ans.release();
|
return ans.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
PyObject*
|
|
||||||
remove_fonts(PDFDoc *self, PyObject *args) {
|
|
||||||
PyObject *fonts;
|
|
||||||
if (!PyArg_ParseTuple(args, "O!", &PyTuple_Type, &fonts)) return NULL;
|
|
||||||
PdfVecObjects &objects = self->doc->GetObjects();
|
|
||||||
for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(fonts); i++) {
|
|
||||||
unsigned long num, gen;
|
|
||||||
if (!PyArg_ParseTuple(PyTuple_GET_ITEM(fonts, i), "kk", &num, &gen)) return NULL;
|
|
||||||
PdfReference ref(num, gen);
|
|
||||||
PdfObject *font = objects.GetObject(ref);
|
|
||||||
if (font) {
|
|
||||||
remove_font(objects, font);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Py_RETURN_NONE;
|
|
||||||
}
|
|
||||||
|
|
||||||
typedef std::unordered_map<PdfReference, unsigned long, PdfReferenceHasher> charprocs_usage_map;
|
typedef std::unordered_map<PdfReference, unsigned long, PdfReferenceHasher> charprocs_usage_map;
|
||||||
|
|
||||||
PyObject*
|
static PyObject*
|
||||||
remove_unused_fonts(PDFDoc *self, PyObject *args) {
|
remove_unused_fonts(PDFDoc *self, PyObject *args) {
|
||||||
unsigned long count = 0;
|
unsigned long count = 0;
|
||||||
try {
|
|
||||||
unordered_reference_set used_fonts;
|
unordered_reference_set used_fonts;
|
||||||
for (int i = 0; i < self->doc->GetPageCount(); i++) {
|
for (int i = 0; i < self->doc->GetPageCount(); i++) {
|
||||||
PdfPage *page = self->doc->GetPage(i);
|
PdfPage *page = self->doc->GetPage(i);
|
||||||
@ -307,7 +291,6 @@ remove_unused_fonts(PDFDoc *self, PyObject *args) {
|
|||||||
delete objects.RemoveObject(x.first);
|
delete objects.RemoveObject(x.first);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch(const PdfError &err) { podofo_set_exception(err); return NULL; }
|
|
||||||
|
|
||||||
return Py_BuildValue("k", count);
|
return Py_BuildValue("k", count);
|
||||||
}
|
}
|
||||||
@ -373,4 +356,6 @@ merge_fonts(PDFDoc *self, PyObject *args) {
|
|||||||
Py_RETURN_NONE;
|
Py_RETURN_NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
PYWRAP(list_fonts)
|
||||||
|
PYWRAP(merge_fonts)
|
||||||
|
PYWRAP(remove_unused_fonts)
|
||||||
|
@ -96,9 +96,8 @@ typedef std::unordered_set<PdfReference, PdfReferenceHasher> unordered_reference
|
|||||||
|
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
PyObject* list_fonts(PDFDoc*, PyObject*);
|
PyObject* py_list_fonts(PDFDoc*, PyObject*);
|
||||||
PyObject* remove_fonts(PDFDoc *self, PyObject *args);
|
PyObject* py_remove_unused_fonts(PDFDoc *self, PyObject *args);
|
||||||
PyObject* remove_unused_fonts(PDFDoc *self, PyObject *args);
|
PyObject* py_merge_fonts(PDFDoc *self, PyObject *args);
|
||||||
PyObject* merge_fonts(PDFDoc *self, PyObject *args);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user