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 = { // {{{
PyObject_HEAD_INIT(NULL)
0, /*ob_size*/
"wpd.Device", /*tp_name*/
sizeof(Device), /*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|Py_TPFLAGS_BASETYPE, /*tp_flags*/
"Device", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
Device_methods, /* tp_methods */
0, /* tp_members */
Device_getsetters, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
(initproc)init, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
PyVarObject_HEAD_INIT(NULL, 0)
/* tp_name */ "wpd.Device",
/* tp_basicsize */ sizeof(Device),
/* 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|Py_TPFLAGS_BASETYPE,
/* tp_doc */ "Device",
/* tp_traverse */ 0,
/* tp_clear */ 0,
/* tp_richcompare */ 0,
/* tp_weaklistoffset */ 0,
/* tp_iter */ 0,
/* tp_iternext */ 0,
/* tp_methods */ Device_methods,
/* tp_members */ 0,
/* tp_getset */ Device_getsetters,
/* tp_base */ 0,
/* tp_dict */ 0,
/* tp_descr_get */ 0,
/* tp_descr_set */ 0,
/* tp_dictoffset */ 0,
/* tp_init */ (initproc)init,
/* tp_alloc */ 0,
/* tp_new */ 0,
}; // }}}

View File

@ -161,6 +161,8 @@ wpd_device_info(PyObject *self, PyObject *args) {
return ans;
} // }}}
static char wpd_doc[] = "Interface to the WPD windows service.";
static PyMethodDef wpd_methods[] = {
{"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."
@ -181,33 +183,60 @@ static PyMethodDef wpd_methods[] = {
{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;
wpd::DeviceType.tp_new = PyType_GenericNew;
if (PyType_Ready(&wpd::DeviceType) < 0)
return;
m = Py_InitModule3("wpd", wpd_methods, "Interface to the WPD windows service.");
if (m == NULL) return;
m = INITMODULE;
if (m == NULL) {
INITERROR;
}
WPDError = PyErr_NewException("wpd.WPDError", NULL, NULL);
if (WPDError == NULL) return;
if (WPDError == NULL) {
INITERROR;
}
PyModule_AddObject(m, "WPDError", WPDError);
NoWPD = PyErr_NewException("wpd.NoWPD", NULL, NULL);
if (NoWPD == NULL) return;
if (NoWPD == NULL) {
INITERROR;
}
PyModule_AddObject(m, "NoWPD", NoWPD);
WPDFileBusy = PyErr_NewException("wpd.WPDFileBusy", NULL, NULL);
if (WPDFileBusy == NULL) return;
if (WPDFileBusy == NULL) {
INITERROR;
}
PyModule_AddObject(m, "WPDFileBusy", WPDFileBusy);
Py_INCREF(&DeviceType);
PyModule_AddObject(m, "Device", (PyObject *)&DeviceType);
#if PY_MAJOR_VERSION >= 3
return m;
#endif
}