Skip to content

Commit 1fb3d24

Browse files
authored
Merge pull request #3958 from dhalbert/sleep_memory-len-and-bool
restore len(alarm.sleep_memory) and bool(alarm.sleep_memory)
2 parents b0eb91d + dd10c53 commit 1fb3d24

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

shared-bindings/alarm/SleepMemory.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,25 @@
5353
//| """Not used. Access the sole instance through `alarm.sleep_memory`."""
5454
//| ...
5555
//|
56+
//| def __bool__(self) -> bool:
57+
//| """``sleep_memory`` is ``True`` if its length is greater than zero.
58+
//| This is an easy way to check for its existence.
59+
//| """
60+
//| ...
61+
//|
62+
//| def __len__(self) -> int:
63+
//| """Return the length. This is used by (`len`)"""
64+
//| ...
65+
//|
66+
STATIC mp_obj_t alarm_sleep_memory_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
67+
alarm_sleep_memory_obj_t *self = MP_OBJ_TO_PTR(self_in);
68+
uint16_t len = common_hal_alarm_sleep_memory_get_length(self);
69+
switch (op) {
70+
case MP_UNARY_OP_BOOL: return mp_obj_new_bool(len != 0);
71+
case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(len);
72+
default: return MP_OBJ_NULL; // op not supported
73+
}
74+
}
5675

5776
STATIC const mp_rom_map_elem_t alarm_sleep_memory_locals_dict_table[] = {
5877
};
@@ -154,6 +173,7 @@ const mp_obj_type_t alarm_sleep_memory_type = {
154173
{ &mp_type_type },
155174
.name = MP_QSTR_SleepMemory,
156175
.subscr = alarm_sleep_memory_subscr,
176+
.unary_op = alarm_sleep_memory_unary_op,
157177
.print = NULL,
158178
.locals_dict = (mp_obj_t)&alarm_sleep_memory_locals_dict,
159179
};

0 commit comments

Comments
 (0)