From 55f52e0e0908ce97a5960b898b5f399f4869a3c1 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 21 Apr 2021 11:17:45 +0530 Subject: [PATCH] Use RAII in device.cpp --- src/calibre/devices/mtp/windows/device.cpp | 57 ++++++---------------- src/calibre/utils/cpp_binding.h | 2 +- 2 files changed, 16 insertions(+), 43 deletions(-) diff --git a/src/calibre/devices/mtp/windows/device.cpp b/src/calibre/devices/mtp/windows/device.cpp index 2389ac7dec..41f9b464c9 100644 --- a/src/calibre/devices/mtp/windows/device.cpp +++ b/src/calibre/devices/mtp/windows/device.cpp @@ -70,68 +70,41 @@ py_get_filesystem(Device *self, PyObject *args) { // get_file() {{{ static PyObject* py_get_file(Device *self, PyObject *args) { - PyObject *object_id, *stream, *callback = NULL, *ret; - wchar_t *object; - - if (!PyArg_ParseTuple(args, "OO|O", &object_id, &stream, &callback)) return NULL; - object = unicode_to_wchar(object_id); - if (object == NULL) return NULL; + PyObject *stream, *callback = NULL; + wchar_raii object; + if (!PyArg_ParseTuple(args, "O&O|O", py_to_wchar, &object, &stream, &callback)) return NULL; if (callback == NULL || !PyCallable_Check(callback)) callback = NULL; - - ret = wpd::get_file(self->device, object, stream, callback); - free(object); - return ret; + return wpd::get_file(self->device, object.ptr(), stream, callback); } // }}} // create_folder() {{{ static PyObject* py_create_folder(Device *self, PyObject *args) { - PyObject *pparent_id, *pname, *ret; - wchar_t *parent_id, *name; - - if (!PyArg_ParseTuple(args, "OO", &pparent_id, &pname)) return NULL; - parent_id = unicode_to_wchar(pparent_id); - name = unicode_to_wchar(pname); - if (parent_id == NULL || name == NULL) return NULL; - - ret = wpd::create_folder(self->device, parent_id, name); - free(parent_id); free(name); - return ret; + wchar_raii parent_id, name; + if (!PyArg_ParseTuple(args, "O&O&", py_to_wchar, &parent_id, py_to_wchar, &name)) return NULL; + return wpd::create_folder(self->device, parent_id.ptr(), name.ptr()); } // }}} // delete_object() {{{ static PyObject* py_delete_object(Device *self, PyObject *args) { - PyObject *pobject_id, *ret; - wchar_t *object_id; + wchar_raii object_id; - if (!PyArg_ParseTuple(args, "O", &pobject_id)) return NULL; - object_id = unicode_to_wchar(pobject_id); - if (object_id == NULL) return NULL; - - ret = wpd::delete_object(self->device, object_id); - free(object_id); - return ret; + if (!PyArg_ParseTuple(args, "O&", py_to_wchar, &object_id)) return NULL; + return wpd::delete_object(self->device, object_id.ptr()); } // }}} -// get_file() {{{ +// put_file() {{{ static PyObject* py_put_file(Device *self, PyObject *args) { - PyObject *pparent_id, *pname, *stream, *callback = NULL, *ret; - wchar_t *parent_id, *name; + PyObject *stream, *callback = NULL; + wchar_raii parent_id, name; unsigned long long size; - if (!PyArg_ParseTuple(args, "OOOK|O", &pparent_id, &pname, &stream, &size, &callback)) return NULL; - parent_id = unicode_to_wchar(pparent_id); - name = unicode_to_wchar(pname); - if (parent_id == NULL || name == NULL) return NULL; - + if (!PyArg_ParseTuple(args, "O&O&OK|O", py_to_wchar, &parent_id, py_to_wchar, &name, &stream, &size, &callback)) return NULL; if (callback == NULL || !PyCallable_Check(callback)) callback = NULL; - - ret = wpd::put_file(self->device, parent_id, name, stream, size, callback); - free(parent_id); free(name); - return ret; + return wpd::put_file(self->device, parent_id.ptr(), name.ptr(), stream, size, callback); } // }}} static PyMethodDef Device_methods[] = { diff --git a/src/calibre/utils/cpp_binding.h b/src/calibre/utils/cpp_binding.h index 7803194c7e..0fe721b49a 100644 --- a/src/calibre/utils/cpp_binding.h +++ b/src/calibre/utils/cpp_binding.h @@ -48,7 +48,7 @@ typedef generic_raii pyobject_raii; static inline int py_to_wchar(PyObject *obj, wchar_t **output) { if (!PyUnicode_Check(obj)) { - if (obj == Py_None) { return 1; } + if (obj == Py_None) { *output = NULL; return 1; } PyErr_SetString(PyExc_TypeError, "unicode object expected"); return 0; }