diff options
| author | Scott Shawcroft <scott@tannewt.org> | 2017-08-28 13:59:26 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-08-28 13:59:26 -0700 |
| commit | 447a4b1ecd5655357c9daaace03b91355d2e2c6d (patch) | |
| tree | 46ca08a93e9870aab4fbb533843f3c99796cbdf4 /py/objnamedtuple.c | |
| parent | 4e63d55d8d0df6d7600bb684fa90a8cb54708ba8 (diff) | |
| parent | c679c80c710fd4a0e37ad87200d8a721e19727aa (diff) | |
Merge pull request #208 from adafruit/merge-v1.9.2
Merge MicroPython v1.9.2
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); } |
