The exc_value parameter from __exit __ () (context manager) is a string instead of Exception (Python 2.6)

I tried to get confused with context managers and was 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 welcome this problem:

 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 

As of 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 

As of 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 

As of 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 

I understand that exc_value should always be an exception. Am I doing something wrong? Is there something I misunderstood? Is this a known issue?

References

+1
python exception contextmanager
source share
1 answer

This was a bug in Python 2.6, see question 7853 . It was fixed for Python 2.7a3, but has never been addressed to branch 2.6.

In other words, you did nothing, Python did.

Unfortunately, there is no real work but to cope with the fact that you do not have an exception instance in version 2.6, but only for a string value.

+2
source share

All Articles