Skip to content

Commit

Permalink
Refactor usage of "inspect" functions for simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
Delgan committed Jul 18, 2020
1 parent cfd1e80 commit 6f86f48
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
14 changes: 6 additions & 8 deletions loguru/_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,13 @@
import builtins
import contextlib
import functools
import inspect
import itertools
import logging
import re
import sys
import warnings
from collections import namedtuple
from inspect import isclass
from inspect import isclass, iscoroutinefunction, isgeneratorfunction
from multiprocessing import current_process
from os.path import basename, splitext
from threading import current_thread
Expand Down Expand Up @@ -790,9 +789,7 @@ def add(
encoding = getattr(sink, "encoding", None)
terminator = ""
exception_prefix = "\n"
elif inspect.iscoroutinefunction(sink) or inspect.iscoroutinefunction(
getattr(sink, "__call__", None)
):
elif iscoroutinefunction(sink) or iscoroutinefunction(getattr(sink, "__call__", None)):
name = getattr(sink, "__name__", None) or repr(sink)

if colorize is None:
Expand All @@ -807,7 +804,8 @@ def add(
# running loop in Python 3.5.2 and earlier versions, see python/asyncio#452.
if enqueue and loop is None:
loop = asyncio.get_event_loop()
coro = sink if inspect.iscoroutinefunction(sink) else sink.__call__

coro = sink if iscoroutinefunction(sink) else sink.__call__
wrapped_sink = AsyncSink(coro, loop, error_interceptor)
encoding = "utf8"
terminator = "\n"
Expand Down Expand Up @@ -1201,14 +1199,14 @@ def __exit__(self_, type_, value, traceback_):
def __call__(_, function):
catcher = Catcher(True)

if inspect.iscoroutinefunction(function):
if iscoroutinefunction(function):

async def catch_wrapper(*args, **kwargs):
with catcher:
return await function(*args, **kwargs)
return default

elif inspect.isgeneratorfunction(function):
elif isgeneratorfunction(function):

def catch_wrapper(*args, **kwargs):
with catcher:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ async def my_async_function(message):
def test_coroutine_function_without_name(monkeypatch):
async_function = Wrapper(lambda _: None, repr="<AsyncFunctionWithout>", name=None)
monkeypatch.setattr(
loguru._logger.inspect,
loguru._logger,
"iscoroutinefunction",
lambda x: x is async_function or iscoroutinefunction(x),
)
Expand All @@ -155,7 +155,7 @@ def test_coroutine_function_without_name(monkeypatch):
def test_coroutine_function_with_empty_name(monkeypatch):
async_function = Wrapper(lambda _: None, repr="<AsyncFunctionEmpty>", name="")
monkeypatch.setattr(
loguru._logger.inspect,
loguru._logger,
"iscoroutinefunction",
lambda x: x is async_function or iscoroutinefunction(x),
)
Expand Down

0 comments on commit 6f86f48

Please sign in to comment.