Commit f28dda56 authored by Kevin Modzelewski's avatar Kevin Modzelewski

dict.__delitem__, and fix a bug where delitems weren't interacting with exceptions properly

parent a262a1d1
......@@ -1436,8 +1436,7 @@ private:
emitter.createPatchpoint(pp, (void*)pyston::delitem, llvm_args, exc_info);
} else {
emitter.getBuilder()->CreateCall2(g.funcs.delitem, converted_target->getValue(),
converted_slice->getValue());
emitter.createCall2(exc_info, g.funcs.delitem, converted_target->getValue(), converted_slice->getValue());
}
converted_target->decvref(emitter);
......
......@@ -119,6 +119,24 @@ Box* dictSetitem(BoxedDict* self, Box* k, Box* v) {
return None;
}
Box* dictDelitem(BoxedDict* self, Box* k) {
assert(self->cls == dict_cls);
auto it = self->d.find(k);
if (it == self->d.end()) {
BoxedString* s = reprOrNull(k);
if (s)
raiseExcHelper(KeyError, "%s", s->s.c_str());
else
raiseExcHelper(KeyError, "");
}
self->d.erase(it);
return None;
}
Box* dictPop(BoxedDict* self, Box* k, Box* d) {
assert(self->cls == dict_cls);
......@@ -268,6 +286,7 @@ void setupDict() {
dict_cls->giveAttr("__getitem__", new BoxedFunction(boxRTFunction((void*)dictGetitem, UNKNOWN, 2)));
dict_cls->giveAttr("__setitem__", new BoxedFunction(boxRTFunction((void*)dictSetitem, NONE, 3)));
dict_cls->giveAttr("__delitem__", new BoxedFunction(boxRTFunction((void*)dictDelitem, UNKNOWN, 2)));
dict_cls->giveAttr("__contains__", new BoxedFunction(boxRTFunction((void*)dictContains, BOXED_BOOL, 2)));
dict_cls->giveAttr("__nonzero__", new BoxedFunction(boxRTFunction((void*)dictNonzero, BOXED_BOOL, 1)));
......
......@@ -72,29 +72,32 @@ print sorted(dict((('a', 1), ('b', 2)), b=3)) == sorted(dict((['a', 1], ('b', 3)
try:
print dict([1,2], [2,3])
except TypeError, e:
print 'dict expected at most 1 arg'
else:
raise
print e
try:
print dict([(1,2), 42])
except TypeError, e:
print 'cannot convert dictionary update sequence element #1 to a sequence'
else:
raise
print e
try:
# invalid tuple len
print dict([(10,20), (1,2,3)])
except TypeError, e:
print 'dictionary update sequence element #1 has length 3; 2 is required'
else:
raise
except ValueError, e:
print e
try:
# invalid list len
print dict([[10,20], [1,2,3]])
except TypeError, e:
print 'dictionary update sequence element #1 has length 3; 2 is required'
else:
raise
except ValueError, e:
print e
d = {i:i**2 for i in xrange(10)}
print sorted(d.items())
del d[2]
print d.__delitem__(4)
print sorted(d.items())
try:
del d[2]
except KeyError, e:
print e
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