Skip to content

Commit

Permalink
Support for args and kwargs in goless.go function
Browse files Browse the repository at this point in the history
  • Loading branch information
rgalanakis committed Jun 5, 2014
1 parent 0ccb4f8 commit 096f54a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
7 changes: 5 additions & 2 deletions goless/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,20 @@ def on_panic(etype, value, tb):
_be.propogate_exc(SystemExit, 1)


def go(func):
def go(func, *args, **kwargs):
"""
Run a function in a new tasklet, like a goroutine.
If the goroutine raises an unhandled exception (*panics*),
the :func:`goless.on_panic` will be called,
which by default logs the error and exits the process.
:param args: Positional arguments to ``func``.
:param kwargs: Keyword arguments to ``func``.
"""
def safe_wrapped(f):
# noinspection PyBroadException
try:
f()
f(*args, **kwargs)
except:
on_panic(*sys.exc_info())
_be.start(safe_wrapped, func)
6 changes: 6 additions & 0 deletions tests/test_goless.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ def test_starts_stuff(self):
be.yield_()
self.assertEqual(items, [1])

def test_starts_with_params(self):
called = mock.Mock()
goless.go(called, 10, a=1)
be.yield_()
called.assert_called_once_with(10, a=1)

def test_exc(self):
def raiseit():
raise RuntimeError('ha!')
Expand Down

0 comments on commit 096f54a

Please sign in to comment.