diff --git a/a_sync/_smart.py b/a_sync/_smart.py index c1b28f29..b6e901d7 100644 --- a/a_sync/_smart.py +++ b/a_sync/_smart.py @@ -6,7 +6,6 @@ """ import asyncio -import contextvars import logging import warnings import weakref @@ -198,7 +197,7 @@ def __init__( if key: self._key = key self._waiters = weakref.WeakSet() - self._callbacks.append((SmartFuture._self_done_cleanup_callback, contextvars.copy_context())) + self.add_done_callback(SmartFuture._self_done_cleanup_callback) def __repr__(self): return f"<{type(self).__name__} key={self._key} waiters={self.num_waiters} {self._state}>" @@ -299,7 +298,7 @@ def __init__( """ asyncio.Task.__init__(self, coro, loop=loop, name=name) self._waiters: Set["asyncio.Task[T]"] = set() - self._callbacks.append((SmartTask._self_done_cleanup_callback, contextvars.copy_context())) + self.add_done_callback(SmartTask._self_done_cleanup_callback) def smart_task_factory( diff --git a/a_sync/primitives/_debug.pyx b/a_sync/primitives/_debug.pyx index a74b41f9..71213f5a 100644 --- a/a_sync/primitives/_debug.pyx +++ b/a_sync/primitives/_debug.pyx @@ -32,7 +32,7 @@ cdef object _get_running_loop(): cdef class _LoopBoundMixin(_LoggerMixin): def __cinit__(self): - self.__loop = None + self._LoopBoundMixin__loop = None def __init__(self, *, loop=None): if loop is not None: raise TypeError( @@ -42,25 +42,25 @@ cdef class _LoopBoundMixin(_LoggerMixin): ) @property def _loop(self) -> asyncio.AbstractEventLoop: - return self.__loop + return self._LoopBoundMixin__loop @_loop.setter def _loop(self, loop: asyncio.AbstractEventLoop): - self.__loop = loop + self._LoopBoundMixin__loop = loop cpdef object _get_loop(self): return self._c_get_loop() cdef object _c_get_loop(self): cdef object loop = _get_running_loop() - if self.__loop is None: + if self._LoopBoundMixin__loop is None: with _global_lock: - if self.__loop is None: - self.__loop = loop + if self._LoopBoundMixin__loop is None: + self._LoopBoundMixin__loop = loop if loop is None: return get_event_loop() - elif loop is not self.__loop: + elif loop is not self._LoopBoundMixin__loop: raise RuntimeError( f'{self!r} is bound to a different event loop', "running loop: ".format(loop), - "bound to: ".format(self.__loop), + "bound to: ".format(self._LoopBoundMixin__loop), ) return loop