From 9717fd235d31b576855a0c0061b7834c0b0f70a9 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Tue, 3 May 2022 15:28:42 -0400 Subject: [PATCH 1/2] make Pin hashable --- locale/circuitpython.pot | 2 +- shared-bindings/microcontroller/Pin.c | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index 3bec8b622b..db06147fe0 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -623,7 +623,7 @@ msgstr "" msgid "Buffer length must be a multiple of 512" msgstr "" -#: ports/stm/common-hal/sdioio/SDCard.c +#: ports/stm/common-hal/sdioio/SDCard.c shared-bindings/floppyio/__init__.c msgid "Buffer must be a multiple of 512 bytes" msgstr "" diff --git a/shared-bindings/microcontroller/Pin.c b/shared-bindings/microcontroller/Pin.c index ee8336fa8a..80c8c1d33c 100644 --- a/shared-bindings/microcontroller/Pin.c +++ b/shared-bindings/microcontroller/Pin.c @@ -78,10 +78,28 @@ STATIC void mcu_pin_print(const mp_print_t *print, mp_obj_t self_in, mp_print_ki } } +//| def __hash__(self) -> int: +//| """Returns a hash for the Pin.""" +//| ... +//| +STATIC mp_obj_t mcu_pin_unary_op(mp_unary_op_t op, mp_obj_t self_in) { + switch (op) { + case MP_UNARY_OP_HASH: { + return mp_obj_id(self_in); + } + default: + return MP_OBJ_NULL; // op not supported + } +} + const mp_obj_type_t mcu_pin_type = { { &mp_type_type }, + .flags = MP_TYPE_FLAG_EXTENDED, .name = MP_QSTR_Pin, - .print = mcu_pin_print + .print = mcu_pin_print, + MP_TYPE_EXTENDED_FIELDS( + .unary_op = mcu_pin_unary_op, + ) }; const mcu_pin_obj_t *validate_obj_is_pin(mp_obj_t obj) { From 68acd14342f2e1e60444d5fb9e2dee59dc23a211 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Tue, 3 May 2022 16:50:48 -0400 Subject: [PATCH 2/2] use generic hash routine where possible --- shared-bindings/microcontroller/Pin.c | 22 +++++++--------------- shared-bindings/socketpool/Socket.c | 22 +++++++--------------- shared-bindings/ssl/SSLSocket.c | 22 +++++++--------------- 3 files changed, 21 insertions(+), 45 deletions(-) diff --git a/shared-bindings/microcontroller/Pin.c b/shared-bindings/microcontroller/Pin.c index 80c8c1d33c..e6809d6b72 100644 --- a/shared-bindings/microcontroller/Pin.c +++ b/shared-bindings/microcontroller/Pin.c @@ -43,6 +43,12 @@ //| ... //| +//| def __hash__(self) -> int: +//| """Returns a hash for the Pin.""" +//| ... +//| +// Provided by mp_generic_unary_op(). + static void get_pin_name(const mcu_pin_obj_t *self, qstr *package, qstr *module, qstr *name) { const mp_map_t *board_map = &board_module_globals.map; for (uint8_t i = 0; i < board_map->alloc; i++) { @@ -78,27 +84,13 @@ STATIC void mcu_pin_print(const mp_print_t *print, mp_obj_t self_in, mp_print_ki } } -//| def __hash__(self) -> int: -//| """Returns a hash for the Pin.""" -//| ... -//| -STATIC mp_obj_t mcu_pin_unary_op(mp_unary_op_t op, mp_obj_t self_in) { - switch (op) { - case MP_UNARY_OP_HASH: { - return mp_obj_id(self_in); - } - default: - return MP_OBJ_NULL; // op not supported - } -} - const mp_obj_type_t mcu_pin_type = { { &mp_type_type }, .flags = MP_TYPE_FLAG_EXTENDED, .name = MP_QSTR_Pin, .print = mcu_pin_print, MP_TYPE_EXTENDED_FIELDS( - .unary_op = mcu_pin_unary_op, + .unary_op = mp_generic_unary_op, ) }; diff --git a/shared-bindings/socketpool/Socket.c b/shared-bindings/socketpool/Socket.c index 2dd832f490..bea3cd1624 100644 --- a/shared-bindings/socketpool/Socket.c +++ b/shared-bindings/socketpool/Socket.c @@ -46,6 +46,12 @@ //| recv that do not allocate bytes objects.""" //| +//| def __hash__(self) -> int: +//| """Returns a hash for the Socket.""" +//| ... +//| +// Provided by mp_generic_unary_op(). + //| def __enter__(self) -> Socket: //| """No-op used by Context Managers.""" //| ... @@ -366,20 +372,6 @@ STATIC mp_obj_t socketpool_socket_settimeout(mp_obj_t self_in, mp_obj_t timeout_ } STATIC MP_DEFINE_CONST_FUN_OBJ_2(socketpool_socket_settimeout_obj, socketpool_socket_settimeout); -//| def __hash__(self) -> int: -//| """Returns a hash for the Socket.""" -//| ... -//| -STATIC mp_obj_t socketpool_socket_unary_op(mp_unary_op_t op, mp_obj_t self_in) { - switch (op) { - case MP_UNARY_OP_HASH: { - return mp_obj_id(self_in); - } - default: - return MP_OBJ_NULL; // op not supported - } -} - STATIC const mp_rom_map_elem_t socketpool_socket_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&socketpool_socket___exit___obj) }, @@ -407,6 +399,6 @@ const mp_obj_type_t socketpool_socket_type = { .name = MP_QSTR_Socket, .locals_dict = (mp_obj_dict_t *)&socketpool_socket_locals_dict, MP_TYPE_EXTENDED_FIELDS( - .unary_op = socketpool_socket_unary_op, + .unary_op = mp_generic_unary_op, ) }; diff --git a/shared-bindings/ssl/SSLSocket.c b/shared-bindings/ssl/SSLSocket.c index 9e25d7f25f..630ab28c68 100644 --- a/shared-bindings/ssl/SSLSocket.c +++ b/shared-bindings/ssl/SSLSocket.c @@ -45,6 +45,12 @@ //| recv that do not allocate bytes objects.""" //| +//| def __hash__(self) -> int: +//| """Returns a hash for the Socket.""" +//| ... +//| +// Provided by mp_generic_unary_op(). + //| def __enter__(self) -> SSLSocket: //| """No-op used by Context Managers.""" //| ... @@ -282,20 +288,6 @@ STATIC mp_obj_t ssl_sslsocket_setblocking(mp_obj_t self_in, mp_obj_t blocking) { } STATIC MP_DEFINE_CONST_FUN_OBJ_2(ssl_sslsocket_setblocking_obj, ssl_sslsocket_setblocking); -//| def __hash__(self) -> int: -//| """Returns a hash for the Socket.""" -//| ... -//| -STATIC mp_obj_t ssl_sslsocket_unary_op(mp_unary_op_t op, mp_obj_t self_in) { - switch (op) { - case MP_UNARY_OP_HASH: { - return mp_obj_id(self_in); - } - default: - return MP_OBJ_NULL; // op not supported - } -} - STATIC const mp_rom_map_elem_t ssl_sslsocket_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&ssl_sslsocket___exit___obj) }, @@ -321,6 +313,6 @@ const mp_obj_type_t ssl_sslsocket_type = { .name = MP_QSTR_SSLSocket, .locals_dict = (mp_obj_dict_t *)&ssl_sslsocket_locals_dict, MP_TYPE_EXTENDED_FIELDS( - .unary_op = ssl_sslsocket_unary_op, + .unary_op = mp_generic_unary_op, ) };