Commit bbbb21d0 authored by Chris Ramstad's avatar Chris Ramstad

Implement dict.__contains__

TODO: When using {} to construct the dict, contains works. However
if the dict type is used, the runtime crashes in various ways (see
tests/dict_contains.py)
parent 141ac41f
......@@ -146,6 +146,11 @@ Box* dictSetdefault(BoxedDict* self, Box* k, Box* v) {
return v;
}
Box* dictContains(BoxedDict* self, Box* k) {
assert(self->cls == dict_cls);
return boxBool(self->d.count(k) != 0);
}
BoxedClass* dict_iterator_cls = NULL;
extern "C" void dictIteratorGCHandler(GCVisitor* v, void* p) {
boxGCHandler(v, p);
......@@ -189,6 +194,7 @@ void setupDict() {
dict_cls->giveAttr("__getitem__", new BoxedFunction(boxRTFunction((void*)dictGetitem, UNKNOWN, 2)));
dict_cls->giveAttr("__setitem__", new BoxedFunction(boxRTFunction((void*)dictSetitem, NONE, 3)));
dict_cls->giveAttr("__contains__", new BoxedFunction(boxRTFunction((void*)dictContains, BOXED_BOOL, 2)));
dict_cls->freeze();
......
# expected: fail
# using {} style works
print 1 in {}
print 1 in {1:1}
print 'a' in {}
print 'a' in {'a': 1}
# using dict fails
print 'a' in dict()
print 'a' in dict(a=1)
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