summaryrefslogtreecommitdiff
path: root/type_decoder.c
diff options
context:
space:
mode:
authorGerhard Sittig <gerhard.sittig@gmx.net>2017-12-17 18:50:25 +0100
committerUwe Hermann <uwe@hermann-uwe.de>2018-03-31 20:44:21 +0200
commitcd8d918846a00847f2e8e15ba1642882c34a1ff3 (patch)
tree38f65383d061d1e17b0eabcb3a9d46eadd457ac5 /type_decoder.c
parent63bbdb445fc2ade82c15766b419c869a41ed8f38 (diff)
downloadlibsigrokdecode-cd8d918846a00847f2e8e15ba1642882c34a1ff3.tar.gz
libsigrokdecode-cd8d918846a00847f2e8e15ba1642882c34a1ff3.zip
decoder: rephrase .has_channel() argument parse logic
Have the Python C API check the argument type and do the type conversion already. Raise an IndexError exception when the range check fails.
Diffstat (limited to 'type_decoder.c')
-rw-r--r--type_decoder.c27
1 files changed, 11 insertions, 16 deletions
diff --git a/type_decoder.c b/type_decoder.c
index 0d21951..7c71132 100644
--- a/type_decoder.c
+++ b/type_decoder.c
@@ -855,9 +855,8 @@ err:
*/
static PyObject *Decoder_has_channel(PyObject *self, PyObject *args)
{
- int idx, max_idx;
+ int idx, count;
struct srd_decoder_inst *di;
- PyObject *py_channel;
PyGILState_STATE gstate;
if (!self || !args)
@@ -870,24 +869,20 @@ static PyObject *Decoder_has_channel(PyObject *self, PyObject *args)
goto err;
}
- /* Parse the argument of self.has_channel() into 'py_channel'. */
- if (!PyArg_ParseTuple(args, "O", &py_channel)) {
+ /*
+ * Get the integer argument of self.has_channel(). Check for
+ * the range of supported PD input channel numbers.
+ */
+ if (!PyArg_ParseTuple(args, "i", &idx)) {
/* Let Python raise this exception. */
goto err;
}
- if (!PyLong_Check(py_channel)) {
- PyErr_SetString(PyExc_Exception, "channel index not a number");
- goto err;
- }
-
- idx = PyLong_AsLong(py_channel);
- max_idx = g_slist_length(di->decoder->channels)
- + g_slist_length(di->decoder->opt_channels) - 1;
-
- if (idx < 0 || idx > max_idx) {
- srd_err("Invalid channel index %d/%d.", idx, max_idx);
- PyErr_SetString(PyExc_Exception, "invalid channel");
+ count = g_slist_length(di->decoder->channels) +
+ g_slist_length(di->decoder->opt_channels);
+ if (idx < 0 || idx >= count) {
+ srd_err("Invalid index %d, PD channel count %d.", idx, count);
+ PyErr_SetString(PyExc_IndexError, "invalid channel index");
goto err;
}