From d194b30407bee1f38ad77f32dca3880853a6a308 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 22 Apr 2013 22:51:00 +0300 Subject: Issue #11714: Use 'with' statements to assure a Semaphore releases a condition variable. Original patch by Thomas Rachel. --- Lib/threading.py | 42 ++++++++++++++++++++---------------------- Misc/ACKS | 1 + Misc/NEWS | 3 +++ 3 files changed, 24 insertions(+), 22 deletions(-) diff --git a/Lib/threading.py b/Lib/threading.py index cb49c4a..225448b 100644 --- a/Lib/threading.py +++ b/Lib/threading.py @@ -457,21 +457,20 @@ class _Semaphore(_Verbose): """ rc = False - self.__cond.acquire() - while self.__value == 0: - if not blocking: - break - if __debug__: - self._note("%s.acquire(%s): blocked waiting, value=%s", - self, blocking, self.__value) - self.__cond.wait() - else: - self.__value = self.__value - 1 - if __debug__: - self._note("%s.acquire: success, value=%s", - self, self.__value) - rc = True - self.__cond.release() + with self.__cond: + while self.__value == 0: + if not blocking: + break + if __debug__: + self._note("%s.acquire(%s): blocked waiting, value=%s", + self, blocking, self.__value) + self.__cond.wait() + else: + self.__value = self.__value - 1 + if __debug__: + self._note("%s.acquire: success, value=%s", + self, self.__value) + rc = True return rc __enter__ = acquire @@ -483,13 +482,12 @@ class _Semaphore(_Verbose): to become larger than zero again, wake up that thread. """ - self.__cond.acquire() - self.__value = self.__value + 1 - if __debug__: - self._note("%s.release: success, value=%s", - self, self.__value) - self.__cond.notify() - self.__cond.release() + with self.__cond: + self.__value = self.__value + 1 + if __debug__: + self._note("%s.release: success, value=%s", + self, self.__value) + self.__cond.notify() def __exit__(self, t, v, tb): self.release() diff --git a/Misc/ACKS b/Misc/ACKS index e9ec9c8..8a316a3 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -810,6 +810,7 @@ Fernando Pérez Eduardo Pérez Brian Quinlan Anders Qvist +Thomas Rachel Burton Radons Jeff Ramnani Brodie Rao diff --git a/Misc/NEWS b/Misc/NEWS index d633536..8b0bce4 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -28,6 +28,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. -- cgit v0.12