Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Adding more exception and testing to dict del command
  • Loading branch information
kellrott committed Jan 13, 2023
commit c89f95cc09a80afe30315a392ee7066303ebb2e1
15 changes: 8 additions & 7 deletions py/dict.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,15 @@ func (d StringDict) M__getitem__(key Object) (Object, error) {

func (d StringDict) M__delitem__(key Object) (Object, error) {
str, ok := key.(String)
if ok {
_, ok := d[string(str)]
if ok {
delete(d, string(str))
return None, nil
}
if !ok {
return nil, ExceptionNewf(KeyError, "%v", key)
}
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) {
Expand Down
4 changes: 4 additions & 0 deletions py/tests/dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
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
Copy Markdown
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 ?

Expand Down