Commit 9f5bfdfd authored by Kevin Modzelewski's avatar Kevin Modzelewski

dict.__eq__

parent 6f7629b4
......@@ -424,6 +424,38 @@ Box* dictFromkeys(BoxedDict* self, Box* iterable, Box* default_value) {
return rtn;
}
Box* dictEq(BoxedDict* self, Box* _rhs) {
if (!isSubclass(self->cls, dict_cls))
raiseExcHelper(TypeError, "descriptor '__eq__' requires a 'dict' object but received a '%s'",
getTypeName(self));
if (!isSubclass(_rhs->cls, dict_cls))
return NotImplemented;
BoxedDict* rhs = static_cast<BoxedDict*>(_rhs);
if (self->d.size() != rhs->d.size())
return False;
for (const auto& p : self->d) {
auto it = rhs->d.find(p.first);
if (it == rhs->d.end())
return False;
if (!nonzero(compare(p.second, it->second, AST_TYPE::Eq)))
return False;
}
return True;
}
Box* dictNe(BoxedDict* self, Box* _rhs) {
Box* eq = dictEq(self, _rhs);
if (eq == NotImplemented)
return eq;
if (eq == True)
return False;
return True;
}
extern "C" Box* dictNew(Box* _cls, BoxedTuple* args, BoxedDict* kwargs) {
......@@ -596,6 +628,9 @@ void setupDict() {
dict_cls->giveAttr("__init__", new BoxedFunction(boxRTFunction((void*)dictInit, NONE, 1, 0, true, true)));
dict_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)dictRepr, STR, 1)));
dict_cls->giveAttr("__eq__", new BoxedFunction(boxRTFunction((void*)dictEq, BOXED_BOOL, 2)));
dict_cls->giveAttr("__ne__", new BoxedFunction(boxRTFunction((void*)dictNe, BOXED_BOOL, 2)));
dict_cls->giveAttr("__iter__",
new BoxedFunction(boxRTFunction((void*)dictIterKeys, typeFromClass(dict_iterator_cls), 1)));
......
......@@ -213,3 +213,11 @@ print list(d.viewvalues())
print list(d.viewitems())
print 'keys of d: ', keys
print 'viewkeys of d: ', list(viewkeys)
print {} == {}
d1 = {}
d2 = {}
for i in xrange(6):
d1[i] = 5 - i
d2[5 - i] = i
print d1 == d2, d1 != d2
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