Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 46 lines (35 sloc) 917 Bytes
from __future__ import with_statement
from contextlib import closing, contextmanager
def foo():
print "started foo()"
print "foo(): no exception"
print "stopped foo()"
class FooCtx:
def __enter__(self):
print "FooCtx.__enter__"
def __exit__(self, type, value, tb):
print "FooCtx.__exit__"
if tb is None:
print "FooCtx.__exit__: no exception"
class ClosableFoo:
def __init__(self):
print "Foo.__init__()"
def close(self):
print "Foo.closed()"
def test_ClosableFoo():
with closing(ClosableFoo()):
print "test with Foo()"
raise Exception("evil")
def test_foo():
with foo():
print "test with foo()"
raise Exception("evil")
def test_FooCtx():
with FooCtx():
print "test with FooCtx"
Jump to Line
Something went wrong with that request. Please try again.