summaryrefslogtreecommitdiff
path: root/Lib/threading.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2013-04-22 22:51:43 +0300
committerSerhiy Storchaka <storchaka@gmail.com>2013-04-22 22:51:43 +0300
commit1685a2870886639377a90fbcd85b25af4aee3341 (patch)
tree40f92105ed6afcfdef0a7b5696e420587805c29f /Lib/threading.py
parent44df9d8220fe78be50691ad4da4cbd6042e8111b (diff)
downloadcpython-1685a2870886639377a90fbcd85b25af4aee3341.tar.gz
Issue #11714: Use 'with' statements to assure a Semaphore releases a
condition variable. Original patch by Thomas Rachel.
Diffstat (limited to 'Lib/threading.py')
-rw-r--r--Lib/threading.py38
1 files changed, 18 insertions, 20 deletions
diff --git a/Lib/threading.py b/Lib/threading.py
index 46df676f24..ab9302c153 100644
--- a/Lib/threading.py
+++ b/Lib/threading.py
@@ -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()