+ struct sigrokdecode_decoder *d;
+ PyObject *py_name, *py_mod, *py_func, *py_res /* , *py_tuple */;
+ int r;
+
+ /* Get the name of the decoder module as Python string. */
+ if (!(py_name = PyString_FromString(name))) { /* NEWREF */
+ PyErr_Print(); /* Returns void. */
+ return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
+ }
+
+ /* "Import" the Python module. */
+ if (!(py_mod = PyImport_Import(py_name))) { /* NEWREF */
+ PyErr_Print(); /* Returns void. */
+ Py_XDECREF(py_name);
+ return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
+ }
+ Py_XDECREF(py_name);
+
+ /* Get the 'register' function name as Python callable object. */
+ py_func = PyObject_GetAttrString(py_mod, "register"); /* NEWREF */
+ if (!py_func || !PyCallable_Check(py_func)) {
+ if (PyErr_Occurred())
+ PyErr_Print(); /* Returns void. */
+ Py_XDECREF(py_mod);
+ return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
+ }
+
+ /* Call the 'register' function without arguments, get the result. */
+ if (!(py_res = PyObject_CallFunction(py_func, NULL))) { /* NEWREF */
+ PyErr_Print(); /* Returns void. */
+ Py_XDECREF(py_func);
+ Py_XDECREF(py_mod);
+ return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
+ }
+
+ if (!(d = malloc(sizeof(struct sigrokdecode_decoder))))
+ return SIGROKDECODE_ERR_MALLOC;
+
+ if ((r = h_str(py_res, py_func, py_mod, "id", &(d->id))) < 0)
+ return r;
+
+ if ((r = h_str(py_res, py_func, py_mod, "name", &(d->name))) < 0)
+ return r;
+
+ if ((r = h_str(py_res, py_func, py_mod, "desc", &(d->desc))) < 0)
+ return r;
+
+ d->py_mod = py_mod;
+
+ Py_XDECREF(py_res);
+ Py_XDECREF(py_func);
+
+ /* Get the 'decode' function name as Python callable object. */
+ py_func = PyObject_GetAttrString(py_mod, "decode"); /* NEWREF */
+ if (!py_func || !PyCallable_Check(py_func)) {
+ if (PyErr_Occurred())
+ PyErr_Print(); /* Returns void. */
+ Py_XDECREF(py_mod);
+ return SIGROKDECODE_ERR_PYTHON; /* TODO: More specific error? */
+ }
+
+ d->py_func = py_func;
+
+ /* TODO: Handle inputformats, outputformats. */
+
+ *dec = d;
+
+ return SIGROKDECODE_OK;