From 1857483eb0af9ce2fa7f23211aface5703396c99 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 20 Apr 2021 11:01:22 +0530 Subject: [PATCH] Start code review of WPD driver --- src/calibre/devices/mtp/windows/wpd.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/calibre/devices/mtp/windows/wpd.cpp b/src/calibre/devices/mtp/windows/wpd.cpp index 44d4cd3931..c1ddf18c51 100644 --- a/src/calibre/devices/mtp/windows/wpd.cpp +++ b/src/calibre/devices/mtp/windows/wpd.cpp @@ -97,9 +97,11 @@ wpd_enumerate_devices(PyObject *self, PyObject *args) { Py_END_ALLOW_THREADS; if (FAILED(hr)) return hresult_set_exc("Failed to refresh the list of portable devices", hr); + Py_BEGIN_ALLOW_THREADS; hr = portable_device_manager->GetDevices(NULL, &num_of_devices); - num_of_devices += 15; // Incase new devices were connected between this call and the next + Py_END_ALLOW_THREADS; if (FAILED(hr)) return hresult_set_exc("Failed to get number of devices on the system", hr); + num_of_devices += 15; // Incase new devices were connected between this call and the next pnp_device_ids = (PWSTR*)calloc(num_of_devices, sizeof(PWSTR)); if (pnp_device_ids == NULL) return PyErr_NoMemory(); @@ -111,7 +113,7 @@ wpd_enumerate_devices(PyObject *self, PyObject *args) { ans = PyTuple_New(num_of_devices); if (ans != NULL) { for(i = 0; i < num_of_devices; i++) { - temp = PyUnicode_FromWideChar(pnp_device_ids[i], wcslen(pnp_device_ids[i])); + temp = PyUnicode_FromWideChar(pnp_device_ids[i], -1); if (temp == NULL) { PyErr_NoMemory(); Py_DECREF(ans); ans = NULL; break;} PyTuple_SET_ITEM(ans, i, temp); } @@ -129,7 +131,7 @@ wpd_enumerate_devices(PyObject *self, PyObject *args) { pnp_device_ids = NULL; Py_END_ALLOW_THREADS; - return Py_BuildValue("N", ans); + return ans; } // }}} // device_info() {{{ @@ -144,8 +146,8 @@ wpd_device_info(PyObject *self, PyObject *args) { if (!PyArg_ParseTuple(args, "O", &py_pnp_id)) return NULL; pnp_id = unicode_to_wchar(py_pnp_id); - if (wcslen(pnp_id) < 1) { PyErr_SetString(WPDError, "The PNP id must not be empty."); return NULL; } if (pnp_id == NULL) return NULL; + if (wcslen(pnp_id) < 1) { PyErr_SetString(WPDError, "The PNP id must not be empty."); return NULL; } client_information = get_client_information(); if (client_information != NULL) {