Commit 8cefa4d3 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Merge pull request #386 from undingen/virtualenv_fixes5

Make dict.fromkeys a class method
parents 58d1dc1f 50c6e493
......@@ -411,11 +411,7 @@ Box* dictNonzero(BoxedDict* self) {
return boxBool(self->d.size());
}
Box* dictFromkeys(BoxedDict* self, Box* iterable, Box* default_value) {
if (!isSubclass(self->cls, dict_cls))
raiseExcHelper(TypeError, "descriptor 'fromkeys' requires a 'dict' object but received a '%s'",
getTypeName(self));
Box* dictFromkeys(Box* cls, Box* iterable, Box* default_value) {
auto rtn = new BoxedDict();
for (Box* e : iterable->pyElements()) {
dictSetitem(rtn, e, default_value);
......@@ -640,8 +636,6 @@ void setupDict() {
dict_cls->giveAttr("copy", new BoxedFunction(boxRTFunction((void*)dictCopy, DICT, 1)));
dict_cls->giveAttr("has_key", new BoxedFunction(boxRTFunction((void*)dictContains, BOXED_BOOL, 2)));
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)));
......@@ -656,6 +650,8 @@ void setupDict() {
dict_cls->giveAttr("pop", new BoxedFunction(boxRTFunction((void*)dictPop, UNKNOWN, 3, 1, false, false), { NULL }));
dict_cls->giveAttr("popitem", new BoxedFunction(boxRTFunction((void*)dictPopitem, BOXED_TUPLE, 1)));
auto* fromkeys_func = new BoxedFunction(boxRTFunction((void*)dictFromkeys, DICT, 3, 1, false, false), { None });
dict_cls->giveAttr("fromkeys", boxInstanceMethod(dict_cls, fromkeys_func));
dict_cls->giveAttr("viewkeys", new BoxedFunction(boxRTFunction((void*)dictViewKeys, UNKNOWN, 1)));
dict_cls->giveAttr("viewvalues", new BoxedFunction(boxRTFunction((void*)dictViewValues, UNKNOWN, 1)));
......
......@@ -127,11 +127,9 @@ 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())
print sorted(dict.fromkeys([1,2]).items())
print sorted(dict.fromkeys([]).items())
print sorted(dict.fromkeys([3,4], 5).items())
try:
print d.fromkeys()
......
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