X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=type_decoder.c;h=cf7ec8e7c383b40b502201e4034b4b9b98e0a8de;hb=27a86cefb655ecdfb8d912a5388fe8768b264ff5;hp=95081835038388f5b9aef30b0799d3f9c087f941;hpb=b8c8dc8a649ad0a76bccbfae7198cc9c8cb2ccda;p=libsigrokdecode.git diff --git a/type_decoder.c b/type_decoder.c index 9508183..cf7ec8e 100644 --- a/type_decoder.c +++ b/type_decoder.c @@ -33,7 +33,7 @@ typedef struct { /* This is only used for nicer srd_dbg() output. */ SRD_PRIV const char *output_type_name(unsigned int idx) { - static const char names[][16] = { + static const char *names[] = { "OUTPUT_ANN", "OUTPUT_PYTHON", "OUTPUT_BINARY", @@ -396,6 +396,13 @@ static void release_meta(GVariant *gvar) g_variant_unref(gvar); } +PyDoc_STRVAR(Decoder_put_doc, + "Put an annotation for the specified span of samples.\n" + "\n" + "Arguments: start and end sample number, stream id, annotation data.\n" + "Annotation data's layout depends on the output stream type." +); + static PyObject *Decoder_put(PyObject *self, PyObject *args) { GSList *l; @@ -554,8 +561,12 @@ err: return NULL; } -static PyObject *Decoder_register(PyObject *self, PyObject *args, - PyObject *kwargs) +PyDoc_STRVAR(Decoder_register_doc, + "Register a new output stream." +); + +static PyObject *Decoder_register(PyObject *self, + PyObject *args, PyObject *kwargs) { struct srd_decoder_inst *di; struct srd_pd_output *pdo; @@ -952,6 +963,23 @@ static int set_skip_condition(struct srd_decoder_inst *di, uint64_t count) return SRD_OK; } +PyDoc_STRVAR(Decoder_wait_doc, + "Wait for one or more conditions to occur.\n" + "\n" + "Returns the sample data at the next position where the condition\n" + "is seen. When the optional condition is missing or empty, the next\n" + "sample number is used. The condition can be a dictionary with one\n" + "condition's details, or a list of dictionaries specifying multiple\n" + "conditions of which at least one condition must be true. Dicts can\n" + "contain one or more key/value pairs, all of which must be true for\n" + "the dict's condition to be considered true. The key either is a\n" + "channel index or a keyword, the value is the operation's parameter.\n" + "\n" + "Supported parameters for channel number keys: 'h', 'l', 'r', 'f',\n" + "or 'e' for level or edge conditions. Other supported keywords:\n" + "'skip' to advance over the given number of samples.\n" +); + static PyObject *Decoder_wait(PyObject *self, PyObject *args) { int ret; @@ -1061,6 +1089,27 @@ static PyObject *Decoder_wait(PyObject *self, PyObject *args) /* Signal the main thread that we handled all samples. */ g_cond_signal(&di->handled_all_samples_cond); + /* + * When EOF was provided externally, communicate the + * Python EOFError exception to .decode() and return + * from the .wait() method call. This is motivated by + * the use of Python context managers, so that .decode() + * methods can "close" incompletely accumulated data + * when the sample data is exhausted. + */ + if (di->communicate_eof) { + /* Advance self.samplenum to the (absolute) last sample number. */ + py_samplenum = PyLong_FromUnsignedLongLong(di->abs_cur_samplenum); + PyObject_SetAttrString(di->py_inst, "samplenum", py_samplenum); + Py_DECREF(py_samplenum); + /* Raise an EOFError Python exception. */ + srd_dbg("%s: %s: Raising EOF from wait().", + di->inst_id, __func__); + g_mutex_unlock(&di->data_mutex); + PyErr_SetString(PyExc_EOFError, "samples exhausted"); + goto err; + } + /* * When termination of wait() and decode() was requested, * then exit the loop after releasing the mutex. @@ -1085,6 +1134,14 @@ err: return NULL; } +PyDoc_STRVAR(Decoder_has_channel_doc, + "Check whether input data is supplied for a given channel.\n" + "\n" + "Argument: A channel index.\n" + "Returns: A boolean, True if the channel is connected,\n" + "False if the channel is open (won't see any input data).\n" +); + /** * Return whether the specified channel was supplied to the decoder. * @@ -1100,6 +1157,7 @@ static PyObject *Decoder_has_channel(PyObject *self, PyObject *args) int idx, count; struct srd_decoder_inst *di; PyGILState_STATE gstate; + PyObject *bool_ret; if (!self || !args) return NULL; @@ -1130,7 +1188,9 @@ static PyObject *Decoder_has_channel(PyObject *self, PyObject *args) PyGILState_Release(gstate); - return (di->dec_channelmap[idx] == -1) ? Py_False : Py_True; + bool_ret = (di->dec_channelmap[idx] == -1) ? Py_False : Py_True; + Py_INCREF(bool_ret); + return bool_ret; err: PyGILState_Release(gstate); @@ -1138,16 +1198,26 @@ err: return NULL; } +PyDoc_STRVAR(Decoder_doc, "sigrok Decoder base class"); + static PyMethodDef Decoder_methods[] = { - { "put", Decoder_put, METH_VARARGS, - "Accepts a dictionary with the following keys: startsample, endsample, data" }, - { "register", (PyCFunction)(void(*)(void))Decoder_register, METH_VARARGS|METH_KEYWORDS, - "Register a new output stream" }, - { "wait", Decoder_wait, METH_VARARGS, - "Wait for one or more conditions to occur" }, - { "has_channel", Decoder_has_channel, METH_VARARGS, - "Report whether a channel was supplied" }, - {NULL, NULL, 0, NULL} + { "put", + Decoder_put, METH_VARARGS, + Decoder_put_doc, + }, + { "register", + (PyCFunction)(void(*)(void))Decoder_register, METH_VARARGS | METH_KEYWORDS, + Decoder_register_doc, + }, + { "wait", + Decoder_wait, METH_VARARGS, + Decoder_wait_doc, + }, + { "has_channel", + Decoder_has_channel, METH_VARARGS, + Decoder_has_channel_doc, + }, + ALL_ZERO, }; /** @@ -1161,10 +1231,10 @@ SRD_PRIV PyObject *srd_Decoder_type_new(void) { PyType_Spec spec; PyType_Slot slots[] = { - { Py_tp_doc, "sigrok Decoder base class" }, + { Py_tp_doc, Decoder_doc }, { Py_tp_methods, Decoder_methods }, { Py_tp_new, (void *)&PyType_GenericNew }, - { 0, NULL } + ALL_ZERO, }; PyObject *py_obj; PyGILState_STATE gstate;