[issue25538] Traceback from __exit__ method is misleading

R. David Murray report at bugs.python.org
Tue Nov 3 10:20:48 EST 2015


R. David Murray added the comment:

What caught me out was more like this (but more complicated so it wasn't obvious the line mentioned wasn't part of the exception chain; in fact it looked like it was since it was a line that could very well have raised the exception in question):

    rdmurray at pydev:~/python/p36>cat temp.py
    def do_something():
        try:
            foo()
        except Exception:
            print('caught in do_something')
            raise

    def foo():
        with cm():
            print('all is well')

    class cm:

        def __enter__(self):
            return self

        def __exit__(*args, **kw):
            raise Exception('exception in __exit__')

    do_something()
    rdmurray at pydev:~/python/p36>./python temp.py
    all is well
    caught in do_something
    Traceback (most recent call last):
      File "temp.py", line 20, in <module>
        do_something()
      File "temp.py", line 3, in do_something
        foo()
      File "temp.py", line 10, in foo
        print('all is well')
      File "temp.py", line 18, in __exit__
        raise Exception('exception in __exit__')
    Exception: exception in __exit__

The confusing line in the traceback is "print('all is well')", which obviously isn't raising the exception.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue25538>
_______________________________________


More information about the Python-bugs-list mailing list