Skip to content

Commit

Permalink
use generic hash routine where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
dhalbert committed May 3, 2022
1 parent 9717fd2 commit f44b6be
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 45 deletions.
23 changes: 8 additions & 15 deletions shared-bindings/microcontroller/Pin.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down Expand Up @@ -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,
)
};

Expand Down Expand Up @@ -202,3 +194,4 @@ void validate_pins(qstr what, uint8_t *pin_nos, mp_int_t max_pins, mp_obj_t seq,
pin_nos[i] = common_hal_mcu_pin_number(pins[i]);
}
}
<
22 changes: 7 additions & 15 deletions shared-bindings/socketpool/Socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
//| ...
Expand Down Expand Up @@ -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) },
Expand Down Expand Up @@ -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,
)
};
22 changes: 7 additions & 15 deletions shared-bindings/ssl/SSLSocket.c
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
//| ...
Expand Down Expand Up @@ -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) },
Expand All @@ -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,
)
};

0 comments on commit f44b6be

Please sign in to comment.