summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2013-04-22 19:51:00 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2013-04-22 19:51:00 (GMT)
commitd194b30407bee1f38ad77f32dca3880853a6a308 (patch)
tree6f5650227734ba45596c47285119997ccb94f723
parent4dc385b4e3a9f7c140ac9725f85d0e60ccce66e6 (diff)
downloadcpython-d194b30407bee1f38ad77f32dca3880853a6a308.zip
cpython-d194b30407bee1f38ad77f32dca3880853a6a308.tar.gz
cpython-d194b30407bee1f38ad77f32dca3880853a6a308.tar.bz2
Issue #11714: Use 'with' statements to assure a Semaphore releases a
condition variable. Original patch by Thomas Rachel.
-rw-r--r--Lib/threading.py42
-rw-r--r--Misc/ACKS1
-rw-r--r--Misc/NEWS3
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.