From 4933fa1c27f987be558a33b1736089b04e880a03 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Tue, 29 Nov 2016 15:49:50 -0800 Subject: shared-bindings: Ensure pin objects are actually pins. Fixes #12 --- shared-bindings/microcontroller/Pin.c | 9 +++++++++ shared-bindings/microcontroller/Pin.h | 2 ++ 2 files changed, 11 insertions(+) (limited to 'shared-bindings/microcontroller') diff --git a/shared-bindings/microcontroller/Pin.c b/shared-bindings/microcontroller/Pin.c index 733fa29ec..c691a9a17 100644 --- a/shared-bindings/microcontroller/Pin.c +++ b/shared-bindings/microcontroller/Pin.c @@ -26,6 +26,9 @@ #include "shared-bindings/microcontroller/Pin.h" +#include "py/nlr.h" +#include "py/obj.h" + //| .. currentmodule:: microcontroller //| //| :class:`Pin` --- Pin reference @@ -44,3 +47,9 @@ const mp_obj_type_t mcu_pin_type = { { &mp_type_type }, .name = MP_QSTR_Pin, }; + +void assert_pin(mp_obj_t obj, bool none_ok) { + if ((obj != mp_const_none || !none_ok) && !MP_OBJ_IS_TYPE(obj, &mcu_pin_type)) { + nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "Expected a Pin")); + } +} diff --git a/shared-bindings/microcontroller/Pin.h b/shared-bindings/microcontroller/Pin.h index 96df67ef8..d31ba040c 100644 --- a/shared-bindings/microcontroller/Pin.h +++ b/shared-bindings/microcontroller/Pin.h @@ -32,4 +32,6 @@ // Type object used in Python. Should be shared between ports. extern const mp_obj_type_t mcu_pin_type; +void assert_pin(mp_obj_t obj, bool none_ok); + #endif // __MICROPY_INCLUDED_SHARED_BINDINGS_MICROCONTROLLER_PIN_H__ -- cgit v1.2.3