mirror of
https://github.com/python/cpython.git
synced 2025-08-31 05:58:33 +00:00
asyncio: Make Semaphore(0) work properly.
This commit is contained in:
parent
d88c6f9b9b
commit
9c55a58a1d
2 changed files with 6 additions and 2 deletions
|
@ -348,12 +348,12 @@ class Semaphore:
|
||||||
|
|
||||||
def __init__(self, value=1, bound=False, *, loop=None):
|
def __init__(self, value=1, bound=False, *, loop=None):
|
||||||
if value < 0:
|
if value < 0:
|
||||||
raise ValueError("Semaphore initial value must be > 0")
|
raise ValueError("Semaphore initial value must be >= 0")
|
||||||
self._value = value
|
self._value = value
|
||||||
self._bound = bound
|
self._bound = bound
|
||||||
self._bound_value = value
|
self._bound_value = value
|
||||||
self._waiters = collections.deque()
|
self._waiters = collections.deque()
|
||||||
self._locked = False
|
self._locked = (value == 0)
|
||||||
if loop is not None:
|
if loop is not None:
|
||||||
self._loop = loop
|
self._loop = loop
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -684,6 +684,10 @@ class SemaphoreTests(unittest.TestCase):
|
||||||
finally:
|
finally:
|
||||||
events.set_event_loop(None)
|
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):
|
def test_repr(self):
|
||||||
sem = locks.Semaphore(loop=self.loop)
|
sem = locks.Semaphore(loop=self.loop)
|
||||||
self.assertTrue(repr(sem).endswith('[unlocked,value:1]>'))
|
self.assertTrue(repr(sem).endswith('[unlocked,value:1]>'))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue