From: Serhiy Storchaka Date: Mon, 22 Apr 2013 19:54:16 +0000 (+0300) Subject: Issue #11714: Use 'with' statements to assure a Semaphore releases a X-Git-Tag: v3.4.0a1~856 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b00b596c053cf21826b5fe342be0354333a102f5;p=python Issue #11714: Use 'with' statements to assure a Semaphore releases a condition variable. Original patch by Thomas Rachel. --- b00b596c053cf21826b5fe342be0354333a102f5 diff --cc Lib/threading.py index e7f7df7d7f,ab9302c153..3526894ae0 --- a/Lib/threading.py +++ b/Lib/threading.py @@@ -253,31 -248,29 +253,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 -= 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 ++ self._value -= 1 + rc = True return rc __enter__ = acquire def release(self): - self._cond.acquire() - self._value += 1 - self._cond.notify() - self._cond.release() + with self._cond: - self._value = self._value + 1 ++ self._value += 1 + self._cond.notify() def __exit__(self, t, v, tb): self.release() diff --cc Misc/ACKS index e6f09f1fba,36adf7d04f..3520e96088 --- a/Misc/ACKS +++ b/Misc/ACKS @@@ -993,7 -970,7 +993,8 @@@ Fernando Pére Pierre Quentel Brian Quinlan Anders Qvist + Thomas Rachel +Ram Rachum Jérôme Radix Burton Radons Jeff Ramnani diff --cc Misc/NEWS index 0e1ebac771,60945c6da7..92c6043dee --- a/Misc/NEWS +++ b/Misc/NEWS @@@ -49,10 -36,9 +49,13 @@@ Core and Builtin Library ------- + - Issue #11714: Use 'with' statements to assure a Semaphore releases a + condition variable. Original patch by Thomas Rachel. + +- Issue #16624: `subprocess.check_output` now accepts an `input` argument, + allowing the subprocess's stdin to be provided as a (byte) string. + Patch by Zack Weinberg. + - Issue #17795: Reverted backwards-incompatible change in SysLogHandler with Unix domain sockets.