Commit bf51b63a authored by Kevin Modzelewski's avatar Kevin Modzelewski

Implement set.__len__, dict.pop

parent d450c321
......@@ -16,6 +16,8 @@
#include "core/stats.h"
#include "core/types.h"
#include "codegen/compvars.h"
#include "runtime/gc_runtime.h"
#include "runtime/objmodel.h"
#include "runtime/types.h"
......@@ -101,6 +103,40 @@ Box* dictSetitem(BoxedDict* self, Box* k, Box* v) {
return None;
}
Box* dictPop2(BoxedDict* self, Box* k) {
assert(self->cls == dict_cls);
auto it = self->d.find(k);
if (it == self->d.end()) {
Box* s = NULL;
try {
s = repr(k);
} catch (Box* e) {
s = NULL;
}
if (s)
raiseExcHelper(KeyError, "%s", static_cast<BoxedString*>(s)->s.c_str());
else
raiseExcHelper(KeyError, "");
}
Box* rtn = it->second;
self->d.erase(it);
return rtn;
}
Box* dictPop3(BoxedDict* self, Box* k, Box* d) {
assert(self->cls == dict_cls);
auto it = self->d.find(k);
if (it == self->d.end())
return d;
Box* rtn = it->second;
self->d.erase(it);
return rtn;
}
void setupDict() {
dict_cls->giveAttr("__name__", boxStrConstant("dict"));
// dict_cls->giveAttr("__len__", new BoxedFunction(boxRTFunction((void*)dictLen, NULL, 1, false)));
......@@ -119,6 +155,10 @@ void setupDict() {
dict_cls->giveAttr("keys", new BoxedFunction(boxRTFunction((void*)dictKeys, NULL, 1, false)));
dict_cls->setattr("iterkeys", dict_cls->peekattr("keys"), NULL, NULL);
CLFunction* pop = boxRTFunction((void*)dictPop2, UNKNOWN, 2, false);
addRTFunction(pop, (void*)dictPop3, UNKNOWN, 3, false);
dict_cls->giveAttr("pop", new BoxedFunction(pop));
dict_cls->giveAttr("__getitem__", new BoxedFunction(boxRTFunction((void*)dictGetitem, NULL, 2, false)));
dict_cls->giveAttr("__setitem__", new BoxedFunction(boxRTFunction((void*)dictSetitem, NULL, 3, false)));
......
......@@ -165,6 +165,11 @@ Box* setIter(BoxedSet* self) {
return new BoxedSetIterator(self);
}
Box* setLen(BoxedSet* self) {
assert(self->cls == set_cls);
return boxInt(self->s.size());
}
} // namespace set
using namespace pyston::set;
......@@ -212,6 +217,8 @@ void setupSet() {
set_cls->giveAttr("__iter__",
new BoxedFunction(boxRTFunction((void*)setIter, typeFromClass(set_iterator_cls), 1, false)));
set_cls->giveAttr("__len__", new BoxedFunction(boxRTFunction((void*)setLen, BOXED_INT, 1, false)));
set_cls->freeze();
}
......
......@@ -9,3 +9,16 @@ for i in xrange(10):
print sorted(d.items())
print sorted(d.values())
print sorted(d.keys())
print d.pop(5, 5)
print sorted(d.items())
print d.pop(5, 5)
print sorted(d.items())
print d.pop(4)
try:
d.pop(5)
assert 0
except KeyError:
print "ok"
print sorted(d.items())
......@@ -15,3 +15,5 @@ print sorted(s2 - s1)
print sorted(s1 ^ s2)
print sorted(s1 & s2)
print sorted(s1 | s2)
print len(set(range(5)))
# expected: fail
# - string iteration
# This should probably be moved into the str_functions test, once it's no longer failing
for c in "hello world":
print repr(c)
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