Skip to content

Commit

Permalink
added list.pop() in storm (#2027)
Browse files Browse the repository at this point in the history
Added pop method to list object in storm
  • Loading branch information
invisig0th authored Dec 26, 2020
1 parent 95e1612 commit d3ca483
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
14 changes: 14 additions & 0 deletions synapse/lib/stormtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2531,6 +2531,7 @@ def __init__(self, valu, path=None):
def getObjLocals(self):
return {
'has': self._methListHas,
'pop': self._methListPop,
'size': self._methListSize,
'index': self._methListIndex,
'length': self._methListLength,
Expand Down Expand Up @@ -2575,7 +2576,20 @@ async def _methListHas(self, valu):

return prim in self.valu

async def _methListPop(self):
'''
Pop and return the last entry in the list.
'''
try:
return self.valu.pop()
except IndexError:
mesg = 'The list is empty. Nothing to pop.'
raise s_exc.StormRuntimeError(mesg=mesg)

async def _methListAppend(self, valu):
'''
Append a value to the list.
'''
self.valu.append(valu)

async def _methListIndex(self, valu):
Expand Down
3 changes: 3 additions & 0 deletions synapse/tests/test_lib_stormtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,9 @@ async def test_storm_lib_list(self):

self.eq('bar', await core.callStorm('$foo = (foo, bar) return($foo.1)'))
self.eq('foo', await core.callStorm('$foo = (foo, bar) return($foo."-2")'))
self.eq('bar', await core.callStorm('$foo = (foo, bar) return($foo.pop())'))
with self.raises(s_exc.StormRuntimeError):
await core.callStorm('$lib.list().pop()')

async def test_storm_lib_fire(self):
async with self.getTestCore() as core:
Expand Down

0 comments on commit d3ca483

Please sign in to comment.