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

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


http://hg.python.org/cpython/rev/2253b8a18bbf
changeset:   83496:2253b8a18bbf
branch:      2.7
parent:      83489:32a5de0e91d5
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Mon Apr 22 22:51:00 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 |  42 +++++++++++++++++------------------
  Misc/ACKS        |   1 +
  Misc/NEWS        |   3 ++
  3 files changed, 24 insertions(+), 22 deletions(-)


diff --git a/Lib/threading.py b/Lib/threading.py
--- a/Lib/threading.py
+++ b/Lib/threading.py
@@ -457,21 +457,20 @@
 
         """
         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 @@
         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
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -810,6 +810,7 @@
 Eduardo Pérez
 Brian Quinlan
 Anders Qvist
+Thomas Rachel
 Burton Radons
 Jeff Ramnani
 Brodie Rao
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -28,6 +28,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