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 {{{
PyTypeObject pdf::PDFDocType = {
PyObject_HEAD_INIT(NULL)
0, /*ob_size*/
"podofo.PDFDoc", /*tp_name*/
sizeof(PDFDoc), /*tp_basicsize*/
0, /*tp_itemsize*/
(destructor)PDFDoc_dealloc, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash */
0, /*tp_call*/
0, /*tp_str*/
0, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT, /*tp_flags*/
"PDF Documents", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
PDFDoc_methods, /* tp_methods */
0, /* tp_members */
PDFDoc_getsetters, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
PDFDoc_new, /* tp_new */
PyVarObject_HEAD_INIT(NULL, 0)
/* tp_name */ "podofo.PDFDoc",
/* tp_basicsize */ sizeof(PDFDoc),
/* tp_itemsize */ 0,
/* tp_dealloc */ (destructor)PDFDoc_dealloc,
/* tp_print */ 0,
/* tp_getattr */ 0,
/* tp_setattr */ 0,
/* tp_compare */ 0,
/* tp_repr */ 0,
/* tp_as_number */ 0,
/* tp_as_sequence */ 0,
/* tp_as_mapping */ 0,
/* tp_hash */ 0,
/* tp_call */ 0,
/* tp_str */ 0,
/* tp_getattro */ 0,
/* tp_setattro */ 0,
/* tp_as_buffer */ 0,
/* tp_flags */ Py_TPFLAGS_DEFAULT,
/* tp_doc */ "PDF Documents",
/* tp_traverse */ 0,
/* tp_clear */ 0,
/* tp_richcompare */ 0,
/* tp_weaklistoffset */ 0,
/* tp_iter */ 0,
/* tp_iternext */ 0,
/* tp_methods */ PDFDoc_methods,
/* tp_members */ 0,
/* tp_getset */ PDFDoc_getsetters,
/* tp_base */ 0,
/* tp_dict */ 0,
/* tp_descr_get */ 0,
/* tp_descr_set */ 0,
/* tp_dictoffset */ 0,
/* tp_init */ 0,
/* tp_alloc */ 0,
/* tp_new */ PDFDoc_new,
};
// }}}

View File

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

View File

@ -10,10 +10,6 @@ using namespace PoDoFo;
PyObject *pdf::Error = NULL;
static PyMethodDef podofo_methods[] = {
{NULL} /* Sentinel */
};
class PyLogMessage : public PdfError::LogMessageCallback {
public:
@ -38,29 +34,63 @@ class PyLogMessage : public PdfError::LogMessageCallback {
PyLogMessage log_message;
CALIBRE_MODINIT_FUNC
initpodofo(void)
{
static char podofo_doc[] = "Wrapper for the PoDoFo PDF library";
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;
if (PyType_Ready(&pdf::PDFDocType) < 0)
return;
if (PyType_Ready(&pdf::PDFDocType) < 0) {
INITERROR;
}
if (PyType_Ready(&pdf::PDFOutlineItemType) < 0)
return;
if (PyType_Ready(&pdf::PDFOutlineItemType) < 0) {
INITERROR;
}
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::EnableDebug(false);
m = Py_InitModule3("podofo", podofo_methods,
"Wrapper for the PoDoFo PDF library");
m = INITMODULE;
if (m == NULL) {
INITERROR;
}
Py_INCREF(&pdf::PDFDocType);
PyModule_AddObject(m, "PDFDoc", (PyObject *)&pdf::PDFDocType);
PyModule_AddObject(m, "Error", pdf::Error);
}
#if PY_MAJOR_VERSION >= 3
return m;
#endif
}