Skip to content

Commit e188ae8

Browse files
committed
time: struct_time: allow construction like a namedtuple, too
Whenever there is more than one argument, delegate the operation to namedtuple_make_new. This allows other circuitpython-compatible idioms, like with keywords time.struct_time(tm_year=2000, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=14, tm_wday=5, tm_yday=5, tm_isdst=-1) with 9 positional arguments, etc. The only vaguely plausible CPython behavior still not permitted in CircuitPython that I found is constructing a timetuple from a length-9 list, a la time.struct_time(list(time.localtime()) Even better, by getting rid of an error message, the build shrinks a tiny bit.
1 parent 8f24ea4 commit e188ae8

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

shared-bindings/time/__init__.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(time_sleep_obj, time_sleep);
8585
#if MICROPY_PY_COLLECTIONS
8686
mp_obj_t struct_time_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
8787
if (n_args != 1 || (kw_args != NULL && kw_args->used > 0)) {
88-
mp_raise_TypeError(translate("time.struct_time() takes exactly 1 argument"));
88+
return namedtuple_make_new(type, n_args, args, kw_args);
8989
}
9090
if (mp_obj_get_type(args[0])->getiter != mp_obj_tuple_getiter || ((mp_obj_tuple_t*) MP_OBJ_TO_PTR(args[0]))->len != 9) {
9191
mp_raise_TypeError(translate("time.struct_time() takes a 9-sequence"));

0 commit comments

Comments
 (0)