Port podofo plugin to build on python2/python3

This commit is contained in:
Eli Schwartz 2019-02-28 16:35:29 -05:00
parent 9ad1ebf487
commit c53322db0d
3 changed files with 121 additions and 95 deletions

View File

@ -605,45 +605,43 @@ static PyMethodDef PDFDoc_methods[] = {
// Type definition {{{ // Type definition {{{
PyTypeObject pdf::PDFDocType = { PyTypeObject pdf::PDFDocType = {
PyObject_HEAD_INIT(NULL) PyVarObject_HEAD_INIT(NULL, 0)
0, /*ob_size*/ /* tp_name */ "podofo.PDFDoc",
"podofo.PDFDoc", /*tp_name*/ /* tp_basicsize */ sizeof(PDFDoc),
sizeof(PDFDoc), /*tp_basicsize*/ /* tp_itemsize */ 0,
0, /*tp_itemsize*/ /* tp_dealloc */ (destructor)PDFDoc_dealloc,
(destructor)PDFDoc_dealloc, /*tp_dealloc*/ /* tp_print */ 0,
0, /*tp_print*/ /* tp_getattr */ 0,
0, /*tp_getattr*/ /* tp_setattr */ 0,
0, /*tp_setattr*/ /* tp_compare */ 0,
0, /*tp_compare*/ /* tp_repr */ 0,
0, /*tp_repr*/ /* tp_as_number */ 0,
0, /*tp_as_number*/ /* tp_as_sequence */ 0,
0, /*tp_as_sequence*/ /* tp_as_mapping */ 0,
0, /*tp_as_mapping*/ /* tp_hash */ 0,
0, /*tp_hash */ /* tp_call */ 0,
0, /*tp_call*/ /* tp_str */ 0,
0, /*tp_str*/ /* tp_getattro */ 0,
0, /*tp_getattro*/ /* tp_setattro */ 0,
0, /*tp_setattro*/ /* tp_as_buffer */ 0,
0, /*tp_as_buffer*/ /* tp_flags */ Py_TPFLAGS_DEFAULT,
Py_TPFLAGS_DEFAULT, /*tp_flags*/ /* tp_doc */ "PDF Documents",
"PDF Documents", /* tp_doc */ /* tp_traverse */ 0,
0, /* tp_traverse */ /* tp_clear */ 0,
0, /* tp_clear */ /* tp_richcompare */ 0,
0, /* tp_richcompare */ /* tp_weaklistoffset */ 0,
0, /* tp_weaklistoffset */ /* tp_iter */ 0,
0, /* tp_iter */ /* tp_iternext */ 0,
0, /* tp_iternext */ /* tp_methods */ PDFDoc_methods,
PDFDoc_methods, /* tp_methods */ /* tp_members */ 0,
0, /* tp_members */ /* tp_getset */ PDFDoc_getsetters,
PDFDoc_getsetters, /* tp_getset */ /* tp_base */ 0,
0, /* tp_base */ /* tp_dict */ 0,
0, /* tp_dict */ /* tp_descr_get */ 0,
0, /* tp_descr_get */ /* tp_descr_set */ 0,
0, /* tp_descr_set */ /* tp_dictoffset */ 0,
0, /* tp_dictoffset */ /* tp_init */ 0,
0, /* tp_init */ /* tp_alloc */ 0,
0, /* tp_alloc */ /* tp_new */ PDFDoc_new,
PDFDoc_new, /* tp_new */
}; };
// }}} // }}}

View File

@ -96,45 +96,43 @@ static PyMethodDef methods[] = {
// Type definition {{{ // Type definition {{{
PyTypeObject pdf::PDFOutlineItemType = { PyTypeObject pdf::PDFOutlineItemType = {
PyObject_HEAD_INIT(NULL) PyVarObject_HEAD_INIT(NULL, 0)
0, /*ob_size*/ /* tp_name */ "podofo.PDFOutlineItem",
"podofo.PDFOutlineItem", /*tp_name*/ /* tp_basicsize */ sizeof(PDFOutlineItem),
sizeof(PDFOutlineItem), /*tp_basicsize*/ /* tp_itemsize */ 0,
0, /*tp_itemsize*/ /* tp_dealloc */ (destructor)dealloc,
(destructor)dealloc, /*tp_dealloc*/ /* tp_print */ 0,
0, /*tp_print*/ /* tp_getattr */ 0,
0, /*tp_getattr*/ /* tp_setattr */ 0,
0, /*tp_setattr*/ /* tp_compare */ 0,
0, /*tp_compare*/ /* tp_repr */ 0,
0, /*tp_repr*/ /* tp_as_number */ 0,
0, /*tp_as_number*/ /* tp_as_sequence */ 0,
0, /*tp_as_sequence*/ /* tp_as_mapping */ 0,
0, /*tp_as_mapping*/ /* tp_hash */ 0,
0, /*tp_hash */ /* tp_call */ 0,
0, /*tp_call*/ /* tp_str */ 0,
0, /*tp_str*/ /* tp_getattro */ 0,
0, /*tp_getattro*/ /* tp_setattro */ 0,
0, /*tp_setattro*/ /* tp_as_buffer */ 0,
0, /*tp_as_buffer*/ /* tp_flags */ Py_TPFLAGS_DEFAULT,
Py_TPFLAGS_DEFAULT, /*tp_flags*/ /* tp_doc */ "PDF Outline items",
"PDF Outline items", /* tp_doc */ /* tp_traverse */ 0,
0, /* tp_traverse */ /* tp_clear */ 0,
0, /* tp_clear */ /* tp_richcompare */ 0,
0, /* tp_richcompare */ /* tp_weaklistoffset */ 0,
0, /* tp_weaklistoffset */ /* tp_iter */ 0,
0, /* tp_iter */ /* tp_iternext */ 0,
0, /* tp_iternext */ /* tp_methods */ methods,
methods, /* tp_methods */ /* tp_members */ 0,
0, /* tp_members */ /* tp_getset */ 0,
0, /* tp_getset */ /* tp_base */ 0,
0, /* tp_base */ /* tp_dict */ 0,
0, /* tp_dict */ /* tp_descr_get */ 0,
0, /* tp_descr_get */ /* tp_descr_set */ 0,
0, /* tp_descr_set */ /* tp_dictoffset */ 0,
0, /* tp_dictoffset */ /* tp_init */ 0,
0, /* tp_init */ /* tp_alloc */ 0,
0, /* tp_alloc */ /* tp_new */ new_item,
new_item, /* tp_new */
}; };
// }}} // }}}

View File

@ -10,10 +10,6 @@ using namespace PoDoFo;
PyObject *pdf::Error = NULL; PyObject *pdf::Error = NULL;
static PyMethodDef podofo_methods[] = {
{NULL} /* Sentinel */
};
class PyLogMessage : public PdfError::LogMessageCallback { class PyLogMessage : public PdfError::LogMessageCallback {
public: public:
@ -38,29 +34,63 @@ class PyLogMessage : public PdfError::LogMessageCallback {
PyLogMessage log_message; PyLogMessage log_message;
CALIBRE_MODINIT_FUNC static char podofo_doc[] = "Wrapper for the PoDoFo PDF library";
initpodofo(void)
{ static PyMethodDef podofo_methods[] = {
{NULL} /* Sentinel */
};
#if PY_MAJOR_VERSION >= 3
#define INITERROR return NULL
#define INITMODULE PyModule_Create(&podofo_module)
static struct PyModuleDef podofo_module = {
/* m_base */ PyModuleDef_HEAD_INIT,
/* m_name */ "podofo",
/* m_doc */ podofo_doc,
/* m_size */ -1,
/* m_methods */ podofo_methods,
/* m_slots */ 0,
/* m_traverse */ 0,
/* m_clear */ 0,
/* m_free */ 0,
};
CALIBRE_MODINIT_FUNC PyInit_podofo(void) {
#else
#define INITERROR return
#define INITMODULE Py_InitModule3("podofo", podofo_methods, podofo_doc)
CALIBRE_MODINIT_FUNC initpodofo(void) {
#endif
PyObject* m; PyObject* m;
if (PyType_Ready(&pdf::PDFDocType) < 0) if (PyType_Ready(&pdf::PDFDocType) < 0) {
return; INITERROR;
}
if (PyType_Ready(&pdf::PDFOutlineItemType) < 0) if (PyType_Ready(&pdf::PDFOutlineItemType) < 0) {
return; INITERROR;
}
pdf::Error = PyErr_NewException((char*)"podofo.Error", NULL, NULL); pdf::Error = PyErr_NewException((char*)"podofo.Error", NULL, NULL);
if (pdf::Error == NULL) return; if (pdf::Error == NULL) {
INITERROR;
}
PdfError::SetLogMessageCallback((PdfError::LogMessageCallback*)&log_message); PdfError::SetLogMessageCallback((PdfError::LogMessageCallback*)&log_message);
PdfError::EnableDebug(false); PdfError::EnableDebug(false);
m = Py_InitModule3("podofo", podofo_methods,
"Wrapper for the PoDoFo PDF library"); m = INITMODULE;
if (m == NULL) {
INITERROR;
}
Py_INCREF(&pdf::PDFDocType); Py_INCREF(&pdf::PDFDocType);
PyModule_AddObject(m, "PDFDoc", (PyObject *)&pdf::PDFDocType); PyModule_AddObject(m, "PDFDoc", (PyObject *)&pdf::PDFDocType);
PyModule_AddObject(m, "Error", pdf::Error); PyModule_AddObject(m, "Error", pdf::Error);
}
#if PY_MAJOR_VERSION >= 3
return m;
#endif
}