diff options
author | Guido van Rossum <guido@python.org> | 2006-02-27 22:32:47 +0000 |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2006-02-27 22:32:47 +0000 |
commit | c2e20744b2b7811632030470971c31630f0975e2 (patch) | |
tree | e97b1c1471fd00e4e5648ed317274c1d9005d2ca /Lib/test/nested.py | |
parent | 5fec904f84a40005f824abe295525a1710056be0 (diff) | |
download | cpython-git-c2e20744b2b7811632030470971c31630f0975e2.tar.gz |
PEP 343 -- the with-statement.
This was started by Mike Bland and completed by Guido
(with help from Neal).
This still needs a __future__ statement added;
Thomas is working on Michael's patch for that aspect.
There's a small amount of code cleanup and refactoring
in ast.c, compile.c and ceval.c (I fixed the lltrace
behavior when EXT_POP is used -- however I had to make
lltrace a static global).
Diffstat (limited to 'Lib/test/nested.py')
-rw-r--r-- | Lib/test/nested.py | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/Lib/test/nested.py b/Lib/test/nested.py new file mode 100644 index 0000000000..bb210d462a --- /dev/null +++ b/Lib/test/nested.py @@ -0,0 +1,41 @@ +import sys +from collections import deque + + +class nested(object): + def __init__(self, *contexts): + self.contexts = contexts + self.entered = None + + def __context__(self): + return self + + def __enter__(self): + if self.entered is not None: + raise RuntimeError("Context is not reentrant") + self.entered = deque() + vars = [] + try: + for context in self.contexts: + mgr = context.__context__() + vars.append(mgr.__enter__()) + self.entered.appendleft(mgr) + except: + self.__exit__(*sys.exc_info()) + raise + return vars + + def __exit__(self, *exc_info): + # Behave like nested with statements + # first in, last out + # New exceptions override old ones + ex = exc_info + for mgr in self.entered: + try: + mgr.__exit__(*ex) + except: + ex = sys.exc_info() + self.entered = None + if ex is not exc_info: + raise ex[0], ex[1], ex[2] + |