Skip to content

Commit

Permalink
chore: refactor unused var c_logger out of method.pyx (#490)
Browse files Browse the repository at this point in the history
* chore: refactor unused var c_logger out of method.pyx

* Update method.pyx

* Update method.pyx

* Update method.pyx

* Update method.pyx
  • Loading branch information
BobTheBuidler authored Dec 16, 2024
1 parent 6aedb43 commit 104bc0e
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions a_sync/a_sync/method.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ asynchronously based on various conditions and configurations.
# mypy: disable-error-code=valid-type
# mypy: disable-error-code=misc
import functools
import logging
import weakref
from inspect import isawaitable
from libc.stdint cimport uintptr_t
from logging import DEBUG, getLogger

from a_sync._typing import *
from a_sync.a_sync._kwargs cimport get_flag_name, is_sync
Expand All @@ -34,9 +34,15 @@ METHOD_CACHE_TTL = 300
cdef int _METHOD_CACHE_TTL = 300


logger = getLogger(__name__)
logger = logging.getLogger(__name__)

cdef object c_logger = logger
cdef object _logger_is_enabled_for = logger.isEnabledFor
cdef object _logger_log = logger._log
cdef int DEBUG = logging.DEBUG

cdef inline void _logger_debug(str msg, tuple args):
if _logger_is_enabled_for(DEBUG):
_logger_log(DEBUG, msg, args)


cdef object get_event_loop = asyncio.get_event_loop
Expand Down Expand Up @@ -96,12 +102,9 @@ class ASyncMethodDescriptor(ASyncDescriptor[I, P, T]):
>>> await descriptor(instance, arg1, arg2, kwarg1=value1)
"""
# NOTE: This is only used by TaskMapping atm # TODO: use it elsewhere
c_logger.debug(
_logger_debug(
"awaiting %s for instance: %s args: %s kwargs: %s",
self,
instance,
args,
kwargs,
(self, instance, args, kwargs),
)
return await self.__get__(instance, None)(*args, **kwargs)

Expand Down Expand Up @@ -166,7 +169,7 @@ class ASyncMethodDescriptor(ASyncDescriptor[I, P, T]):
instance, self.__wrapped__, self.__is_async_def__, **self.modifiers
)
instance.__dict__[self.field_name] = bound
c_logger.debug("new bound method: %s", bound)
_logger_debug("new bound method: %s", (bound,))
_update_cache_timer(self.field_name, instance, bound)
return bound

Expand Down Expand Up @@ -322,7 +325,7 @@ class ASyncMethodDescriptorSyncDefault(ASyncMethodDescriptor[I, P, T]):
instance, self.__wrapped__, self.__is_async_def__, **self.modifiers
)
instance.__dict__[self.field_name] = bound
c_logger.debug("new bound method: %s", bound)
_logger_debug("new bound method: %s", (bound,))
_update_cache_timer(self.field_name, instance, bound)
return bound

Expand Down Expand Up @@ -407,7 +410,7 @@ class ASyncMethodDescriptorAsyncDefault(ASyncMethodDescriptor[I, P, T]):
instance, self.__wrapped__, self.__is_async_def__, **self.modifiers
)
instance.__dict__[self.field_name] = bound
c_logger.debug("new bound method: %s", bound)
_logger_debug("new bound method: %s", (bound,))
_update_cache_timer(self.field_name, instance, bound)
return bound

Expand Down Expand Up @@ -596,8 +599,8 @@ class ASyncBoundMethod(ASyncFunction[P, T], Generic[I, P, T]):
"""
cdef object retval, coro
cdef bint debug_logs
if debug_logs := c_logger.isEnabledFor(DEBUG):
c_logger._log(DEBUG, "calling %s with args: %s kwargs: %s", (self, args, kwargs))
if debug_logs := _logger_is_enabled_for(DEBUG):
_logger_log(DEBUG, "calling %s with args: %s kwargs: %s", (self, args, kwargs))
# This could either be a coroutine or a return value from an awaited coroutine,
# depending on if an overriding flag kwarg was passed into the function call.
retval = coro = ASyncFunction.__call__(self, self.__self__, *args, **kwargs)
Expand All @@ -608,12 +611,12 @@ class ASyncBoundMethod(ASyncFunction[P, T], Generic[I, P, T]):
elif _should_await(self, kwargs):
# The awaitable was not awaited, so now we need to check the flag as defined on 'self' and await if appropriate.
if debug_logs:
c_logger._log(
_logger_log(
DEBUG, "awaiting %s for %s args: %s kwargs: %s", (coro, self, args, kwargs)
)
retval = _await(coro)
if debug_logs:
c_logger._log(
_logger_log(
DEBUG, "returning %s for %s args: %s kwargs: %s", (retval, self, args, kwargs)
)
return retval # type: ignore [call-overload, return-value]
Expand Down

0 comments on commit 104bc0e

Please sign in to comment.