diff options
| author | Guido van Rossum <guido@python.org> | 2006-02-27 22:32:47 (GMT) |
|---|---|---|
| committer | Guido van Rossum <guido@python.org> | 2006-02-27 22:32:47 (GMT) |
| commit | c2e20744b2b7811632030470971c31630f0975e2 (patch) | |
| tree | e97b1c1471fd00e4e5648ed317274c1d9005d2ca /Lib/test/contextmanager.py | |
| parent | 5fec904f84a40005f824abe295525a1710056be0 (diff) | |
| download | cpython-c2e20744b2b7811632030470971c31630f0975e2.zip cpython-c2e20744b2b7811632030470971c31630f0975e2.tar.gz cpython-c2e20744b2b7811632030470971c31630f0975e2.tar.bz2 | |
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/contextmanager.py')
| -rw-r--r-- | Lib/test/contextmanager.py | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/Lib/test/contextmanager.py b/Lib/test/contextmanager.py new file mode 100644 index 0000000..0ebf646 --- /dev/null +++ b/Lib/test/contextmanager.py @@ -0,0 +1,34 @@ +class GeneratorContextManager(object): + def __init__(self, gen): + self.gen = gen + + def __context__(self): + return self + + def __enter__(self): + try: + return self.gen.next() + except StopIteration: + raise RuntimeError("generator didn't yield") + + def __exit__(self, type, value, traceback): + if type is None: + try: + self.gen.next() + except StopIteration: + return + else: + raise RuntimeError("generator didn't stop") + else: + try: + self.gen.throw(type, value, traceback) + except (type, StopIteration): + return + else: + raise RuntimeError("generator caught exception") + +def contextmanager(func): + def helper(*args, **kwds): + return GeneratorContextManager(func(*args, **kwds)) + return helper + |
