Use smart pointers for the device object

This commit is contained in:
Kovid Goyal 2021-04-20 20:03:01 +05:30
parent 4d575db0a4
commit e1a133c990
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
4 changed files with 27 additions and 39 deletions

View File

@ -12,15 +12,13 @@ using namespace wpd;
static void
dealloc(Device* self)
{
if (self->pnp_id != NULL) free(self->pnp_id);
self->pnp_id = NULL;
self->pnp_id.release();
if (self->bulk_properties) self->bulk_properties.Release();
if (self->bulk_properties != NULL) { self->bulk_properties->Release(); self->bulk_properties = NULL; }
if (self->device != NULL) {
if (self->device) {
Py_BEGIN_ALLOW_THREADS;
self->device->Close(); self->device->Release();
self->device = NULL;
self->device->Close();
self->device.Release();
Py_END_ALLOW_THREADS;
}
@ -32,28 +30,21 @@ dealloc(Device* self)
static int
init(Device *self, PyObject *args, PyObject *kwds)
{
PyObject *pnp_id;
int ret = -1;
if (!PyArg_ParseTuple(args, "O", &pnp_id)) return -1;
self->pnp_id = unicode_to_wchar(pnp_id);
if (self->pnp_id == NULL) return -1;
self->bulk_properties = NULL;
if (!PyArg_ParseTuple(args, "O&", py_to_wchar_no_none, &self->pnp_id)) return -1;
self->bulk_properties.Release();
CComPtr<IPortableDeviceValues> client_information = get_client_information();
if (client_information) {
self->device = open_device(self->pnp_id, client_information);
if (self->device != NULL) {
self->device_information = get_device_information(self->device, &(self->bulk_properties));
if (self->device_information != NULL) {
ret = 0;
}
self->device = open_device(self->pnp_id.ptr(), client_information);
if (self->device) {
IPortableDevicePropertiesBulk *bulk_properties = NULL;
self->device_information = get_device_information(self->device, &bulk_properties);
if (self->device_information) {
ret = 0;
self->bulk_properties = bulk_properties;
}
}
}
return ret;
}

View File

@ -10,14 +10,13 @@
namespace wpd {
IPortableDeviceValues *get_client_information() { // {{{
IPortableDeviceValues *client_information;
HRESULT hr;
ENSURE_WPD(NULL);
CComPtr<IPortableDeviceValues> client_information;
Py_BEGIN_ALLOW_THREADS;
hr = CoCreateInstance(CLSID_PortableDeviceValues, NULL,
CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&client_information));
hr = client_information.CoCreateInstance(CLSID_PortableDeviceValues, NULL, CLSCTX_INPROC_SERVER);
Py_END_ALLOW_THREADS;
if (FAILED(hr)) { hresult_set_exc("Failed to create IPortableDeviceValues", hr); return NULL; }
@ -43,7 +42,7 @@ IPortableDeviceValues *get_client_information() { // {{{
hr = client_information->SetUnsignedIntegerValue(WPD_CLIENT_SECURITY_QUALITY_OF_SERVICE, SECURITY_IMPERSONATION);
Py_END_ALLOW_THREADS;
if (FAILED(hr)) { hresult_set_exc("Failed to set quality of service", hr); return NULL; }
return client_information;
return client_information.Detach();
} // }}}
IPortableDevice *open_device(const wchar_t *pnp_id, CComPtr<IPortableDeviceValues> &client_information) { // {{{
@ -207,7 +206,8 @@ end:
return ans;
} // }}}
PyObject* get_device_information(IPortableDevice *device, IPortableDevicePropertiesBulk **pb) { // {{{
PyObject*
get_device_information(CComPtr<IPortableDevice> &device, IPortableDevicePropertiesBulk **pb) { // {{{
IPortableDeviceContent *content = NULL;
IPortableDeviceProperties *properties = NULL;
IPortableDevicePropertiesBulk *properties_bulk = NULL;

View File

@ -40,11 +40,10 @@ extern ClientInfo client_info;
typedef struct {
PyObject_HEAD
// Type-specific fields go here.
wchar_t *pnp_id;
IPortableDevice *device;
wchar_raii pnp_id;
CComPtr<IPortableDevice> device;
PyObject *device_information;
IPortableDevicePropertiesBulk *bulk_properties;
CComPtr<IPortableDevicePropertiesBulk> bulk_properties;
} Device;
extern PyTypeObject DeviceType;
@ -56,7 +55,7 @@ int pump_waiting_messages();
extern IPortableDeviceValues* get_client_information();
extern IPortableDevice* open_device(const wchar_t *pnp_id, CComPtr<IPortableDeviceValues> &client_information);
extern PyObject* get_device_information(IPortableDevice *device, IPortableDevicePropertiesBulk **bulk_properties);
extern PyObject* get_device_information(CComPtr<IPortableDevice> &device, IPortableDevicePropertiesBulk **bulk_properties);
extern PyObject* get_filesystem(IPortableDevice *device, const wchar_t *storage_id, IPortableDevicePropertiesBulk *bulk_properties, PyObject *callback);
extern PyObject* get_file(IPortableDevice *device, const wchar_t *object_id, PyObject *dest, PyObject *callback);
extern PyObject* create_folder(IPortableDevice *device, const wchar_t *parent_id, const wchar_t *name);

View File

@ -131,7 +131,7 @@ wpd_enumerate_devices(PyObject *self, PyObject *args) {
static PyObject *
wpd_device_info(PyObject *self, PyObject *args) {
PyObject *ans = NULL;
IPortableDevice *device = NULL;
CComPtr<IPortableDevice> device = NULL;
ENSURE_WPD(NULL);
@ -142,12 +142,10 @@ wpd_device_info(PyObject *self, PyObject *args) {
CComPtr<IPortableDeviceValues> client_information = get_client_information();
if (client_information) {
device = open_device(pnp_id.ptr(), client_information);
if (device != NULL) {
ans = get_device_information(device, NULL);
}
if (device) ans = get_device_information(device, NULL);
}
if (device != NULL) {device->Close(); device->Release();}
if (device) device->Close();
return ans;
} // }}}