Skip to content

Commit 79ed58f

Browse files
stinospfalcon
authored andcommitted
py/objnamedtuple: Add _asdict function if OrderedDict is supported
1 parent cada971 commit 79ed58f

4 files changed

Lines changed: 50 additions & 0 deletions

File tree

ports/unix/mpconfigport_coverage.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,4 @@
4444
#undef MICROPY_VFS_FAT
4545
#define MICROPY_VFS_FAT (1)
4646
#define MICROPY_PY_FRAMEBUF (1)
47+
#define MICROPY_PY_COLLECTIONS_NAMEDTUPLE__ASDICT (1)

py/mpconfig.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,11 @@ typedef double mp_float_t;
894894
#define MICROPY_PY_COLLECTIONS_ORDEREDDICT (0)
895895
#endif
896896

897+
// Whether to provide the _asdict function for namedtuple
898+
#ifndef MICROPY_PY_COLLECTIONS_NAMEDTUPLE__ASDICT
899+
#define MICROPY_PY_COLLECTIONS_NAMEDTUPLE__ASDICT (0)
900+
#endif
901+
897902
// Whether to provide "math" module
898903
#ifndef MICROPY_PY_MATH
899904
#define MICROPY_PY_MATH (1)

py/objnamedtuple.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,23 @@ STATIC size_t namedtuple_find_field(const mp_obj_namedtuple_type_t *type, qstr n
5252
return (size_t)-1;
5353
}
5454

55+
#if MICROPY_PY_COLLECTIONS_NAMEDTUPLE__ASDICT
56+
STATIC mp_obj_t namedtuple_asdict(mp_obj_t self_in) {
57+
mp_obj_namedtuple_t *self = MP_OBJ_TO_PTR(self_in);
58+
const qstr *fields = ((mp_obj_namedtuple_type_t*)self->tuple.base.type)->fields;
59+
mp_obj_t dict = mp_obj_new_dict(self->tuple.len);
60+
//make it an OrderedDict
61+
mp_obj_dict_t *dictObj = MP_OBJ_TO_PTR(dict);
62+
dictObj->base.type = &mp_type_ordereddict;
63+
dictObj->map.is_ordered = 1;
64+
for (size_t i = 0; i < self->tuple.len; ++i) {
65+
mp_obj_dict_store(dict, MP_OBJ_NEW_QSTR(fields[i]), self->tuple.items[i]);
66+
}
67+
return dict;
68+
}
69+
MP_DEFINE_CONST_FUN_OBJ_1(namedtuple_asdict_obj, namedtuple_asdict);
70+
#endif
71+
5572
STATIC void namedtuple_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
5673
(void)kind;
5774
mp_obj_namedtuple_t *o = MP_OBJ_TO_PTR(o_in);
@@ -64,6 +81,13 @@ STATIC void namedtuple_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
6481
if (dest[0] == MP_OBJ_NULL) {
6582
// load attribute
6683
mp_obj_namedtuple_t *self = MP_OBJ_TO_PTR(self_in);
84+
#if MICROPY_PY_COLLECTIONS_NAMEDTUPLE__ASDICT
85+
if (attr == MP_QSTR__asdict) {
86+
dest[0] = MP_OBJ_FROM_PTR(&namedtuple_asdict_obj);
87+
dest[1] = self_in;
88+
return;
89+
}
90+
#endif
6791
size_t id = namedtuple_find_field((mp_obj_namedtuple_type_t*)self->tuple.base.type, attr);
6892
if (id == (size_t)-1) {
6993
return;

tests/basics/namedtuple_asdict.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
try:
2+
try:
3+
from collections import namedtuple
4+
except ImportError:
5+
from ucollections import namedtuple
6+
except ImportError:
7+
print("SKIP")
8+
raise SystemExit
9+
10+
t = namedtuple("Tup", ["baz", "foo", "bar"])(3, 2, 5)
11+
12+
try:
13+
t._asdict
14+
except AttributeError:
15+
print("SKIP")
16+
raise SystemExit
17+
18+
d = t._asdict()
19+
print(list(d.keys()))
20+
print(list(d.values()))

0 commit comments

Comments
 (0)