[Python-checkins] cpython (3.4): Accept optional lock object in Condition ctor (tulip issue #198)

andrew.svetlov python-checkins at python.org
Sat Jul 26 16:55:27 CEST 2014


http://hg.python.org/cpython/rev/922ba40a6d3c
changeset:   91884:922ba40a6d3c
branch:      3.4
parent:      91878:2a4a3d3c47a8
user:        Andrew Svetlov <andrew.svetlov at gmail.com>
date:        Sat Jul 26 17:54:34 2014 +0300
summary:
  Accept optional lock object in Condition ctor (tulip issue #198)

files:
  Lib/asyncio/locks.py                |   9 ++++++---
  Lib/test/test_asyncio/test_locks.py |  12 ++++++++++++
  2 files changed, 18 insertions(+), 3 deletions(-)


diff --git a/Lib/asyncio/locks.py b/Lib/asyncio/locks.py
--- a/Lib/asyncio/locks.py
+++ b/Lib/asyncio/locks.py
@@ -255,14 +255,17 @@
     A new Lock object is created and used as the underlying lock.
     """
 
-    def __init__(self, *, loop=None):
+    def __init__(self, lock=None, *, loop=None):
         if loop is not None:
             self._loop = loop
         else:
             self._loop = events.get_event_loop()
 
-        # Lock as an attribute as in threading.Condition.
-        lock = Lock(loop=self._loop)
+        if lock is None:
+            lock = Lock(loop=self._loop)
+        elif lock._loop is not self._loop:
+            raise ValueError("loop argument must agree with lock")
+
         self._lock = lock
         # Export the lock's locked(), acquire() and release() methods.
         self.locked = lock.locked
diff --git a/Lib/test/test_asyncio/test_locks.py b/Lib/test/test_asyncio/test_locks.py
--- a/Lib/test/test_asyncio/test_locks.py
+++ b/Lib/test/test_asyncio/test_locks.py
@@ -656,6 +656,18 @@
 
         self.assertFalse(cond.locked())
 
+    def test_explicit_lock(self):
+        lock = asyncio.Lock(loop=self.loop)
+        cond = asyncio.Condition(lock, loop=self.loop)
+
+        self.assertIs(lock._loop, cond._loop)
+
+    def test_ambiguous_loops(self):
+        loop = self.new_test_loop()
+        lock = asyncio.Lock(loop=self.loop)
+        with self.assertRaises(ValueError):
+            asyncio.Condition(lock, loop=loop)
+
 
 class SemaphoreTests(test_utils.TestCase):
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list