Skip to content

Commit 0cc4032

Browse files
authored
py: add __delitem__ to dict
1 parent c60d425 commit 0cc4032

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

py/dict.go

+13
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,19 @@ func (d StringDict) M__getitem__(key Object) (Object, error) {
189189
return nil, ExceptionNewf(KeyError, "%v", key)
190190
}
191191

192+
func (d StringDict) M__delitem__(key Object) (Object, error) {
193+
str, ok := key.(String)
194+
if !ok {
195+
return nil, ExceptionNewf(KeyError, "%v", key)
196+
}
197+
_, ok = d[string(str)]
198+
if !ok {
199+
return nil, ExceptionNewf(KeyError, "%v", key)
200+
}
201+
delete(d, string(str))
202+
return None, nil
203+
}
204+
192205
func (d StringDict) M__setitem__(key, value Object) (Object, error) {
193206
str, ok := key.(String)
194207
if !ok {

py/tests/dict.py

+10
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@
4646
assert v == 5.5
4747
assertRaises(TypeError, a.items, 'a')
4848

49+
doc="del"
50+
a = {'hello': 'world', 'hi': 'there'}
51+
del a["hello"]
52+
def doDel(d, key):
53+
del d[key]
54+
assertRaises(KeyError, lambda: doDel(a, "bob"))
55+
assertRaises(KeyError, lambda: doDel(a, 123))
56+
assert not a.__contains__('hello')
57+
assert a.__contains__('hi')
58+
4959
doc="__contain__"
5060
a = {'hello': 'world'}
5161
assert a.__contains__('hello')

0 commit comments

Comments
 (0)