From: Serhiy Storchaka Date: Mon, 22 Apr 2013 19:51:43 +0000 (+0300) Subject: Issue #11714: Use 'with' statements to assure a Semaphore releases a X-Git-Tag: v3.3.2~67 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=81a5855a2739797a9a41e03acecf9746baa3e882;p=thirdparty%2FPython%2Fcpython.git Issue #11714: Use 'with' statements to assure a Semaphore releases a condition variable. Original patch by Thomas Rachel. --- diff --git a/Lib/threading.py b/Lib/threading.py index 46df676f2418..ab9302c15324 100644 --- a/Lib/threading.py +++ b/Lib/threading.py @@ -248,31 +248,29 @@ class Semaphore: raise ValueError("can't specify timeout for non-blocking acquire") rc = False endtime = None - self._cond.acquire() - while self._value == 0: - if not blocking: - break - if timeout is not None: - if endtime is None: - endtime = _time() + timeout - else: - timeout = endtime - _time() - if timeout <= 0: - break - self._cond.wait(timeout) - else: - self._value = self._value - 1 - rc = True - self._cond.release() + with self._cond: + while self._value == 0: + if not blocking: + break + if timeout is not None: + if endtime is None: + endtime = _time() + timeout + else: + timeout = endtime - _time() + if timeout <= 0: + break + self._cond.wait(timeout) + else: + self._value = self._value - 1 + rc = True return rc __enter__ = acquire def release(self): - self._cond.acquire() - self._value = self._value + 1 - self._cond.notify() - self._cond.release() + with self._cond: + self._value = self._value + 1 + self._cond.notify() def __exit__(self, t, v, tb): self.release() diff --git a/Misc/ACKS b/Misc/ACKS index 9961aa560e27..36adf7d04f0b 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -970,6 +970,7 @@ Fernando Pérez Pierre Quentel Brian Quinlan Anders Qvist +Thomas Rachel Jérôme Radix Burton Radons Jeff Ramnani diff --git a/Misc/NEWS b/Misc/NEWS index e71b6ed016ab..60945c6da734 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -36,6 +36,9 @@ Core and Builtins 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 Unix domain sockets.