Commit 87d1620d authored by Kevin Modzelewski's avatar Kevin Modzelewski

tuple.__contains__

parent 6a1d65dd
...@@ -1774,6 +1774,9 @@ Box* compareInternal(Box* lhs, Box* rhs, int op_type, CompareRewriteArgs *rewrit ...@@ -1774,6 +1774,9 @@ Box* compareInternal(Box* lhs, Box* rhs, int op_type, CompareRewriteArgs *rewrit
if (getitem) if (getitem)
ASSERT(isUserDefined(rhs->cls), "%s should probably have a __contains__", getTypeName(rhs)->c_str()); ASSERT(isUserDefined(rhs->cls), "%s should probably have a __contains__", getTypeName(rhs)->c_str());
RELEASE_ASSERT(getitem == NULL, "need to try old iteration protocol"); RELEASE_ASSERT(getitem == NULL, "need to try old iteration protocol");
fprintf(stderr, "TypeError: argument of type '%s' is not iterable\n", getTypeName(rhs)->c_str());
raiseExc();
} }
bool b = nonzero(contained); bool b = nonzero(contained);
......
...@@ -162,10 +162,23 @@ Box* tupleNe(BoxedTuple *self, Box *rhs) { ...@@ -162,10 +162,23 @@ Box* tupleNe(BoxedTuple *self, Box *rhs) {
return _tupleCmp(self, static_cast<BoxedTuple*>(rhs), AST_TYPE::NotEq); return _tupleCmp(self, static_cast<BoxedTuple*>(rhs), AST_TYPE::NotEq);
} }
Box* tupleContains(BoxedTuple* self, Box *elt) {
int size = self->elts.size();
for (int i = 0; i < size; i++) {
Box* e = self->elts[i];
Box* cmp = compareInternal(e, elt, AST_TYPE::Eq, NULL);
bool b = nonzero(cmp);
if (b)
return True;
}
return False;
}
void setupTuple() { void setupTuple() {
tuple_cls->giveAttr("__name__", boxStrConstant("tuple")); tuple_cls->giveAttr("__name__", boxStrConstant("tuple"));
tuple_cls->giveAttr("__getitem__", new BoxedFunction(boxRTFunction((void*)tupleGetitem, NULL, 2, false))); tuple_cls->giveAttr("__getitem__", new BoxedFunction(boxRTFunction((void*)tupleGetitem, NULL, 2, false)));
tuple_cls->giveAttr("__contains__", new BoxedFunction(boxRTFunction((void*)tupleContains, NULL, 2, false)));
tuple_cls->giveAttr("__lt__", new BoxedFunction(boxRTFunction((void*)tupleLt, NULL, 2, false))); tuple_cls->giveAttr("__lt__", new BoxedFunction(boxRTFunction((void*)tupleLt, NULL, 2, false)));
tuple_cls->giveAttr("__le__", new BoxedFunction(boxRTFunction((void*)tupleLe, NULL, 2, false))); tuple_cls->giveAttr("__le__", new BoxedFunction(boxRTFunction((void*)tupleLe, NULL, 2, false)));
......
...@@ -18,3 +18,6 @@ print 2 in [C("")] # False ...@@ -18,3 +18,6 @@ print 2 in [C("")] # False
for i in xrange(1, 4): for i in xrange(1, 4):
print i in range(6), i not in range(5) print i in range(6), i not in range(5)
print i in (1, 2, 5)
t = (1, "h")
print t, str(t), repr(t)
if 1:
t = (3,)
print t
def f():
t = (1, 3)
print t
f()
print ()
print (1,)
print (1, 2)
print (1, 2, 3)
t = 1, 3
print t
print (2,) < (2,)
print (2,) < (2, 3)
print (3,) < (2, 3)
print
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