Skip to content

better reporting for python 3.3+ chained exceptions #138

Closed
@pytestbot

Description

@pytestbot

Originally reported by: Anonymous


With Python 3, py.test doesn't propery display exception chains. The following script

#!python
def test_chained_exception():
    try:
        raise ValueError(17)
    except Exception as exc:
        raise ValueError(23) from exc

if __name__ == "__main__":
    test_chained_exception()

when run with Python 3.2 properly outputs the exception chain:

$ python3.2 ~/bug.py
Traceback (most recent call last):
  File "/Users/walter/bug.py", line 3, in test_chained_exception
    raise ValueError(17)
ValueError: 17

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/walter/bug.py", line 8, in <module>
    test_chained_exception()
  File "/Users/walter/bug.py", line 5, in test_chained_exception
    raise ValueError(23) from exc
ValueError: 23

but when run unter py.test only the outermost exception is displayed:

$ python3.2 -mpytest bug.py
================ test session starts ================
platform darwin -- Python 3.2.2 -- pytest-2.1.2
collected 1 items 

bug.py F

====================== FAILURES =====================
_______________ test_chained_exception ______________

    def test_chained_exception():
        try:
            raise ValueError(17)
        except Exception as exc:
>           raise ValueError(23) from exc
E     ValueError: 23

bug.py:5: ValueError
============= 1 failed in 0.01 seconds ==============

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: tracebacksrelated to displaying and handling of tracebackstype: enhancementnew feature or API change, should be merged into features branch

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions