+ o->values = g_slist_prepend(o->values,
+ g_variant_ref_sink(gvar));
+ }
+ }
+ }
+ d->options = options;
+ Py_DECREF(py_opts);
+ PyGILState_Release(gstate);
+
+ return SRD_OK;
+
+except_out:
+ srd_exception_catch("Failed to get %s decoder options", d->name);
+
+err_out:
+ g_slist_free_full(options, &decoder_option_free);
+ Py_XDECREF(py_opts);
+ PyGILState_Release(gstate);
+
+ return SRD_ERR_PYTHON;
+}
+
+/* Convert annotation class attribute to GSList of char **. */
+static int get_annotations(struct srd_decoder *dec, size_t *ret_count)
+{
+ PyObject *py_annlist, *py_ann;
+ GSList *annotations;
+ char **annpair;
+ ssize_t ann_idx;
+ PyGILState_STATE gstate;
+
+ if (ret_count)
+ *ret_count = 0;
+
+ gstate = PyGILState_Ensure();
+
+ if (!PyObject_HasAttrString(dec->py_dec, "annotations")) {
+ PyGILState_Release(gstate);
+ return SRD_OK;
+ }
+
+ annotations = NULL;
+
+ py_annlist = PyObject_GetAttrString(dec->py_dec, "annotations");
+ if (!py_annlist)
+ goto except_out;
+
+ if (!PyTuple_Check(py_annlist)) {
+ srd_err("Protocol decoder %s annotations should be a tuple.",
+ dec->name);
+ goto err_out;
+ }
+
+ ann_idx = PyTuple_Size(py_annlist);
+ if (ret_count)
+ *ret_count = ann_idx;
+ while (ann_idx--) {
+ py_ann = PyTuple_GetItem(py_annlist, ann_idx);
+ if (!py_ann)
+ goto except_out;
+
+ if (!PyTuple_Check(py_ann) || PyTuple_Size(py_ann) != 2) {
+ srd_err("Protocol decoder %s annotation %zd should be a tuple with two elements.",
+ dec->name, ann_idx + 1);
+ goto err_out;
+ }
+ if (py_strseq_to_char(py_ann, &annpair) != SRD_OK)
+ goto err_out;
+
+ annotations = g_slist_prepend(annotations, annpair);
+ }
+ dec->annotations = annotations;
+ Py_DECREF(py_annlist);
+ PyGILState_Release(gstate);
+
+ return SRD_OK;
+
+except_out:
+ srd_exception_catch("Failed to get %s decoder annotations", dec->name);
+
+err_out:
+ g_slist_free_full(annotations, (GDestroyNotify)&g_strfreev);
+ Py_XDECREF(py_annlist);
+ PyGILState_Release(gstate);
+
+ return SRD_ERR_PYTHON;
+}
+
+/* Convert annotation_rows to GSList of 'struct srd_decoder_annotation_row'. */
+static int get_annotation_rows(struct srd_decoder *dec, size_t cls_count)
+{
+ const char *py_member_name = "annotation_rows";
+
+ PyObject *py_ann_rows, *py_ann_row, *py_ann_classes, *py_item;
+ GSList *annotation_rows;
+ struct srd_decoder_annotation_row *ann_row;
+ ssize_t row_idx, item_idx;
+ size_t class_idx;
+ PyGILState_STATE gstate;
+
+ gstate = PyGILState_Ensure();
+
+ if (!PyObject_HasAttrString(dec->py_dec, py_member_name)) {
+ PyGILState_Release(gstate);
+ return SRD_OK;
+ }
+
+ annotation_rows = NULL;
+
+ py_ann_rows = PyObject_GetAttrString(dec->py_dec, py_member_name);
+ if (!py_ann_rows)
+ goto except_out;
+
+ if (!PyTuple_Check(py_ann_rows)) {
+ srd_err("Protocol decoder %s %s must be a tuple.",
+ dec->name, py_member_name);
+ goto err_out;
+ }
+
+ row_idx = PyTuple_Size(py_ann_rows);
+ while (row_idx--) {
+ py_ann_row = PyTuple_GetItem(py_ann_rows, row_idx);
+ if (!py_ann_row)
+ goto except_out;
+
+ if (!PyTuple_Check(py_ann_row) || PyTuple_Size(py_ann_row) != 3) {
+ srd_err("Protocol decoder %s %s must contain only tuples of 3 elements.",
+ dec->name, py_member_name);
+ goto err_out;
+ }
+ ann_row = g_malloc0(sizeof(struct srd_decoder_annotation_row));
+ /* Add to list right away so it doesn't get lost. */
+ annotation_rows = g_slist_prepend(annotation_rows, ann_row);
+
+ py_item = PyTuple_GetItem(py_ann_row, 0);
+ if (!py_item)
+ goto except_out;
+ if (py_str_as_str(py_item, &ann_row->id) != SRD_OK)
+ goto err_out;
+
+ py_item = PyTuple_GetItem(py_ann_row, 1);
+ if (!py_item)
+ goto except_out;
+ if (py_str_as_str(py_item, &ann_row->desc) != SRD_OK)
+ goto err_out;
+
+ py_ann_classes = PyTuple_GetItem(py_ann_row, 2);
+ if (!py_ann_classes)
+ goto except_out;
+
+ if (!PyTuple_Check(py_ann_classes)) {
+ srd_err("Protocol decoder %s %s tuples must have a tuple of numbers as 3rd element.",
+ dec->name, py_member_name);
+ goto err_out;
+ }
+
+ item_idx = PyTuple_Size(py_ann_classes);
+ while (item_idx--) {
+ py_item = PyTuple_GetItem(py_ann_classes, item_idx);
+ if (!py_item)
+ goto except_out;
+
+ if (!PyLong_Check(py_item)) {
+ srd_err("Protocol decoder %s annotation row class tuple must only contain numbers.",
+ dec->name);
+ goto err_out;
+ }
+ class_idx = PyLong_AsSize_t(py_item);
+ if (PyErr_Occurred())
+ goto except_out;
+ if (class_idx >= cls_count) {
+ srd_err("Protocol decoder %s annotation row %zd references invalid class %zu.",
+ dec->name, row_idx, class_idx);
+ goto err_out;