Skip to content

Commit

Permalink
fix(each.py,map.py): fixed return_exceptions kwarg
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita Grishko committed Jan 10, 2017
1 parent 41f070c commit 708e3e9
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
9 changes: 6 additions & 3 deletions paco/each.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
import asyncio
from .decorator import overload
from .concurrent import ConcurrentExecutor
from .concurrent import ConcurrentExecutor, safe_run
from .assertions import assert_corofunction, assert_iter


Expand Down Expand Up @@ -77,7 +77,8 @@ async def mul_2(num):

@asyncio.coroutine
def collector(index, item):
result = yield from coro(item, *args, **kw)
result = yield from safe_run(coro(item, *args, **kw),
return_exceptions=return_exceptions)
if collect:
results[index] = result
return result
Expand All @@ -87,7 +88,9 @@ def collector(index, item):
pool.add(collector(index, value))

# Wait until all the coroutines finishes
yield from pool.run(timeout=timeout, ignore_empty=True)
yield from pool.run(return_exceptions=return_exceptions,
ignore_empty=True,
timeout=timeout)

# Returns list of mapped results in order
return results
3 changes: 2 additions & 1 deletion paco/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,5 @@ async def mul_2(num):
# Call each iterable but collecting yielded values
return (yield from each(coro, iterable,
limit=limit, loop=loop,
timeout=timeout, collect=True))
timeout=timeout, collect=True,
return_exceptions=return_exceptions))
13 changes: 13 additions & 0 deletions tests/each_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,16 @@ def test_each_invalid_input():
def test_each_invalid_coro():
with pytest.raises(TypeError):
run_in_loop(each(None))


def test_each_return_exceptions():
@asyncio.coroutine
def coro(num):
raise ValueError('foo')

task = each(coro, [1, 2, 3, 4, 5], collect=True, return_exceptions=True)
results = run_in_loop(task)
assert len(results) == 5

for err in results:
assert str(err) == 'foo'
13 changes: 13 additions & 0 deletions tests/map_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,16 @@ def test_map_invalid_input():
def test_map_invalid_coro():
with pytest.raises(TypeError):
run_in_loop(map(None))


def test_map_return_exceptions():
@asyncio.coroutine
def coro(num):
raise ValueError('foo')

task = map(coro, [1, 2, 3, 4, 5], return_exceptions=True)
results = run_in_loop(task)
assert len(results) == 5

for err in results:
assert str(err) == 'foo'

0 comments on commit 708e3e9

Please sign in to comment.