[Python-checkins] cpython: asyncio: Make Semaphore(0) work properly.

guido.van.rossum python-checkins at python.org
Thu Nov 21 20:08:45 CET 2013


http://hg.python.org/cpython/rev/4dd5de61e5b5
changeset:   87317:4dd5de61e5b5
parent:      87315:cf8ac1272e07
user:        Guido van Rossum <guido at dropbox.com>
date:        Thu Nov 21 11:07:45 2013 -0800
summary:
  asyncio: Make Semaphore(0) work properly.

files:
  Lib/asyncio/locks.py                |  4 ++--
  Lib/test/test_asyncio/test_locks.py |  4 ++++
  2 files changed, 6 insertions(+), 2 deletions(-)


diff --git a/Lib/asyncio/locks.py b/Lib/asyncio/locks.py
--- a/Lib/asyncio/locks.py
+++ b/Lib/asyncio/locks.py
@@ -348,12 +348,12 @@
 
     def __init__(self, value=1, bound=False, *, loop=None):
         if value < 0:
-            raise ValueError("Semaphore initial value must be > 0")
+            raise ValueError("Semaphore initial value must be >= 0")
         self._value = value
         self._bound = bound
         self._bound_value = value
         self._waiters = collections.deque()
-        self._locked = False
+        self._locked = (value == 0)
         if loop is not None:
             self._loop = loop
         else:
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
@@ -684,6 +684,10 @@
         finally:
             events.set_event_loop(None)
 
+    def test_initial_value_zero(self):
+        sem = locks.Semaphore(0, loop=self.loop)
+        self.assertTrue(sem.locked())
+
     def test_repr(self):
         sem = locks.Semaphore(loop=self.loop)
         self.assertTrue(repr(sem).endswith('[unlocked,value:1]>'))

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


More information about the Python-checkins mailing list