bpo-24959: fix unittest.assertRaises bug where traceback entries are dropped from chained exceptions by iritkatriel · Pull Request #23688 · python/cpython (original) (raw)

The use case is:

prepare()
try:
    test()
finally:
    cleanup()

We want to acquire some resource or make some environment changes for the part of the test and release the resource or restore environment after testing, so we use the try/finally block. Now, if an assertion failed inside the block, and an error occurred in the clean up code, the new exception will override the original AssertionError which is now accessible via the __context__ link. The traceback output will contain lines from the unittest code. Note that tracebecks for __context__ should be cleaned up not only if exctype is test.failureException, but if type(value.__context__) is test.failureException, type(value.__context__.__context__) is test.failureException, etc. But it may be a different issue.

I do not know the use case for __cause__, but I would clean up them too.