Commit a89e4d16 authored by Leandro Lameiro's avatar Leandro Lameiro

Add dict.fromkeys + tests

parent 2c760930
......@@ -201,6 +201,19 @@ Box* dictNonzero(BoxedDict* self) {
return boxBool(self->d.size());
}
Box* dictFromkeys(BoxedDict* self, Box* iterable, Box* default_value) {
RELEASE_ASSERT(self->cls == dict_cls, "");
auto rtn = new BoxedDict();
for (Box* e : iterable->pyElements()) {
dictSetitem(rtn, e, default_value);
}
return rtn;
}
extern "C" Box* dictNew(Box* _cls, BoxedTuple* args, BoxedDict* kwargs) {
if (!isSubclass(_cls->cls, type_cls))
raiseExcHelper(TypeError, "dict.__new__(X): X is not a type object (%s)", getTypeName(_cls)->c_str());
......@@ -309,6 +322,8 @@ void setupDict() {
dict_cls->giveAttr("clear", new BoxedFunction(boxRTFunction((void*)dictClear, NONE, 1)));
dict_cls->giveAttr("copy", new BoxedFunction(boxRTFunction((void*)dictCopy, DICT, 1)));
dict_cls->giveAttr("fromkeys",
new BoxedFunction(boxRTFunction((void*)dictFromkeys, DICT, 3, 1, false, false), { None }));
dict_cls->giveAttr("items", new BoxedFunction(boxRTFunction((void*)dictItems, LIST, 1)));
dict_cls->giveAttr("iteritems",
new BoxedFunction(boxRTFunction((void*)dictIterItems, typeFromClass(dict_iterator_cls), 1)));
......
......@@ -125,3 +125,16 @@ d = {}
d.clear()
print d
# fromkeys
d = {1:2, 3:4}
print sorted(d.fromkeys([1,2]).items())
print sorted(d.fromkeys([]).items())
print sorted(d.fromkeys([3,4], 5).items())
try:
print d.fromkeys()
assert 0
except TypeError, e:
print 'ok'
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