Skip to content

Instantly share code, notes, and snippets.

@akaptur
Created October 29, 2013 15:45
Show Gist options
  • Save akaptur/7217177 to your computer and use it in GitHub Desktop.
Save akaptur/7217177 to your computer and use it in GitHub Desktop.
Possible misunderstanding of how `cause` interacts with context manager silencing in python 3
>>> class FalseContext(object):
... def __enter__(self):
... l.append('i')
... return self
... def __exit__(self, exc_type, exc_val, exc_tb):
... l.append('o')
... return False
...
>>> class TrueContext(object):
... def __enter__(self):
... l.append('i')
... return self
... def __exit__(self, exc_type, exc_val, exc_tb):
... l.append('o')
... return True
...
>>> with TrueContext():
... raise Exception
...
>>> with FalseContext():
... raise Exception
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
Exception
>>> with TrueContext():
... raise Exception from ValueError
...
>>> with FalseContext():
... raise Exception from ValueError
...
ValueError
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
Exception
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment