diff options
| author | Scott Shawcroft <scott.shawcroft@gmail.com> | 2017-01-05 14:00:48 -0800 |
|---|---|---|
| committer | Scott Shawcroft <scott.shawcroft@gmail.com> | 2017-01-05 14:00:48 -0800 |
| commit | e52fbf2e6aa13a34ceae60e8ee6ad2c3a18092ee (patch) | |
| tree | 360d92e97dde7dc475da7c66568bf278d0116f36 /py | |
| parent | 4bf7c6fb6e560200b988e1ba9505d12bb34e8975 (diff) | |
Add time.struct_time support.
Diffstat (limited to 'py')
| -rw-r--r-- | py/objnamedtuple.c | 19 | ||||
| -rw-r--r-- | py/objnamedtuple.h | 55 |
2 files changed, 60 insertions, 14 deletions
diff --git a/py/objnamedtuple.c b/py/objnamedtuple.c index 18931a16c..6f50d6cf9 100644 --- a/py/objnamedtuple.c +++ b/py/objnamedtuple.c @@ -28,22 +28,13 @@ #include <string.h> #include "py/nlr.h" +#include "py/objnamedtuple.h" #include "py/objtuple.h" #include "py/runtime.h" #include "py/objstr.h" #if MICROPY_PY_COLLECTIONS -typedef struct _mp_obj_namedtuple_type_t { - mp_obj_type_t base; - mp_uint_t n_fields; - qstr fields[]; -} mp_obj_namedtuple_type_t; - -typedef struct _mp_obj_namedtuple_t { - mp_obj_tuple_t tuple; -} mp_obj_namedtuple_t; - STATIC mp_uint_t namedtuple_find_field(const mp_obj_namedtuple_type_t *type, qstr name) { for (mp_uint_t i = 0; i < type->n_fields; i++) { if (type->fields[i] == name) { @@ -53,7 +44,7 @@ STATIC mp_uint_t namedtuple_find_field(const mp_obj_namedtuple_type_t *type, qst return -1; } -STATIC void namedtuple_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) { +void namedtuple_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) { (void)kind; mp_obj_namedtuple_t *o = MP_OBJ_TO_PTR(o_in); mp_printf(print, "%q", o->tuple.base.type->name); @@ -61,7 +52,7 @@ STATIC void namedtuple_print(const mp_print_t *print, mp_obj_t o_in, mp_print_ki mp_obj_attrtuple_print_helper(print, fields, &o->tuple); } -STATIC void namedtuple_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { +void namedtuple_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { if (dest[0] == MP_OBJ_NULL) { // load attribute mp_obj_namedtuple_t *self = MP_OBJ_TO_PTR(self_in); @@ -77,7 +68,7 @@ STATIC void namedtuple_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { } } -STATIC mp_obj_t namedtuple_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args) { +mp_obj_t namedtuple_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args) { const mp_obj_namedtuple_type_t *type = (const mp_obj_namedtuple_type_t*)type_in; size_t num_fields = type->n_fields; if (n_args + n_kw != num_fields) { @@ -134,7 +125,7 @@ STATIC mp_obj_t namedtuple_make_new(const mp_obj_type_t *type_in, size_t n_args, return MP_OBJ_FROM_PTR(tuple); } -STATIC const mp_rom_obj_tuple_t namedtuple_base_tuple = {{&mp_type_tuple}, 1, {MP_ROM_PTR(&mp_type_tuple)}}; +const mp_rom_obj_tuple_t namedtuple_base_tuple = {{&mp_type_tuple}, 1, {MP_ROM_PTR(&mp_type_tuple)}}; STATIC mp_obj_t mp_obj_new_namedtuple_type(qstr name, mp_uint_t n_fields, mp_obj_t *fields) { mp_obj_namedtuple_type_t *o = m_new_obj_var(mp_obj_namedtuple_type_t, qstr, n_fields); diff --git a/py/objnamedtuple.h b/py/objnamedtuple.h new file mode 100644 index 000000000..f6621642a --- /dev/null +++ b/py/objnamedtuple.h @@ -0,0 +1,55 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2013, 2014 Damien P. George + * Copyright (c) 2014 Paul Sokolovsky + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include <string.h> + +#include "py/nlr.h" +#include "py/objtuple.h" +#include "py/runtime.h" +#include "py/objstr.h" + +#if MICROPY_PY_COLLECTIONS + +typedef struct _mp_obj_namedtuple_type_t { + mp_obj_type_t base; + mp_uint_t n_fields; + qstr fields[]; +} mp_obj_namedtuple_type_t; + +typedef struct _mp_obj_namedtuple_t { + mp_obj_tuple_t tuple; +} mp_obj_namedtuple_t; + +void namedtuple_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind); + +void namedtuple_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest); + +mp_obj_t namedtuple_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args); + +const mp_rom_obj_tuple_t namedtuple_base_tuple; + +#endif // MICROPY_PY_COLLECTIONS |
