Skip to content

Instantly share code, notes, and snippets.

@satiani
Last active December 25, 2015 00:09
Show Gist options
  • Save satiani/6885627 to your computer and use it in GitHub Desktop.
Save satiani/6885627 to your computer and use it in GitHub Desktop.
import sys
class TestContext(object):
def __init__(self):
self.entered = False
self.exited = False
def __enter__(self):
self.entered = True
def __exit__(self, exc_type, exc_value, tb):
self.exited = True
sys.exc_clear()
print "In __exit__", sys.exc_info()
c = TestContext()
try:
with c:
raise Exception('dummy')
except:
pass
# prints True
print c.entered
# prints True
print c.exited
print "At the end: ", sys.exc_info()
# Output:
# In __exit__ (None, None, None)
# True
# True
# At the end: (<type 'exceptions.Exception'>, Exception('dummy',), <traceback object at 0x1c053b0>)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment