time: Fix and better document time.struct_time constructor
INCOMPATIBLE CHANGE: struct_time(1,2,3,4,5,6,7,8,9) is now _rejected_ just as on standad Python. This incorrect constructor was added by me in #2327; I assumed without even checking that the `struct_time` constructor was also compatible with the `namedtuple` constructor, but it is not and has always been rejected by standard Python (checked 2.7 and 3.9) This commit restores the specific error message that we used for this purpose, which was removed in the previous commit either out of laziness or out of trying to reduce unneeded error strings. In this case, the alternate string is too misleading (it refers to arguments, not to sequence elements) so let's put the better message back.
This commit is contained in:
parent
cac71a33fc
commit
3dcd603e39
|
@ -80,20 +80,21 @@ MP_DEFINE_CONST_FUN_OBJ_1(time_sleep_obj, time_sleep);
|
|||
|
||||
#if MICROPY_PY_COLLECTIONS
|
||||
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) {
|
||||
if (n_args != 1 || (kw_args != NULL && kw_args->used > 0)) {
|
||||
return namedtuple_make_new(type, n_args, args, kw_args);
|
||||
}
|
||||
mp_arg_check_num(n_args, kw_args, 1, 1, false);
|
||||
size_t len;
|
||||
mp_obj_t *items;
|
||||
mp_obj_get_array(args[0], &len, &items);
|
||||
if (len != 9) {
|
||||
mp_raise_TypeError(translate("time.struct_time() takes a 9-sequence"));
|
||||
}
|
||||
return namedtuple_make_new(type, len, items, NULL);
|
||||
}
|
||||
|
||||
//| class struct_time:
|
||||
//| def __init__(self, time_tuple: Tuple[int, int, int, int, int, int, int, int, int]) -> None:
|
||||
//| """Structure used to capture a date and time. Note that it takes a tuple!
|
||||
//| def __init__(self, time_tuple: Sequence) -> None:
|
||||
//| """Structure used to capture a date and time. Can be constructed from a `struct_time`, `tuple`, `list`, or `namedtuple` with 9 elements.
|
||||
//|
|
||||
//| :param tuple time_tuple: Tuple of time info: ``(tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst)``
|
||||
//| :param Sequence time_tuple: Sequence of time info: ``(tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst)``
|
||||
//|
|
||||
//| * ``tm_year``: the year, 2017 for example
|
||||
//| * ``tm_mon``: the month, range [1, 12]
|
||||
|
|
Loading…
Reference in New Issue