summaryrefslogtreecommitdiff
path: root/py/objtuple.c
diff options
context:
space:
mode:
authorPaul Sokolovsky <pfalcon@users.sourceforge.net>2014-05-11 03:16:04 +0300
committerPaul Sokolovsky <pfalcon@users.sourceforge.net>2014-05-11 03:33:19 +0300
commitea9708092e8a49377a465ef8c8500943fe9ba772 (patch)
tree90e5dd9cf1ab38518df12410225b9f4c94066e45 /py/objtuple.c
parent9511f60f016547ab00f634d451c230351bd8b225 (diff)
objtuple: Go out of the way to support comparison of subclasses.
Two things are handled here: allow to compare native subtypes of tuple, e.g. namedtuple (TODO: should compare type too, currently compared duck-typedly by content). Secondly, allow user sunclasses of tuples (and its subtypes) be compared either. "Magic" I did previously in objtype.c covers only one argument (lhs is many), so we're in trouble when lhs is native type - there's no other option besides handling rhs in special manner. Fortunately, this patch outlines approach with fast path for native types.
Diffstat (limited to 'py/objtuple.c')
-rw-r--r--py/objtuple.c14
1 files changed, 11 insertions, 3 deletions
diff --git a/py/objtuple.c b/py/objtuple.c
index 7d4e87755..ca65b28e3 100644
--- a/py/objtuple.c
+++ b/py/objtuple.c
@@ -99,12 +99,20 @@ mp_obj_t mp_obj_tuple_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const m
// Don't pass MP_BINARY_OP_NOT_EQUAL here
STATIC bool tuple_cmp_helper(int op, mp_obj_t self_in, mp_obj_t another_in) {
- assert(MP_OBJ_IS_TYPE(self_in, &mp_type_tuple));
- if (!MP_OBJ_IS_TYPE(another_in, &mp_type_tuple)) {
- return false;
+ mp_obj_type_t *self_type = mp_obj_get_type(self_in);
+ if (self_type->getiter != tuple_getiter) {
+ assert(0);
}
+ mp_obj_type_t *another_type = mp_obj_get_type(another_in);
mp_obj_tuple_t *self = self_in;
mp_obj_tuple_t *another = another_in;
+ if (another_type->getiter != tuple_getiter) {
+ // Slow path for user subclasses
+ another = mp_instance_cast_to_native_base(another, &mp_type_tuple);
+ if (another == MP_OBJ_NULL) {
+ return false;
+ }
+ }
return mp_seq_cmp_objs(op, self->items, self->len, another->items, another->len);
}