-
Notifications
You must be signed in to change notification settings - Fork 718
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for async generators to "@logger.catch"
The implementation is ugly because of Python 3.5 required compatibility ("yield" inside "async def" would generate "SyntaxError"). Also, note that currently async generator do not support arbitrary return values, but this might change (there is an opened PR at the time of writing).
- Loading branch information
Showing
9 changed files
with
221 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Done |
42 changes: 42 additions & 0 deletions
42
tests/exceptions/output/modern/exception_formatting_async_generator.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
|
||
Traceback (most recent call last): | ||
File "tests/exceptions/source/modern/exception_formatting_async_generator.py", line 20, in <module> | ||
f.send(None) | ||
File "tests/exceptions/source/modern/exception_formatting_async_generator.py", line 14, in foo | ||
yield a / b | ||
ZeroDivisionError: division by zero | ||
|
||
Traceback (most recent call last): | ||
|
||
File "tests/exceptions/source/modern/exception_formatting_async_generator.py", line 20, in <module> | ||
f.send(None) | ||
│ └ <method 'send' of 'coroutine' objects> | ||
└ <coroutine object Logger.catch.<locals>.Catcher.__call__.<locals>.AsyncGenCatchWrapper.asend at 0xDEADBEEF> | ||
|
||
File "tests/exceptions/source/modern/exception_formatting_async_generator.py", line 14, in foo | ||
yield a / b | ||
│ └ 0 | ||
└ 1 | ||
|
||
ZeroDivisionError: division by zero | ||
|
||
Traceback (most recent call last): | ||
> File "tests/exceptions/source/modern/exception_formatting_async_generator.py", line 20, in <module> | ||
f.send(None) | ||
File "tests/exceptions/source/modern/exception_formatting_async_generator.py", line 14, in foo | ||
yield a / b | ||
ZeroDivisionError: division by zero | ||
|
||
Traceback (most recent call last): | ||
|
||
> File "tests/exceptions/source/modern/exception_formatting_async_generator.py", line 20, in <module> | ||
f.send(None) | ||
│ └ <method 'send' of 'coroutine' objects> | ||
└ <coroutine object Logger.catch.<locals>.Catcher.__call__.<locals>.AsyncGenCatchWrapper.asend at 0xDEADBEEF> | ||
|
||
File "tests/exceptions/source/modern/exception_formatting_async_generator.py", line 14, in foo | ||
yield a / b | ||
│ └ 0 | ||
└ 1 | ||
|
||
ZeroDivisionError: division by zero |
111 changes: 111 additions & 0 deletions
111
tests/exceptions/source/modern/decorate_async_generator.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
from loguru import logger | ||
import asyncio | ||
import sys | ||
|
||
logger.remove() | ||
|
||
# We're truly only testing whether the tests succeed, we do not care about the formatting. | ||
# These should be regular Pytest test cases, but that is not possible because the syntax is not valid in Python 3.5. | ||
logger.add(lambda m: None, format="", diagnose=True, backtrace=True, colorize=True) | ||
|
||
def test_decorate_async_generator(): | ||
@logger.catch(reraise=True) | ||
async def generator(x, y): | ||
yield x | ||
yield y | ||
|
||
async def coro(): | ||
out = [] | ||
async for val in generator(1, 2): | ||
out.append(val) | ||
return out | ||
|
||
res = asyncio.run(coro()) | ||
assert res == [1, 2] | ||
|
||
|
||
def test_decorate_async_generator_with_error(): | ||
@logger.catch(reraise=False) | ||
async def generator(x, y): | ||
yield x | ||
yield y | ||
raise ValueError | ||
|
||
async def coro(): | ||
out = [] | ||
async for val in generator(1, 2): | ||
out.append(val) | ||
return out | ||
|
||
res = asyncio.run(coro()) | ||
assert res == [1, 2] | ||
|
||
def test_decorate_async_generator_with_error_reraised(): | ||
@logger.catch(reraise=True) | ||
async def generator(x, y): | ||
yield x | ||
yield y | ||
raise ValueError | ||
|
||
async def coro(): | ||
out = [] | ||
try: | ||
async for val in generator(1, 2): | ||
out.append(val) | ||
except ValueError: | ||
pass | ||
else: | ||
raise AssertionError("ValueError not raised") | ||
return out | ||
|
||
res = asyncio.run(coro()) | ||
assert res == [1, 2] | ||
|
||
|
||
def test_decorate_async_generator_then_async_send(): | ||
@logger.catch | ||
async def generator(x, y): | ||
yield x | ||
yield y | ||
|
||
async def coro(): | ||
gen = generator(1, 2) | ||
await gen.asend(None) | ||
await gen.asend(None) | ||
try: | ||
await gen.asend(None) | ||
except StopAsyncIteration: | ||
pass | ||
else: | ||
raise AssertionError("StopAsyncIteration not raised") | ||
|
||
asyncio.run(coro()) | ||
|
||
|
||
def test_decorate_async_generator_then_async_throw(): | ||
@logger.catch | ||
async def generator(x, y): | ||
yield x | ||
yield y | ||
|
||
async def coro(): | ||
gen = generator(1, 2) | ||
await gen.asend(None) | ||
try: | ||
await gen.athrow(ValueError) | ||
except ValueError: | ||
pass | ||
else: | ||
raise AssertionError("ValueError not raised") | ||
|
||
asyncio.run(coro()) | ||
|
||
|
||
test_decorate_async_generator() | ||
test_decorate_async_generator_with_error() | ||
test_decorate_async_generator_with_error_reraised() | ||
test_decorate_async_generator_then_async_send() | ||
test_decorate_async_generator_then_async_throw() | ||
|
||
logger.add(sys.stderr, format="{message}") | ||
logger.info("Done") |
22 changes: 22 additions & 0 deletions
22
tests/exceptions/source/modern/exception_formatting_async_generator.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import sys | ||
|
||
from loguru import logger | ||
|
||
logger.remove() | ||
logger.add(sys.stderr, format="", diagnose=False, backtrace=False, colorize=False) | ||
logger.add(sys.stderr, format="", diagnose=True, backtrace=False, colorize=False) | ||
logger.add(sys.stderr, format="", diagnose=False, backtrace=True, colorize=False) | ||
logger.add(sys.stderr, format="", diagnose=True, backtrace=True, colorize=False) | ||
|
||
|
||
@logger.catch | ||
async def foo(a, b): | ||
yield a / b | ||
|
||
|
||
f = foo(1, 0).asend(None) | ||
|
||
try: | ||
f.send(None) | ||
except StopAsyncIteration: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters