8000 Allow and correctly implement Semaphore(0). · Python-Repository-Hub/asyncio@f94c113 · GitHub
[go: up one dir, main page]

Skip to content

Commit f94c113

Browse files
committed
Allow and correctly implement Semaphore(0).
1 parent 7953750 commit f94c113

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

asyncio/locks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,12 +348,12 @@ class Semaphore:
348348

349349
def __init__(self, value=1, bound=False, *, loop=None):
350350
if value < 0:
351-
raise ValueError("Semaphore initial value must be > 0")
351+
raise ValueError("Semaphore initial value must be >= 0")
352352
self._value = value
353353
self._bound = bound
354354
self._bound_value = value
355355
self._waiters = collections.deque()
356-
self._locked = False
356+
self._locked = (value == 0)
357357
if loop is not None:
358358
self._loop = loop
359359
else:

tests/test_locks.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,10 @@ def test_ctor_noloop(self):
684684
finally:
685685
events.set_event_loop(None)
686686

687+
def test_initial_value_zero(self):
688+
sem = locks.Semaphore(0, loop=self.loop)
689+
self.assertTrue(sem.locked())
690+
687691
def test_repr(self):
688692
sem = locks.Semaphore(loop=self.loop)
689693
self.assertTrue(repr(sem).endswith('[unlocked,value:1]>'))

0 commit comments

Comments
 (0)
0