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 = { // {{{
PyObject_HEAD_INIT(NULL)
0, /*ob_size*/
"libmtp.Device", /*tp_name*/
sizeof(Device), /*tp_basicsize*/
0, /*tp_itemsize*/
(destructor)Device_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)Device_init, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
PyVarObject_HEAD_INIT(NULL, 0)
/* tp_name */ "libmtp.Device",
/* tp_basicsize */ sizeof(Device),
/* tp_itemsize */ 0,
/* tp_dealloc */ (destructor)Device_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)Device_init,
/* tp_alloc */ 0,
/* tp_new */ 0,
}; // }}}
// }}} End Device object definition
@ -699,6 +698,8 @@ known_devices(PyObject *self, PyObject *args) {
return ans;
}
static char libmtp_doc[] = "Interface to libmtp.";
static PyMethodDef libmtp_methods[] = {
{"set_debug_level", set_debug_level, METH_VARARGS,
"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
initlibmtp(void) {
PyObject *m;
#if PY_MAJOR_VERSION >= 3
#define INITERROR return NULL
#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;
if (PyType_Ready(&DeviceType) < 0)
return;
if (PyType_Ready(&DeviceType) < 0) {
INITERROR;
}
m = Py_InitModule3("libmtp", libmtp_methods, "Interface to libmtp.");
if (m == NULL) return;
PyObject *m = INITMODULE;
if (m == NULL) {
INITERROR;
}
MTPError = PyErr_NewException("libmtp.MTPError", NULL, NULL);
if (MTPError == NULL) return;
if (MTPError == NULL) {
INITERROR;
}
PyModule_AddObject(m, "MTPError", MTPError);
// 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_DATA);
PyModule_AddIntMacro(m, LIBMTP_DEBUG_ALL);
#if PY_MAJOR_VERSION >= 3
return m;
#endif
}