Use RAII for device_information

This commit is contained in:
Kovid Goyal 2021-04-21 10:54:27 +05:30
parent 902f25985b
commit 91b7c428d3
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 6 additions and 6 deletions

View File

@ -22,8 +22,7 @@ dealloc(Device* self)
Py_END_ALLOW_THREADS; Py_END_ALLOW_THREADS;
} }
Py_XDECREF(self->device_information); self->device_information = NULL; self->device_information.release();
Py_TYPE(self)->tp_free((PyObject*)self); Py_TYPE(self)->tp_free((PyObject*)self);
} }
@ -37,7 +36,7 @@ init(Device *self, PyObject *args, PyObject *kwds)
if (client_information) { if (client_information) {
self->device = open_device(self->pnp_id.ptr(), client_information); self->device = open_device(self->pnp_id.ptr(), client_information);
if (self->device) { if (self->device) {
self->device_information = get_device_information(self->device, self->bulk_properties); self->device_information.attach(get_device_information(self->device, self->bulk_properties));
if (self->device_information) ret = 0; if (self->device_information) ret = 0;
} }
} }
@ -53,7 +52,7 @@ update_data(Device *self, PyObject *args) {
CComPtr<IPortableDevicePropertiesBulk> bulk_properties; CComPtr<IPortableDevicePropertiesBulk> bulk_properties;
di = get_device_information(self->device, bulk_properties); di = get_device_information(self->device, bulk_properties);
if (di == NULL) return NULL; if (di == NULL) return NULL;
Py_XDECREF(self->device_information); self->device_information = di; self->device_information.attach(di);
Py_RETURN_NONE; Py_RETURN_NONE;
} // }}} } // }}}
@ -171,7 +170,8 @@ static PyMethodDef Device_methods[] = {
// Device.data {{{ // Device.data {{{
static PyObject * static PyObject *
Device_data(Device *self, void *closure) { Device_data(Device *self, void *closure) {
Py_INCREF(self->device_information); return self->device_information; PyObject *ans = self->device_information.ptr();
Py_INCREF(ans); return ans;
} // }}} } // }}}

View File

@ -42,7 +42,7 @@ typedef struct {
// Type-specific fields go here. // Type-specific fields go here.
wchar_raii pnp_id; wchar_raii pnp_id;
CComPtr<IPortableDevice> device; CComPtr<IPortableDevice> device;
PyObject *device_information; pyobject_raii device_information;
CComPtr<IPortableDevicePropertiesBulk> bulk_properties; CComPtr<IPortableDevicePropertiesBulk> bulk_properties;
} Device; } Device;
extern PyTypeObject DeviceType; extern PyTypeObject DeviceType;