Skip to content

Commit

Permalink
Refactor StormCtrlFlow implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
vEpiphyte committed Dec 26, 2024
1 parent 32a59bc commit 2172afa
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
31 changes: 22 additions & 9 deletions synapse/lib/stormctrl.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
class StormCtrlFlow(Exception):
def __init__(self, item=None, **info):
self.item = item
'''
Base class all StormCtlFlow exceptions derive from.
'''

class _SynErrMixin:
'''
An exception mixin to give some control flow classes functionality like SynErr
'''
def __init__(self, **info):
self.errinfo = info
Exception.__init__(self, self._getExcMsg())

Expand Down Expand Up @@ -56,19 +63,25 @@ def update(self, items):
self.errinfo[k] = v
self._setExcMesg()

class StormLoopCtrl(Exception):
class StormLoopCtrl(_SynErrMixin):
# Control flow statements for WHILE and FOR loop control
statement = ''

class StormGenrCtrl(Exception):
class StormGenrCtrl(_SynErrMixin):
# Control flow statements for GENERATOR control
statement = ''

class StormExit(StormCtrlFlow): pass
class StormStop(StormCtrlFlow, StormGenrCtrl):
class StormStop(StormGenrCtrl, StormCtrlFlow):
statement = 'stop'
class StormBreak(StormCtrlFlow, StormLoopCtrl):

class StormBreak(StormLoopCtrl, StormCtrlFlow):
statement = 'break'
class StormReturn(StormCtrlFlow): pass
class StormContinue(StormCtrlFlow, StormLoopCtrl):

class StormContinue(StormLoopCtrl, StormCtrlFlow):
statement = 'continue'

class StormExit(_SynErrMixin, StormCtrlFlow): pass

class StormReturn(StormCtrlFlow):
def __init__(self, item=None):
self.item = item
2 changes: 1 addition & 1 deletion synapse/lib/stormtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1663,7 +1663,7 @@ async def _exit(self, mesg=None, **kwargs):
if mesg:
mesg = await self._get_mesg(mesg, **kwargs)
await self.runt.warn(mesg, log=False)
raise s_stormctrl.StormExit(mesg)
raise s_stormctrl.StormExit(mesg=mesg)
raise s_stormctrl.StormExit()

@stormfunc(readonly=True)
Expand Down
4 changes: 2 additions & 2 deletions synapse/tests/test_lib_storm.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ async def test_lib_storm_emit(self):
function inner(v) {
if ( $v = 2 ) {
stop
}
}
return ( $v )
}
function outer(n) {
Expand Down Expand Up @@ -377,7 +377,7 @@ async def test_lib_storm_emit(self):
if ($j = 2) {
stop
}
$lib.print(`{$j}/{$l}`)
$lib.print(`{$j}/{$l}`)
}
'''
msgs = await core.stormlist(q)
Expand Down
6 changes: 3 additions & 3 deletions synapse/tests/test_lib_stormtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5954,7 +5954,7 @@ async def test_exit(self):
with self.raises(s_ctrl.StormExit) as cm:
q = '[test:str=beep.sys] $lib.exit(foo)'
_ = await core.callStorm(q)
self.eq(cm.exception.args, ('foo',))
self.eq(cm.exception.get('mesg'), 'foo')

# Remote tests
async with core.getLocalProxy() as prox:
Expand All @@ -5968,7 +5968,7 @@ async def test_exit(self):
q = '[test:str=beep.sys] $lib.exit()'
with self.raises(s_exc.SynErr) as cm:
_ = await prox.callStorm(q)
self.eq(cm.exception.get('mesg'), '')
self.eq(cm.exception.get('mesg'), 'StormExit: ')
self.eq(cm.exception.get('errx'), 'StormExit')

# A warn is emitted
Expand All @@ -5980,7 +5980,7 @@ async def test_exit(self):
q = '[test:str=beep.sys] $lib.exit("foo {bar}", bar=baz)'
with self.raises(s_exc.SynErr) as cm:
_ = await prox.callStorm(q)
self.eq(cm.exception.get('mesg'), 'foo baz')
self.eq(cm.exception.get('mesg'), "StormExit: mesg='foo baz'")
self.eq(cm.exception.get('errx'), 'StormExit')

async def test_iter(self):
Expand Down

0 comments on commit 2172afa

Please sign in to comment.