Add a type check to the gamepad module

Make sure that all the arguments passed are indeed DigitalInOut.
This avoids crashes when the users pass something else.
This commit is contained in:
Radomir Dopieralski 2018-03-31 20:41:16 +02:00
parent 3215b85568
commit 6ca4fd82ed
1 changed files with 7 additions and 0 deletions

View File

@ -27,6 +27,7 @@
#include "py/runtime.h"
#include "py/mphal.h"
#include "shared-module/gamepad/GamePad.h"
#include "shared-bindings/digitalio/DigitalInOut.h"
#include "GamePad.h"
@ -96,6 +97,12 @@ STATIC mp_obj_t gamepad_make_new(const mp_obj_type_t *type, size_t n_args,
gamepad_singleton = m_new_obj(gamepad_obj_t);
gamepad_singleton->base.type = &gamepad_type;
}
for (size_t i = 0; i < n_args; ++i) {
if (!MP_OBJ_IS_TYPE(args[i], &digitalio_digitalinout_type)) {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"Expected a %q", digitalio_digitalinout_type.name));
}
}
gamepad_init(n_args, args);
return MP_OBJ_FROM_PTR(gamepad_singleton);
}