Updates to the with-statement:
- New semantics for __exit__() -- it must re-raise the exception
if type is not None; the with-statement itself doesn't do this.
(See the updated PEP for motivation.)
- Added context managers to:
- file
- thread.LockType
- threading.{Lock,RLock,Condition,Semaphore,BoundedSemaphore}
- decimal.Context
- Added contextlib.py, which defines @contextmanager, nested(), closing().
- Unit tests all around; bot no docs yet.
diff --git a/Lib/threading.py b/Lib/threading.py
index 9cc108e..5b485d5 100644
--- a/Lib/threading.py
+++ b/Lib/threading.py
@@ -90,6 +90,9 @@
self.__owner and self.__owner.getName(),
self.__count)
+ def __context__(self):
+ return self
+
def acquire(self, blocking=1):
me = currentThread()
if self.__owner is me:
@@ -108,6 +111,8 @@
self._note("%s.acquire(%s): failure", self, blocking)
return rc
+ __enter__ = acquire
+
def release(self):
me = currentThread()
assert self.__owner is me, "release() of un-acquire()d lock"
@@ -121,6 +126,11 @@
if __debug__:
self._note("%s.release(): non-final release", self)
+ def __exit__(self, t, v, tb):
+ self.release()
+ if t is not None:
+ raise t, v, tb
+
# Internal methods used by condition variables
def _acquire_restore(self, (count, owner)):
@@ -156,6 +166,7 @@
self.__lock = lock
# Export the lock's acquire() and release() methods
self.acquire = lock.acquire
+ self.__enter__ = self.acquire
self.release = lock.release
# If the lock defines _release_save() and/or _acquire_restore(),
# these override the default implementations (which just call
@@ -174,6 +185,14 @@
pass
self.__waiters = []
+ def __context__(self):
+ return self
+
+ def __exit__(self, t, v, tb):
+ self.release()
+ if t is not None:
+ raise t, v, tb
+
def __repr__(self):
return "<Condition(%s, %d)>" % (self.__lock, len(self.__waiters))
@@ -267,6 +286,9 @@
self.__cond = Condition(Lock())
self.__value = value
+ def __context__(self):
+ return self
+
def acquire(self, blocking=1):
rc = False
self.__cond.acquire()
@@ -286,6 +308,8 @@
self.__cond.release()
return rc
+ __enter__ = acquire
+
def release(self):
self.__cond.acquire()
self.__value = self.__value + 1
@@ -295,6 +319,11 @@
self.__cond.notify()
self.__cond.release()
+ def __exit__(self, t, v, tb):
+ self.release()
+ if t is not None:
+ raise t, v, tb
+
def BoundedSemaphore(*args, **kwargs):
return _BoundedSemaphore(*args, **kwargs)