+ /* The first element should be an integer. */
+ py_tmp = PyList_GetItem(obj, 0);
+ if (!PyLong_Check(py_tmp)) {
+ srd_err("Protocol decoder %s submitted SRD_OUTPUT_BINARY list, "
+ "but first element was not an integer.", di->decoder->name);
+ return SRD_ERR_PYTHON;
+ }
+ bin_class = PyLong_AsLong(py_tmp);
+ if (!(class_name = g_slist_nth_data(di->decoder->binary, bin_class))) {
+ srd_err("Protocol decoder %s submitted SRD_OUTPUT_BINARY with "
+ "unregistered binary class %d.", di->decoder->name, bin_class);
+ return SRD_ERR_PYTHON;
+ }
+
+ /* Second element should be bytes. */
+ py_tmp = PyList_GetItem(obj, 1);
+ if (!PyBytes_Check(py_tmp)) {
+ srd_err("Protocol decoder %s submitted SRD_OUTPUT_BINARY list, "
+ "but second element was not bytes.", di->decoder->name);
+ return SRD_ERR_PYTHON;
+ }
+
+ /* Consider an empty set of bytes a bug. */
+ if (PyBytes_Size(py_tmp) == 0) {
+ srd_err("Protocol decoder %s submitted SRD_OUTPUT_BINARY "
+ "with empty data set.", di->decoder->name);
+ return SRD_ERR_PYTHON;
+ }
+
+ pdb = g_malloc(sizeof(struct srd_proto_data_binary));
+ if (PyBytes_AsStringAndSize(py_tmp, &buf, &size) == -1)
+ return SRD_ERR_PYTHON;
+ pdb->bin_class = bin_class;
+ pdb->size = size;
+ if (!(pdb->data = g_try_malloc(pdb->size)))
+ return SRD_ERR_MALLOC;
+ memcpy((void *)pdb->data, (const void *)buf, pdb->size);
+ pdata->data = pdb;
+
+ return SRD_OK;
+}
+
+static int convert_meta(struct srd_proto_data *pdata, PyObject *obj)
+{
+ long long intvalue;
+ double dvalue;
+
+ if (pdata->pdo->meta_type == G_VARIANT_TYPE_INT64) {
+ if (!PyLong_Check(obj)) {
+ PyErr_Format(PyExc_TypeError, "This output was registered "
+ "as 'int', but something else was passed.");
+ return SRD_ERR_PYTHON;
+ }
+ intvalue = PyLong_AsLongLong(obj);
+ if (PyErr_Occurred())
+ return SRD_ERR_PYTHON;
+ pdata->data = g_variant_new_int64(intvalue);
+ } else if (pdata->pdo->meta_type == G_VARIANT_TYPE_DOUBLE) {
+ if (!PyFloat_Check(obj)) {
+ PyErr_Format(PyExc_TypeError, "This output was registered "
+ "as 'float', but something else was passed.");
+ return SRD_ERR_PYTHON;
+ }
+ dvalue = PyFloat_AsDouble(obj);
+ if (PyErr_Occurred())
+ return SRD_ERR_PYTHON;
+ pdata->data = g_variant_new_double(dvalue);
+ }
+