I tried to mess around with context managers and got a bit surprised when running my code with Python 2.6. Indeed, the exc_value
parameter seems to be a string instead of an exception.
A bit of code to hi-light this issue :
import sys
class contextmanager(object):
def __enter__(self):
pass
def __exit__(self, type_, value, traceback):
assert (type_ is None) == (value is None)
if value is not None:
print(type(value))
if __name__ == '__main__':
print(sys.version_info)
with contextmanager():
__name_ # should trigger name exception
With Python 2.7 :
<type 'exceptions.NameError'> # GOOD
Traceback (most recent call last):
File "test_conman.py", line 17, in <module>
__name_
NameError: name '__name_' is not defined
With Python 3.2 :
sys.version_info(major=3, minor=2, micro=3, releaselevel='final', serial=0)
<class 'NameError'> # GOOD
Traceback (most recent call last):
File "test_conman.py", line 17, in <module>
__name_
NameError: name '__name_' is not defined
With Python 2.6 :
(2, 6, 7, 'final', 0)
<type 'str'> # BAD
Traceback (most recent call last):
File "test_conman.py", line 17, in <module>
__name_
NameError: name '__name_' is not defined
My understanding is that exc_value
should always be an exception.
Is there anything I did wrong ?
Is there anything I misunderstood ?
Is this a known issue ?
References
What’s New in Python 2.6 : PEP 343: The
with
statementPython 2 docs :
object.__exit__(self, exc_type, exc_value, traceback)