]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #11714: Use 'with' statements to assure a Semaphore releases a
authorSerhiy Storchaka <storchaka@gmail.com>
Mon, 22 Apr 2013 19:51:00 +0000 (22:51 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Mon, 22 Apr 2013 19:51:00 +0000 (22:51 +0300)
condition variable.  Original patch by Thomas Rachel.

Lib/threading.py
Misc/ACKS
Misc/NEWS

index cb49c4a7ff81cf4c8abec2d8a416e96571de6a16..225448bf561fda8f4ff081af48ec5964fb38e039 100644 (file)
@@ -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()
index e9ec9c87fc289d74b8314c59e2080ddff796f9a6..8a316a395b5a5f923f42da9551a8251b69389d74 100644 (file)
--- 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
index d633536b2cbe207ca623005a925038b3625d15f1..8b0bce46b1b56b14b3600dad5e50bb06da4f5452 100644 (file)
--- 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.