diff options
| author | Dan Halbert <halbert@halwitz.org> | 2017-08-25 22:17:07 -0400 |
|---|---|---|
| committer | Dan Halbert <halbert@halwitz.org> | 2017-08-25 22:17:07 -0400 |
| commit | ef61b5ecb59e9b4e37e3e3c023c62d583c23eb16 (patch) | |
| tree | 45a798fbed0dc673304597b29e0b60174195ce79 /py/objnamedtuple.c | |
| parent | 266be307770abe11c220eb493388807920914b7a (diff) | |
| parent | 1f78e7a43130acfa4bedf16c1007a1b0f37c75c3 (diff) | |
Initial merge of micropython v1.9.2 into circuitpython 2.0.0 (in development) master.
cpx build compiles and loads and works in repl; test suite not run yet
esp8266 not tested yet
Diffstat (limited to 'py/objnamedtuple.c')
| -rw-r--r-- | py/objnamedtuple.c | 56 |
1 files changed, 25 insertions, 31 deletions
diff --git a/py/objnamedtuple.c b/py/objnamedtuple.c index ab343ef63..2288fa7b6 100644 --- a/py/objnamedtuple.c +++ b/py/objnamedtuple.c @@ -1,5 +1,5 @@ /* - * This file is part of the Micro Python project, http://micropython.org/ + * This file is part of the MicroPython project, http://micropython.org/ * * The MIT License (MIT) * @@ -85,43 +85,37 @@ mp_obj_t namedtuple_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t } } - mp_obj_t *arg_objects; - if (n_args == num_fields) { - arg_objects = (mp_obj_t*)args; - } else { - size_t alloc_size = sizeof(mp_obj_t) * num_fields; - arg_objects = alloca(alloc_size); - memset(arg_objects, 0, alloc_size); + // Create a tuple and set the type to this namedtuple + mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(mp_obj_new_tuple(num_fields, NULL)); + tuple->base.type = type_in; - for (size_t i = 0; i < n_args; i++) { - arg_objects[i] = args[i]; - } + // Copy the positional args into the first slots of the namedtuple + memcpy(&tuple->items[0], args, sizeof(mp_obj_t) * n_args); - for (size_t i = n_args; i < n_args + 2 * n_kw; i += 2) { - qstr kw = mp_obj_str_get_qstr(args[i]); - size_t id = namedtuple_find_field(type, kw); - if (id == (size_t)-1) { - if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { - mp_arg_error_terse_mismatch(); - } else { - mp_raise_TypeError_varg( - "unexpected keyword argument '%q'", kw); - } + // Fill in the remaining slots with the keyword args + memset(&tuple->items[n_args], 0, sizeof(mp_obj_t) * n_kw); + for (size_t i = n_args; i < n_args + 2 * n_kw; i += 2) { + qstr kw = mp_obj_str_get_qstr(args[i]); + size_t id = namedtuple_find_field(type, kw); + if (id == (size_t)-1) { + if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { + mp_arg_error_terse_mismatch(); + } else { + mp_raise_TypeError_varg( + "unexpected keyword argument '%q'", kw); } - if (arg_objects[id] != MP_OBJ_NULL) { - if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { - mp_arg_error_terse_mismatch(); - } else { - mp_raise_TypeError_varg( - "function got multiple values for argument '%q'", kw); - } + } + if (tuple->items[id] != MP_OBJ_NULL) { + if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { + mp_arg_error_terse_mismatch(); + } else { + mp_raise_TypeError_varg( + "function got multiple values for argument '%q'", kw); } - arg_objects[id] = args[i + 1]; } + tuple->items[id] = args[i + 1]; } - mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(mp_obj_new_tuple(num_fields, arg_objects)); - tuple->base.type = type_in; return MP_OBJ_FROM_PTR(tuple); } |
