Commit e74f52b7 authored by Paul Sokolovsky's avatar Paul Sokolovsky
Browse files

namedtuple: Inherit unary/binary ops from tuple base class.

parent d86d22e1
......@@ -127,8 +127,8 @@ mp_obj_t mp_obj_new_namedtuple_type(qstr name, const char *fields) {
o->base.name = name;
o->base.print = namedtuple_print;
o->base.make_new = namedtuple_make_new;
// o->base.unary_op = ;
// o->base.binary_op = ;
o->base.unary_op = tuple_unary_op;
o->base.binary_op = tuple_binary_op;
o->base.load_attr = namedtuple_load_attr;
o->base.store_attr = namedtuple_store_attr;
o->base.bases_tuple = (mp_obj_t)&namedtuple_base_tuple;
......
......@@ -86,7 +86,7 @@ STATIC bool tuple_cmp_helper(int op, mp_obj_t self_in, mp_obj_t another_in) {
return mp_seq_cmp_objs(op, self->items, self->len, another->items, another->len);
}
STATIC mp_obj_t tuple_unary_op(int op, mp_obj_t self_in) {
mp_obj_t tuple_unary_op(int op, mp_obj_t self_in) {
mp_obj_tuple_t *self = self_in;
switch (op) {
case RT_UNARY_OP_BOOL: return MP_BOOL(self->len != 0);
......@@ -95,7 +95,7 @@ STATIC mp_obj_t tuple_unary_op(int op, mp_obj_t self_in) {
}
}
STATIC mp_obj_t tuple_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
mp_obj_t tuple_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
mp_obj_tuple_t *o = lhs;
switch (op) {
case RT_BINARY_OP_SUBSCR:
......@@ -116,7 +116,7 @@ STATIC mp_obj_t tuple_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
}
case RT_BINARY_OP_ADD:
{
if (!MP_OBJ_IS_TYPE(rhs, &tuple_type)) {
if (!mp_obj_is_subclass_fast(mp_obj_get_type(rhs), (mp_obj_t)&tuple_type)) {
return NULL;
}
mp_obj_tuple_t *p = rhs;
......
......@@ -5,3 +5,5 @@ typedef struct _mp_obj_tuple_t {
} mp_obj_tuple_t;
void tuple_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind);
mp_obj_t tuple_unary_op(int op, mp_obj_t self_in);
mp_obj_t tuple_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs);
......@@ -5,8 +5,14 @@ T = namedtuple("Tup", "foo bar")
#print(T)
t = T(1, 2)
print(t)
print(t[0], t[1])
print(t.foo, t.bar)
print(len(t))
print(bool(t))
print(t + t)
print(t * 3)
print(isinstance(t, tuple))
try:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment