]> granicus.if.org Git - python/commitdiff
Issue #11714: Use 'with' statements to assure a Semaphore releases a
authorSerhiy Storchaka <storchaka@gmail.com>
Mon, 22 Apr 2013 19:54:16 +0000 (22:54 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Mon, 22 Apr 2013 19:54:16 +0000 (22:54 +0300)
condition variable.  Original patch by Thomas Rachel.

1  2 
Lib/threading.py
Misc/ACKS
Misc/NEWS

index e7f7df7d7f80d5857641de4e1afc319e5ad8dd8c,ab9302c15324297c0e0eb08ebb4cb01e3c83e44c..3526894ae0f3cea54e3ce11a3680471c7b673034
@@@ -253,31 -248,29 +253,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 -= 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
++                self._value -= 1
+                 rc = True
          return rc
  
      __enter__ = acquire
  
      def release(self):
-         self._cond.acquire()
-         self._value += 1
-         self._cond.notify()
-         self._cond.release()
+         with self._cond:
 -            self._value = self._value + 1
++            self._value += 1
+             self._cond.notify()
  
      def __exit__(self, t, v, tb):
          self.release()
diff --cc Misc/ACKS
index e6f09f1fbafcf49ac02471f14b7e648ebb18ba20,36adf7d04f0b3c084783b149f9da7693673d3714..3520e96088adf581a479be7c09a6ee20a18392e4
+++ b/Misc/ACKS
@@@ -993,7 -970,7 +993,8 @@@ Fernando Pére
  Pierre Quentel
  Brian Quinlan
  Anders Qvist
+ Thomas Rachel
 +Ram Rachum
  Jérôme Radix
  Burton Radons
  Jeff Ramnani
diff --cc Misc/NEWS
index 0e1ebac7714cc7591c82c030499e9392fb9185cc,60945c6da7341adf1a5b47933c86f293ffedb7d6..92c6043dee2c7dac548c47b3dad2f8727e71f290
+++ b/Misc/NEWS
@@@ -49,10 -36,9 +49,13 @@@ Core and Builtin
  Library
  -------
  
+ - Issue #11714: Use 'with' statements to assure a Semaphore releases a
+   condition variable.  Original patch by Thomas Rachel.
 +- Issue #16624: `subprocess.check_output` now accepts an `input` argument,
 +  allowing the subprocess's stdin to be provided as a (byte) string.
 +  Patch by Zack Weinberg.
 +
  - Issue #17795: Reverted backwards-incompatible change in SysLogHandler with
    Unix domain sockets.