Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(each.py,map.py): fixed return_exceptions kwarg #33

Merged
merged 1 commit into from
Jan 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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'