Port wpd plugin to build on python2/python3

This commit is contained in:
Eli Schwartz 2019-03-13 00:42:00 -04:00
parent ccfe12e4f3
commit 3acac495d0
2 changed files with 76 additions and 48 deletions

View File

@ -204,43 +204,42 @@ static PyGetSetDef Device_getsetters[] = {
PyTypeObject wpd::DeviceType = { // {{{ PyTypeObject wpd::DeviceType = { // {{{
PyObject_HEAD_INIT(NULL) PyVarObject_HEAD_INIT(NULL, 0)
0, /*ob_size*/ /* tp_name */ "wpd.Device",
"wpd.Device", /*tp_name*/ /* tp_basicsize */ sizeof(Device),
sizeof(Device), /*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_BASETYPE,
Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /*tp_flags*/ /* tp_doc */ "Device",
"Device", /* 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 */ Device_methods,
Device_methods, /* tp_methods */ /* tp_members */ 0,
0, /* tp_members */ /* tp_getset */ Device_getsetters,
Device_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 */ (initproc)init,
(initproc)init, /* tp_init */ /* tp_alloc */ 0,
0, /* tp_alloc */ /* tp_new */ 0,
0, /* tp_new */
}; // }}} }; // }}}

View File

@ -161,6 +161,8 @@ wpd_device_info(PyObject *self, PyObject *args) {
return ans; return ans;
} // }}} } // }}}
static char wpd_doc[] = "Interface to the WPD windows service.";
static PyMethodDef wpd_methods[] = { static PyMethodDef wpd_methods[] = {
{"init", wpd_init, METH_VARARGS, {"init", wpd_init, METH_VARARGS,
"init(name, major_version, minor_version, revision)\n\n Initializes this module. Call this method *only* in the thread in which you intend to use this module. Also remember to call uninit before the thread exits." "init(name, major_version, minor_version, revision)\n\n Initializes this module. Call this method *only* in the thread in which you intend to use this module. Also remember to call uninit before the thread exits."
@ -181,33 +183,60 @@ static PyMethodDef wpd_methods[] = {
{NULL, NULL, 0, NULL} {NULL, NULL, 0, NULL}
}; };
#if PY_MAJOR_VERSION >= 3
#define INITERROR return NULL
#define INITMODULE PyModule_Create(&wpd_module)
static struct PyModuleDef wpd_module = {
/* m_base */ PyModuleDef_HEAD_INIT,
/* m_name */ "wpd",
/* m_doc */ wpd_doc,
/* m_size */ -1,
/* m_methods */ wpd_methods,
/* m_slots */ 0,
/* m_traverse */ 0,
/* m_clear */ 0,
/* m_free */ 0,
};
CALIBRE_MODINIT_FUNC PyInit_wpd(void) {
#else
#define INITERROR return
#define INITMODULE Py_InitModule3("wpd", wpd_methods, wpd_doc)
CALIBRE_MODINIT_FUNC initwpd(void) {
#endif
CALIBRE_MODINIT_FUNC
initwpd(void) {
PyObject *m; PyObject *m;
wpd::DeviceType.tp_new = PyType_GenericNew; wpd::DeviceType.tp_new = PyType_GenericNew;
if (PyType_Ready(&wpd::DeviceType) < 0) if (PyType_Ready(&wpd::DeviceType) < 0)
return; return;
m = Py_InitModule3("wpd", wpd_methods, "Interface to the WPD windows service."); m = INITMODULE;
if (m == NULL) return; if (m == NULL) {
INITERROR;
}
WPDError = PyErr_NewException("wpd.WPDError", NULL, NULL); WPDError = PyErr_NewException("wpd.WPDError", NULL, NULL);
if (WPDError == NULL) return; if (WPDError == NULL) {
INITERROR;
}
PyModule_AddObject(m, "WPDError", WPDError); PyModule_AddObject(m, "WPDError", WPDError);
NoWPD = PyErr_NewException("wpd.NoWPD", NULL, NULL); NoWPD = PyErr_NewException("wpd.NoWPD", NULL, NULL);
if (NoWPD == NULL) return; if (NoWPD == NULL) {
INITERROR;
}
PyModule_AddObject(m, "NoWPD", NoWPD); PyModule_AddObject(m, "NoWPD", NoWPD);
WPDFileBusy = PyErr_NewException("wpd.WPDFileBusy", NULL, NULL); WPDFileBusy = PyErr_NewException("wpd.WPDFileBusy", NULL, NULL);
if (WPDFileBusy == NULL) return; if (WPDFileBusy == NULL) {
INITERROR;
}
PyModule_AddObject(m, "WPDFileBusy", WPDFileBusy); PyModule_AddObject(m, "WPDFileBusy", WPDFileBusy);
Py_INCREF(&DeviceType); Py_INCREF(&DeviceType);
PyModule_AddObject(m, "Device", (PyObject *)&DeviceType); PyModule_AddObject(m, "Device", (PyObject *)&DeviceType);
#if PY_MAJOR_VERSION >= 3
return m;
#endif
} }