X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=type_decoder.c;h=cf7ec8e7c383b40b502201e4034b4b9b98e0a8de;hb=27a86cefb655ecdfb8d912a5388fe8768b264ff5;hp=d21e92d5d0df76501c0ceb7722e3089e3be7279d;hpb=11c6995e4bdaf176e1a38b8ee642217cdb98dfda;p=libsigrokdecode.git diff --git a/type_decoder.c b/type_decoder.c index d21e92d..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", @@ -1089,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. @@ -1196,7 +1217,7 @@ static PyMethodDef Decoder_methods[] = { Decoder_has_channel, METH_VARARGS, Decoder_has_channel_doc, }, - {NULL, NULL, 0, NULL} + ALL_ZERO, }; /** @@ -1213,7 +1234,7 @@ SRD_PRIV PyObject *srd_Decoder_type_new(void) { 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;