struct gpiod_chip_info *info;
PyObject *type, *ret;
- type = Py_gpiod_GetGlobalType("ChipInfo");
+ type = Py_gpiod_GetModuleAttrString("gpiod.chip_info", "ChipInfo");
if (!type)
return NULL;
info = gpiod_chip_get_info(self->chip);
- if (!info)
+ if (!info) {
+ Py_DECREF(type);
return PyErr_SetFromErrno(PyExc_OSError);
+ }
- ret = PyObject_CallFunction(type, "ssI",
- gpiod_chip_info_get_name(info),
- gpiod_chip_info_get_label(info),
- gpiod_chip_info_get_num_lines(info));
- gpiod_chip_info_free(info);
- return ret;
+ ret = PyObject_CallFunction(type, "ssI",
+ gpiod_chip_info_get_name(info),
+ gpiod_chip_info_get_label(info),
+ gpiod_chip_info_get_num_lines(info));
+ gpiod_chip_info_free(info);
+ Py_DECREF(type);
+ return ret;
}
static PyObject *make_line_info(struct gpiod_line_info *info)
{
- PyObject *type;
+ PyObject *type, *ret;
- type = Py_gpiod_GetGlobalType("LineInfo");
+ type = Py_gpiod_GetModuleAttrString("gpiod.line_info", "LineInfo");
if (!type)
return NULL;
- return PyObject_CallFunction(type, "IsOsiOiiiiOk",
+ ret = PyObject_CallFunction(type, "IsOsiOiiiiOk",
gpiod_line_info_get_offset(info),
gpiod_line_info_get_name(info),
gpiod_line_info_is_used(info) ?
gpiod_line_info_is_debounced(info) ?
Py_True : Py_False,
gpiod_line_info_get_debounce_period_us(info));
+ Py_DECREF(type);
+ return ret;
}
static PyObject *chip_get_line_info(chip_object *self, PyObject *args)
struct gpiod_info_event *event;
struct gpiod_line_info *info;
- type = Py_gpiod_GetGlobalType("InfoEvent");
- if (!type)
- return NULL;
-
Py_BEGIN_ALLOW_THREADS;
event = gpiod_chip_read_info_event(self->chip);
Py_END_ALLOW_THREADS;
return NULL;
}
+ type = Py_gpiod_GetModuleAttrString("gpiod.info_event", "InfoEvent");
+ if (!type) {
+ Py_DECREF(info_obj);
+ gpiod_info_event_free(event);
+ return NULL;
+ }
+
event_obj = PyObject_CallFunction(type, "iKO",
gpiod_info_event_get_event_type(event),
gpiod_info_event_get_timestamp_ns(event),
info_obj);
Py_DECREF(info_obj);
+ Py_DECREF(type);
gpiod_info_event_free(event);
return event_obj;
}
return PyErr_SetFromErrno(exc);
}
-PyObject *Py_gpiod_GetGlobalType(const char *type_name)
+PyObject *Py_gpiod_GetModuleAttrString(const char *modname,
+ const char *attrname)
{
- PyObject *globals;
+ PyObject *module, *attribute;
- globals = PyEval_GetGlobals();
- if (!globals)
+ module = PyImport_ImportModule(modname);
+ if (!module)
return NULL;
- return PyDict_GetItemString(globals, type_name);
+ attribute = PyObject_GetAttrString(module, attrname);
+ Py_DECREF(module);
+
+ return attribute;
}
unsigned int Py_gpiod_PyLongAsUnsignedInt(PyObject *pylong)
if (num_offsets < 0)
return NULL;
- type = Py_gpiod_GetGlobalType("Value");
- if (!type)
- return NULL;
-
iter = PyObject_GetIter(offsets);
if (!iter)
return NULL;
if (ret)
return Py_gpiod_SetErrFromErrno();
+ type = Py_gpiod_GetModuleAttrString("gpiod.line", "Value");
+ if (!type)
+ return NULL;
+
for (pos = 0; pos < num_offsets; pos++) {
val = PyObject_CallFunction(type, "i", self->values[pos]);
- if (!val)
+ if (!val) {
+ Py_DECREF(type);
return NULL;
+ }
ret = PyList_SetItem(values, pos, val);
if (ret) {
Py_DECREF(val);
+ Py_DECREF(type);
return NULL;
}
}
+ Py_DECREF(type);
Py_RETURN_NONE;
}
max_events = 64;
}
- type = Py_gpiod_GetGlobalType("EdgeEvent");
- if (!type)
- return NULL;
-
Py_BEGIN_ALLOW_THREADS;
ret = gpiod_line_request_read_edge_events(self->request,
self->buffer, max_events);
if (!events)
return NULL;
+ type = Py_gpiod_GetModuleAttrString("gpiod.edge_event", "EdgeEvent");
+ if (!type) {
+ Py_DECREF(events);
+ return NULL;
+ }
+
for (i = 0; i < num_events; i++) {
event = gpiod_edge_event_buffer_get_event(self->buffer, i);
if (!event) {
Py_DECREF(events);
+ Py_DECREF(type);
return NULL;
}
gpiod_edge_event_get_line_seqno(event));
if (!event_obj) {
Py_DECREF(events);
+ Py_DECREF(type);
return NULL;
}
if (ret) {
Py_DECREF(event_obj);
Py_DECREF(events);
+ Py_DECREF(type);
return NULL;
}
}
+ Py_DECREF(type);
return events;
}