Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions py/dict.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,19 @@ func (d StringDict) M__getitem__(key Object) (Object, error) {
return nil, ExceptionNewf(KeyError, "%v", key)
}

func (d StringDict) M__delitem__(key Object) (Object, error) {
str, ok := key.(String)
if !ok {
return nil, ExceptionNewf(KeyError, "%v", key)
}
_, ok = d[string(str)]
if !ok {
return nil, ExceptionNewf(KeyError, "%v", key)
}
delete(d, string(str))
return None, nil
}

func (d StringDict) M__setitem__(key, value Object) (Object, error) {
str, ok := key.(String)
if !ok {
Expand Down
10 changes: 10 additions & 0 deletions py/tests/dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@
assert v == 5.5
assertRaises(TypeError, a.items, 'a')

doc="del"
a = {'hello': 'world', 'hi': 'there'}
del a["hello"]
def doDel(d, key):
del d[key]
assertRaises(KeyError, lambda: doDel(a, "bob"))
assertRaises(KeyError, lambda: doDel(a, 123))
assert not a.__contains__('hello')
assert a.__contains__('hi')

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps add a couple of cases that exercize the 2 ExceptionNewf branches ?

doc="__contain__"
a = {'hello': 'world'}
assert a.__contains__('hello')
Expand Down