Skip to content

Commit 6e54808

Browse files
DJ SuttonDJ Sutton
authored andcommitted
fix TypeError: function takes exactly 3 arguments (2 given) from wait_for_edge
Trying to parse 3 required arguments and then falling back to 2 will set the PyError condition when only two arguments are present even though the wait_for_edge function as a whole succeeds. According to https://docs.python.org/2/c-api/exceptions.html, returning normally after this will lead to undefined behavior the next time the code enters the C API. Fix this by using the PyArg_ParseTuple optional argument feature instead of trying to parse 3 arguments and falling back to 2 if it fails.
1 parent a9353da commit 6e54808

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

source/py_gpio.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -444,10 +444,9 @@ static PyObject *py_wait_for_edge(__attribute__ ((unused)) PyObject *self, PyObj
444444
char error[30];
445445
BBIO_err err;
446446

447-
if (!PyArg_ParseTuple(args, "sii", &channel, &edge, &timeout)){
448-
timeout = -1;
449-
if (!PyArg_ParseTuple(args, "si", &channel, &edge))
450-
return NULL;
447+
timeout = -1;
448+
if (!PyArg_ParseTuple(args, "si|i", &channel, &edge, &timeout)){
449+
return NULL;
451450
}
452451

453452
err = get_gpio_number(channel, &gpio);

0 commit comments

Comments
 (0)