Issue #11714: Use 'with' statements to assure a Semaphore releases a
authorSerhiy Storchaka <storchaka@gmail.com>
Mon, 22 Apr 2013 19:51:43 +0000 (22:51 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Mon, 22 Apr 2013 19:51:43 +0000 (22:51 +0300)
condition variable.  Original patch by Thomas Rachel.

Lib/threading.py
Misc/ACKS
Misc/NEWS

index 46df676f24181dd8249a4dc736f84473039e2499..ab9302c15324297c0e0eb08ebb4cb01e3c83e44c 100644 (file)
@@ -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()
index 9961aa560e27f72761eda3d144469861b66777da..36adf7d04f0b3c084783b149f9da7693673d3714 100644 (file)
--- 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
index e71b6ed016abef49c1fde270a0e7d9a91b88b3be..60945c6da7341adf1a5b47933c86f293ffedb7d6 100644 (file)
--- 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.