diff --git a/src/calibre/devices/mtp/unix/libmtp.c b/src/calibre/devices/mtp/unix/libmtp.c index 21db96f115..58e8df6686 100644 --- a/src/calibre/devices/mtp/unix/libmtp.c +++ b/src/calibre/devices/mtp/unix/libmtp.c @@ -7,15 +7,17 @@ #include "devices.h" // Macros and utilities +static PyObject *MTPError = NULL; + #define ENSURE_DEV(rval) \ if (self->device == NULL) { \ - PyErr_SetString(PyExc_ValueError, "This device has not been initialized."); \ + PyErr_SetString(MTPError, "This device has not been initialized."); \ return rval; \ } #define ENSURE_STORAGE(rval) \ if (self->device->storage == NULL) { \ - PyErr_SetString(PyExc_RuntimeError, "The device has no storage information."); \ + PyErr_SetString(MTPError, "The device has no storage information."); \ return rval; \ } @@ -31,6 +33,7 @@ #define AC_ReadOnly 0x0001 #define AC_ReadOnly_with_Object_Deletion 0x0002 + typedef struct { PyObject *obj; PyObject *extra; @@ -183,7 +186,7 @@ libmtp_Device_init(libmtp_Device *self, PyObject *args, PyObject *kwds) Py_END_ALLOW_THREADS; if (dev == NULL) { - PyErr_SetString(PyExc_ValueError, "Unable to open raw device."); + PyErr_SetString(MTPError, "Unable to open raw device."); return -1; } @@ -272,7 +275,7 @@ static PyObject* libmtp_Device_update_storage_info(libmtp_Device *self, PyObject *args, PyObject *kwargs) { ENSURE_DEV(NULL); if (LIBMTP_Get_Storage(self->device, LIBMTP_STORAGE_SORTBY_NOTSORTED) < 0) { - PyErr_SetString(PyExc_RuntimeError, "Failed to get storage infor for device."); + PyErr_SetString(MTPError, "Failed to get storage infor for device."); return NULL; } Py_RETURN_NONE; @@ -761,6 +764,8 @@ initlibmtp(void) { m = Py_InitModule3("libmtp", libmtp_methods, "Interface to libmtp."); if (m == NULL) return; + MTPError = PyErr_NewException("libmtp.MTPError", NULL, NULL); + if (MTPError == NULL) return; LIBMTP_Init(); LIBMTP_Set_Debug(LIBMTP_DEBUG_NONE); diff --git a/src/calibre/devices/mtp/windows/wpd.cpp b/src/calibre/devices/mtp/windows/wpd.cpp index f4029e7061..889a3e02c5 100644 --- a/src/calibre/devices/mtp/windows/wpd.cpp +++ b/src/calibre/devices/mtp/windows/wpd.cpp @@ -14,7 +14,7 @@ #include static int _com_initialized = 0; -static PyObject *WPDError; +static PyObject *WPDError = NULL; static IPortableDeviceManager *portable_device_manager = NULL; static PyObject * @@ -32,6 +32,7 @@ wpd_init(PyObject *self, PyObject *args) { CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&portable_device_manager)); if (FAILED(hr)) { + portable_device_manager = NULL; PyErr_SetString(WPDError, (hr == REGDB_E_CLASSNOTREG) ? "This computer is not running the Windows Portable Device framework. You may need to install Windows Media Player 11 or newer." : "Failed to create the WPD device manager interface");