Skip to content

Commit 9d16496

Browse files
committed
localtime: don't hard-fault on argument type errors; handle localtime(float)
It turns out `mp_obj_int_get_checked` is not appropriate to call when the argument is not of int or long type--the "checked" refers to guarding against overflow/underflow, not type checking. For compatibility with CPython, handle float arguments. Closes: adafruit#2069
1 parent 85d7398 commit 9d16496

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

shared-bindings/time/__init__.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,12 @@ STATIC mp_obj_t time_localtime(size_t n_args, const mp_obj_t *args) {
236236
return rtc_get_time_source_time();
237237
}
238238

239-
mp_int_t secs = mp_obj_int_get_checked(args[0]);
239+
mp_obj_t arg = args[0];
240+
if(mp_obj_is_float(arg))
241+
arg = mp_obj_new_int_from_float(mp_obj_get_float(arg));
242+
243+
mp_int_t secs = mp_obj_get_int(arg);
244+
240245
if (secs < EPOCH1970_EPOCH2000_DIFF_SECS)
241246
mp_raise_msg(&mp_type_OverflowError, translate("timestamp out of range for platform time_t"));
242247

0 commit comments

Comments
 (0)