Skip to content

Commit

Permalink
Add **kwargs to capture and acapture
Browse files Browse the repository at this point in the history
  • Loading branch information
RazerM committed Mar 29, 2018
1 parent 43ef9ae commit eddfff8
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 20 deletions.
8 changes: 4 additions & 4 deletions docs/source/tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ Tutorial
Outcome provides a function for capturing the outcome of a Python
function call, so that it can be passed around. The basic rule is::

result = outcome.capture(f, *args)
result = outcome.capture(f, *args, **kwargs)
x = result.unwrap()

is the same as::

x = f(*args)
x = f(*args, **kwargs)

even if ``f`` raises an error.

On Python 3.5+, there's also :func:`acapture`::

result = await outcome.acapture(f, *args)
result = await outcome.acapture(f, *args, **kwargs)
x = result.unwrap()

which, like before, is the same as::

x = await f(*args)
x = await f(*args, **kwargs)

See the :ref:`api-reference` for the types involved.
12 changes: 6 additions & 6 deletions src/outcome/_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,29 @@
__all__ = ['Error', 'Outcome', 'Value', 'acapture', 'capture']


def capture(sync_fn, *args):
"""Run ``sync_fn(*args)`` and capture the result.
def capture(sync_fn, *args, **kwargs):
"""Run ``sync_fn(*args, **kwargs)`` and capture the result.
Returns:
Either a :class:`Value` or :class:`Error` as appropriate.
"""
# _sync.capture references ErrorBase and ValueBase
try:
return Value(sync_fn(*args))
return Value(sync_fn(*args, **kwargs))
except BaseException as exc:
return Error(exc)


async def acapture(async_fn, *args):
"""Run ``await async_fn(*args)`` and capture the result.
async def acapture(async_fn, *args, **kwargs):
"""Run ``await async_fn(*args, **kwargs)`` and capture the result.
Returns:
Either a :class:`Value` or :class:`Error` as appropriate.
"""
try:
return Value(await async_fn(*args))
return Value(await async_fn(*args, **kwargs))
except BaseException as exc:
return Error(exc)

Expand Down
6 changes: 3 additions & 3 deletions src/outcome/_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
__all__ = ['Error', 'Outcome', 'Value', 'capture']


def capture(sync_fn, *args):
"""Run ``sync_fn(*args)`` and capture the result.
def capture(sync_fn, *args, **kwargs):
"""Run ``sync_fn(*args, **kwargs)`` and capture the result.
Returns:
Either a :class:`Value` or :class:`Error` as appropriate.
"""
try:
return Value(sync_fn(*args))
return Value(sync_fn(*args, **kwargs))
except BaseException as exc:
return Error(exc)

Expand Down
6 changes: 3 additions & 3 deletions tests/test_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@


async def test_acapture():
async def return_arg(x):
async def add(x, y):
await trio.hazmat.checkpoint()
return x
return x + y

v = await outcome.acapture(return_arg, 7)
v = await outcome.acapture(add, 3, y=4)
assert v == Value(7)

async def raise_ValueError(x):
Expand Down
8 changes: 4 additions & 4 deletions tests/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ def test_Value_compare():


def test_capture():
def return_arg(x):
return x
def add(x, y):
return x + y

v = outcome.capture(return_arg, 2)
v = outcome.capture(add, 2, y=3)
assert type(v) == Value
assert v.unwrap() == 2
assert v.unwrap() == 5

def raise_ValueError(x):
raise ValueError(x)
Expand Down

0 comments on commit eddfff8

Please sign in to comment.