Port libmtp plugin to build on python2/python3

This commit is contained in:
Eli Schwartz 2019-02-26 22:29:39 -05:00
parent c0754900fe
commit 6aa1592052

View File

@ -612,45 +612,44 @@ static PyGetSetDef Device_getsetters[] = {
}; };
static PyTypeObject DeviceType = { // {{{ static PyTypeObject DeviceType = { // {{{
PyObject_HEAD_INIT(NULL) PyVarObject_HEAD_INIT(NULL, 0)
0, /*ob_size*/ /* tp_name */ "libmtp.Device",
"libmtp.Device", /*tp_name*/ /* tp_basicsize */ sizeof(Device),
sizeof(Device), /*tp_basicsize*/ /* tp_itemsize */ 0,
0, /*tp_itemsize*/ /* tp_dealloc */ (destructor)Device_dealloc,
(destructor)Device_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)Device_init,
(initproc)Device_init, /* tp_init */ /* tp_alloc */ 0,
0, /* tp_alloc */ /* tp_new */ 0,
0, /* tp_new */
}; // }}} }; // }}}
// }}} End Device object definition // }}} End Device object definition
@ -699,6 +698,8 @@ known_devices(PyObject *self, PyObject *args) {
return ans; return ans;
} }
static char libmtp_doc[] = "Interface to libmtp.";
static PyMethodDef libmtp_methods[] = { static PyMethodDef libmtp_methods[] = {
{"set_debug_level", set_debug_level, METH_VARARGS, {"set_debug_level", set_debug_level, METH_VARARGS,
"set_debug_level(level)\n\nSet the debug level bit mask, see LIBMTP_DEBUG_* constants." "set_debug_level(level)\n\nSet the debug level bit mask, see LIBMTP_DEBUG_* constants."
@ -716,19 +717,41 @@ static PyMethodDef libmtp_methods[] = {
}; };
CALIBRE_MODINIT_FUNC #if PY_MAJOR_VERSION >= 3
initlibmtp(void) { #define INITERROR return NULL
PyObject *m; #define INITMODULE PyModule_Create(&libmtp_module)
static struct PyModuleDef libmtp_module = {
/* m_base */ PyModuleDef_HEAD_INIT,
/* m_name */ "libmtp",
/* m_doc */ libmtp_doc,
/* m_size */ -1,
/* m_methods */ libmtp_methods,
/* m_slots */ 0,
/* m_traverse */ 0,
/* m_clear */ 0,
/* m_free */ 0,
};
CALIBRE_MODINIT_FUNC PyInit_libmtp(void) {
#else
#define INITERROR return
#define INITMODULE Py_InitModule3("libmtp", libmtp_methods, libmtp_doc);
CALIBRE_MODINIT_FUNC initlibmtp(void) {
#endif
DeviceType.tp_new = PyType_GenericNew; DeviceType.tp_new = PyType_GenericNew;
if (PyType_Ready(&DeviceType) < 0) if (PyType_Ready(&DeviceType) < 0) {
return; INITERROR;
}
m = Py_InitModule3("libmtp", libmtp_methods, "Interface to libmtp."); PyObject *m = INITMODULE;
if (m == NULL) return; if (m == NULL) {
INITERROR;
}
MTPError = PyErr_NewException("libmtp.MTPError", NULL, NULL); MTPError = PyErr_NewException("libmtp.MTPError", NULL, NULL);
if (MTPError == NULL) return; if (MTPError == NULL) {
INITERROR;
}
PyModule_AddObject(m, "MTPError", MTPError); PyModule_AddObject(m, "MTPError", MTPError);
// Redirect stdout to get rid of the annoying message about mtpz. Really, // Redirect stdout to get rid of the annoying message about mtpz. Really,
@ -758,4 +781,8 @@ initlibmtp(void) {
PyModule_AddIntMacro(m, LIBMTP_DEBUG_USB); PyModule_AddIntMacro(m, LIBMTP_DEBUG_USB);
PyModule_AddIntMacro(m, LIBMTP_DEBUG_DATA); PyModule_AddIntMacro(m, LIBMTP_DEBUG_DATA);
PyModule_AddIntMacro(m, LIBMTP_DEBUG_ALL); PyModule_AddIntMacro(m, LIBMTP_DEBUG_ALL);
#if PY_MAJOR_VERSION >= 3
return m;
#endif
} }