Issue #11714: Use 'with' statements to assure a Semaphore releases a

condition variable.  Original patch by Thomas Rachel.
This commit is contained in:
Serhiy Storchaka 2013-04-22 22:51:43 +03:00
parent d4cb4b7451
commit 81a5855a27
3 changed files with 22 additions and 20 deletions

View File

@ -248,31 +248,29 @@ class Semaphore:
raise ValueError("can't specify timeout for non-blocking acquire") raise ValueError("can't specify timeout for non-blocking acquire")
rc = False rc = False
endtime = None endtime = None
self._cond.acquire() with self._cond:
while self._value == 0: while self._value == 0:
if not blocking: if not blocking:
break break
if timeout is not None: if timeout is not None:
if endtime is None: if endtime is None:
endtime = _time() + timeout endtime = _time() + timeout
else: else:
timeout = endtime - _time() timeout = endtime - _time()
if timeout <= 0: if timeout <= 0:
break break
self._cond.wait(timeout) self._cond.wait(timeout)
else: else:
self._value = self._value - 1 self._value = self._value - 1
rc = True rc = True
self._cond.release()
return rc return rc
__enter__ = acquire __enter__ = acquire
def release(self): def release(self):
self._cond.acquire() with self._cond:
self._value = self._value + 1 self._value = self._value + 1
self._cond.notify() self._cond.notify()
self._cond.release()
def __exit__(self, t, v, tb): def __exit__(self, t, v, tb):
self.release() self.release()

View File

@ -970,6 +970,7 @@ Fernando Pérez
Pierre Quentel Pierre Quentel
Brian Quinlan Brian Quinlan
Anders Qvist Anders Qvist
Thomas Rachel
Jérôme Radix Jérôme Radix
Burton Radons Burton Radons
Jeff Ramnani Jeff Ramnani

View File

@ -36,6 +36,9 @@ Core and Builtins
Library Library
------- -------
- Issue #11714: Use 'with' statements to assure a Semaphore releases a
condition variable. Original patch by Thomas Rachel.
- Issue #17795: Reverted backwards-incompatible change in SysLogHandler with - Issue #17795: Reverted backwards-incompatible change in SysLogHandler with
Unix domain sockets. Unix domain sockets.