Skip to content

Commit

Permalink
Fix unwrapping exceptions from decorated functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mwaskom committed Dec 19, 2024
1 parent a99d474 commit 7b2055f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/synchronicity/async_wrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def wraps_by_interface(interface: Interface, func):
Note: Does not forward async generator information other than explicit annotations
"""
if inspect.iscoroutinefunction(func) and interface == Interface._ASYNC_WITH_BLOCKING_TYPES:
if is_coroutine_function_follow_wrapped(func) and interface == Interface._ASYNC_WITH_BLOCKING_TYPES:

def asyncfunc_deco(user_wrapper):
@functools.wraps(func)
Expand Down
1 change: 1 addition & 0 deletions src/synchronicity/synchronizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ async def wrapper_coro():
return value

def _run_function_sync_future(self, coro, original_func):
print(">>> Running!")
coro = wrap_coro_exception(coro)
coro = self._wrap_check_async_leakage(coro)
loop = self._get_loop(start=True)
Expand Down
25 changes: 25 additions & 0 deletions test/exception_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import asyncio
import concurrent
import functools
import inspect
import pytest
import time
Expand Down Expand Up @@ -164,3 +165,27 @@ async def test_function_raises_with_cause_async_syncwrap(synchronizer):
await coro
assert SLEEP_DELAY < time.monotonic() - t0 < 2 * SLEEP_DELAY
assert isinstance(exc.value.__cause__, CustomExceptionCause)


def decorator(f):
@functools.wraps(f)
def wrapper(*args, **kwargs):
return f(*args, **kwargs)

return wrapper


f_raises_wrapped = decorator(f_raises)


@pytest.mark.asyncio
async def test_wrapped_function_raises_async(synchronizer):
t0 = time.monotonic()
f_raises_s = synchronizer.create_blocking(f_raises_wrapped)
coro = f_raises_s.aio()
assert inspect.iscoroutine(coro)
assert time.monotonic() - t0 < SLEEP_DELAY
with pytest.raises(CustomException) as exc:
await coro
assert SLEEP_DELAY < time.monotonic() - t0 < 2 * SLEEP_DELAY
assert exc.value.__suppress_context__ or exc.value.__context__ is None

0 comments on commit 7b2055f

Please sign in to comment.