Skip to content

Commit

Permalink
Merge pull request #88 from bgreen-litl/allow-sync-from-async
Browse files Browse the repository at this point in the history
Allow sync decorator call from async function
  • Loading branch information
bgreen-litl authored Dec 7, 2019
2 parents 4c98ce6 + 5d714cc commit decdeef
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 64 deletions.
39 changes: 0 additions & 39 deletions backoff/_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,6 @@ def decorate(target):
import backoff._async
retry = backoff._async.retry_predicate

elif _is_event_loop() and _is_current_task():
# Verify that sync version is not being run from coroutine
# (that would lead to event loop hiccups).
raise TypeError(
"backoff.on_predicate applied to a regular function "
"inside coroutine, this will lead to event loop "
"hiccups. Use backoff.on_predicate on coroutines in "
"asynchronous code.")

if retry is None:
retry = _sync.retry_predicate

Expand Down Expand Up @@ -174,14 +165,6 @@ def decorate(target):
if asyncio.iscoroutinefunction(target):
import backoff._async
retry = backoff._async.retry_exception
elif _is_event_loop() and _is_current_task():
# Verify that sync version is not being run from coroutine
# (that would lead to event loop hiccups).
raise TypeError(
"backoff.on_exception applied to a regular function "
"inside coroutine, this will lead to event loop "
"hiccups. Use backoff.on_exception on coroutines in "
"asynchronous code.")

if retry is None:
retry = _sync.retry_exception
Expand All @@ -193,25 +176,3 @@ def decorate(target):

# Return a function which decorates a target with a retry loop.
return decorate


def _is_event_loop(): # pragma: no cover
import asyncio

try:
if sys.version_info >= (3, 7):
asyncio.get_running_loop()

asyncio.get_event_loop()
except RuntimeError:
return False
else:
return True


def _is_current_task(): # pragma: no cover
import asyncio
if sys.version_info >= (3, 7):
return asyncio.current_task() is not None

return asyncio.Task.current_task() is not None
25 changes: 0 additions & 25 deletions tests/python35/test_backoff_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,31 +615,6 @@ async def coro():
assert (await task)


@pytest.mark.asyncio
async def test_on_exception_on_regular_function():
# Force this function to be a running coroutine.
await asyncio.sleep(0)

with pytest.raises(TypeError) as excinfo:
@backoff.on_exception(backoff.expo, ValueError)
def regular_func():
pass
assert "applied to a regular function" in str(excinfo.value)


@pytest.mark.asyncio
async def test_on_predicate_on_regular_function():
# Force this function to be a running coroutine.
await asyncio.sleep(0)

with pytest.raises(TypeError) as excinfo:
@backoff.on_predicate(backoff.expo)
def regular_func():
pass

assert "applied to a regular function" in str(excinfo.value)


def test_on_predicate_on_regular_function_without_event_loop(monkeypatch):
monkeypatch.setattr('time.sleep', lambda x: None)

Expand Down

0 comments on commit decdeef

Please sign in to comment.