[Python-checkins] cpython (3.3): Issue #11714: Use 'with' statements to assure a Semaphore releases a

serhiy.storchaka python-checkins at python.org
Mon Apr 22 21:57:53 CEST 2013


http://hg.python.org/cpython/rev/af30c5cb248f
changeset:   83497:af30c5cb248f
branch:      3.3
parent:      83492:041960bf92fe
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Mon Apr 22 22:51:43 2013 +0300
summary:
  Issue #11714: Use 'with' statements to assure a Semaphore releases a
condition variable.  Original patch by Thomas Rachel.

files:
  Lib/threading.py |  38 +++++++++++++++++------------------
  Misc/ACKS        |   1 +
  Misc/NEWS        |   3 ++
  3 files changed, 22 insertions(+), 20 deletions(-)


diff --git a/Lib/threading.py b/Lib/threading.py
--- a/Lib/threading.py
+++ b/Lib/threading.py
@@ -248,31 +248,29 @@
             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()
diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -970,6 +970,7 @@
 Pierre Quentel
 Brian Quinlan
 Anders Qvist
+Thomas Rachel
 Jérôme Radix
 Burton Radons
 Jeff Ramnani
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -36,6 +36,9 @@
 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.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list