diff options
Diffstat (limited to 'Lib/asyncio/locks.py')
| -rw-r--r-- | Lib/asyncio/locks.py | 69 | 
1 files changed, 17 insertions, 52 deletions
| diff --git a/Lib/asyncio/locks.py b/Lib/asyncio/locks.py index f1ce732478..6f322c258c 100644 --- a/Lib/asyncio/locks.py +++ b/Lib/asyncio/locks.py @@ -3,10 +3,9 @@  __all__ = ('Lock', 'Event', 'Condition', 'Semaphore', 'BoundedSemaphore')  import collections -import warnings -from . import events  from . import exceptions +from . import mixins  class _ContextManagerMixin: @@ -20,7 +19,7 @@ class _ContextManagerMixin:          self.release() -class Lock(_ContextManagerMixin): +class Lock(_ContextManagerMixin, mixins._LoopBoundedMixin):      """Primitive lock objects.      A primitive lock is a synchronization primitive that is not owned @@ -74,16 +73,9 @@ class Lock(_ContextManagerMixin):      """ -    def __init__(self, *, loop=None): +    def __init__(self):          self._waiters = None          self._locked = False -        if loop is None: -            self._loop = events.get_event_loop() -        else: -            self._loop = loop -            warnings.warn("The loop argument is deprecated since Python 3.8, " -                          "and scheduled for removal in Python 3.10.", -                          DeprecationWarning, stacklevel=2)      def __repr__(self):          res = super().__repr__() @@ -109,7 +101,7 @@ class Lock(_ContextManagerMixin):          if self._waiters is None:              self._waiters = collections.deque() -        fut = self._loop.create_future() +        fut = self._get_loop().create_future()          self._waiters.append(fut)          # Finally block should be called before the CancelledError @@ -161,7 +153,7 @@ class Lock(_ContextManagerMixin):              fut.set_result(True) -class Event: +class Event(mixins._LoopBoundedMixin):      """Asynchronous equivalent to threading.Event.      Class implementing event objects. An event manages a flag that can be set @@ -170,16 +162,9 @@ class Event:      false.      """ -    def __init__(self, *, loop=None): +    def __init__(self):          self._waiters = collections.deque()          self._value = False -        if loop is None: -            self._loop = events.get_event_loop() -        else: -            self._loop = loop -            warnings.warn("The loop argument is deprecated since Python 3.8, " -                          "and scheduled for removal in Python 3.10.", -                          DeprecationWarning, stacklevel=2)      def __repr__(self):          res = super().__repr__() @@ -220,7 +205,7 @@ class Event:          if self._value:              return True -        fut = self._loop.create_future() +        fut = self._get_loop().create_future()          self._waiters.append(fut)          try:              await fut @@ -229,7 +214,7 @@ class Event:              self._waiters.remove(fut) -class Condition(_ContextManagerMixin): +class Condition(_ContextManagerMixin, mixins._LoopBoundedMixin):      """Asynchronous equivalent to threading.Condition.      This class implements condition variable objects. A condition variable @@ -239,18 +224,10 @@ class Condition(_ContextManagerMixin):      A new Lock object is created and used as the underlying lock.      """ -    def __init__(self, lock=None, *, loop=None): -        if loop is None: -            self._loop = events.get_event_loop() -        else: -            self._loop = loop -            warnings.warn("The loop argument is deprecated since Python 3.8, " -                          "and scheduled for removal in Python 3.10.", -                          DeprecationWarning, stacklevel=2) - +    def __init__(self, lock=None):          if lock is None: -            lock = Lock(loop=loop) -        elif lock._loop is not self._loop: +            lock = Lock() +        elif lock._loop is not self._get_loop():              raise ValueError("loop argument must agree with lock")          self._lock = lock @@ -284,7 +261,7 @@ class Condition(_ContextManagerMixin):          self.release()          try: -            fut = self._loop.create_future() +            fut = self._get_loop().create_future()              self._waiters.append(fut)              try:                  await fut @@ -351,7 +328,7 @@ class Condition(_ContextManagerMixin):          self.notify(len(self._waiters)) -class Semaphore(_ContextManagerMixin): +class Semaphore(_ContextManagerMixin, mixins._LoopBoundedMixin):      """A Semaphore implementation.      A semaphore manages an internal counter which is decremented by each @@ -366,18 +343,11 @@ class Semaphore(_ContextManagerMixin):      ValueError is raised.      """ -    def __init__(self, value=1, *, loop=None): +    def __init__(self, value=1):          if value < 0:              raise ValueError("Semaphore initial value must be >= 0")          self._value = value          self._waiters = collections.deque() -        if loop is None: -            self._loop = events.get_event_loop() -        else: -            self._loop = loop -            warnings.warn("The loop argument is deprecated since Python 3.8, " -                          "and scheduled for removal in Python 3.10.", -                          DeprecationWarning, stacklevel=2)      def __repr__(self):          res = super().__repr__() @@ -407,7 +377,7 @@ class Semaphore(_ContextManagerMixin):          True.          """          while self._value <= 0: -            fut = self._loop.create_future() +            fut = self._get_loop().create_future()              self._waiters.append(fut)              try:                  await fut @@ -436,14 +406,9 @@ class BoundedSemaphore(Semaphore):      above the initial value.      """ -    def __init__(self, value=1, *, loop=None): -        if loop: -            warnings.warn("The loop argument is deprecated since Python 3.8, " -                          "and scheduled for removal in Python 3.10.", -                          DeprecationWarning, stacklevel=2) - +    def __init__(self, value=1):          self._bound_value = value -        super().__init__(value, loop=loop) +        super().__init__(value)      def release(self):          if self._value >= self._bound_value: | 
