Commit 2c760930 authored by Leandro Lameiro's avatar Leandro Lameiro

Add dict.clear + test

parent 56f07b13
...@@ -46,6 +46,12 @@ Box* dictRepr(BoxedDict* self) { ...@@ -46,6 +46,12 @@ Box* dictRepr(BoxedDict* self) {
return boxString(std::string(chars.begin(), chars.end())); return boxString(std::string(chars.begin(), chars.end()));
} }
Box* dictClear(BoxedDict* self) {
RELEASE_ASSERT(self->cls == dict_cls, "");
self->d.clear();
return None;
}
Box* dictCopy(BoxedDict* self) { Box* dictCopy(BoxedDict* self) {
RELEASE_ASSERT(self->cls == dict_cls, ""); RELEASE_ASSERT(self->cls == dict_cls, "");
...@@ -300,6 +306,7 @@ void setupDict() { ...@@ -300,6 +306,7 @@ void setupDict() {
dict_cls->giveAttr("__iter__", dict_cls->giveAttr("__iter__",
new BoxedFunction(boxRTFunction((void*)dictIterKeys, typeFromClass(dict_iterator_cls), 1))); new BoxedFunction(boxRTFunction((void*)dictIterKeys, typeFromClass(dict_iterator_cls), 1)));
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("copy", new BoxedFunction(boxRTFunction((void*)dictCopy, DICT, 1)));
dict_cls->giveAttr("items", new BoxedFunction(boxRTFunction((void*)dictItems, LIST, 1))); dict_cls->giveAttr("items", new BoxedFunction(boxRTFunction((void*)dictItems, LIST, 1)));
......
...@@ -114,3 +114,14 @@ print d2, d ...@@ -114,3 +114,14 @@ print d2, d
d = {} d = {}
print d.__init__((('a', 1), ('b', 2)), b=3) print d.__init__((('a', 1), ('b', 2)), b=3)
print sorted(d.items()) print sorted(d.items())
# clear
d = {1:2, 'a': 'b'}
d.clear()
print d
d = {}
d.clear()
print d
# expected: fail
# (del statement)
# Not sure how important or necessary this behavior is, but # Not sure how important or necessary this behavior is, but
# CPython's behavior around sys.modules seems to be that it saves # CPython's behavior around sys.modules seems to be that it saves
# an internal reference to the object, so if you set sys.modules # an internal reference to the object, so if you set sys.modules
......
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