-
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.
- Loading branch information
Showing
8 changed files
with
202 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
Traceback (most recent call last): | ||
File "<frozen _collections_abc>", line 240, in __anext__ | ||
File "tests/exceptions/source/modern/decorate_async_generator.py", line 29, in generator | ||
raise ValueError | ||
ValueError |
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 |
85 changes: 85 additions & 0 deletions
85
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,85 @@ | ||
from loguru import logger | ||
import asyncio | ||
import sys | ||
|
||
logger.remove() | ||
logger.add(sys.stderr, format="", diagnose=False, backtrace=False, colorize=False) | ||
|
||
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_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()) | ||
|
||
|
||
# These should be regular Pytest test cases, but that is not | ||
# possible because the syntax is not valid in Python 3.5. | ||
test_decorate_async_generator() | ||
test_decorate_async_generator_with_error() | ||
test_decorate_async_generator_then_async_send() | ||
test_decorate_async_generator_then_async_throw() |
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