﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
25761	Re-raised exceptions with __cause__ should also set __traceback__ on the exception	Raphaël Hertzog	nobody	"In bug https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=802677 we have a report that testtools is broken when handling database exceptions raised by Django. This has been brought to the upstream developers of testtools who were quick to point out that Django is at fault:
https://github.com/testing-cabal/testtools/issues/162#issuecomment-156891988

Django is setting the {{{__cause__}}} attribute thus mimicking Python's 3 behaviour, but not ensuring that the associated exception has a usable {{{__traceback__}}} attribute. The traceback2 module (backport of Python 3's traceback) relies on this... thus Django should also make sure that the exception has the expected attribute when it sets {{{__cause__}}}.

This needs to be fixed at least in django/db/utils.py ({{{DatabaseErrorWrapper.__exit__()}}}). But there are other cases where Django is setting {{{__cause___}}} in django/db/migrations/loader.py, 
django/utils/dictconfig.py and django/utils/timezone.py. It looks like they might have the same problem.

A fix for the first case might possibly be something like this (untested):
{{{
--- a/django/db/utils.py
+++ b/django/db/utils.py
@@ -91,6 +91,8 @@ class DatabaseErrorWrapper(object):
             if issubclass(exc_type, db_exc_type):
                 dj_exc_value = dj_exc_type(*exc_value.args)
                 dj_exc_value.__cause__ = exc_value
+                if not hasattr(exc_value, '__traceback__'):
+                    setattr(exc_value, '__traceback__', traceback)
                 # Only set the 'errors_occurred' flag for errors that may make
                 # the connection unusable.
                 if dj_exc_type not in (DataError, IntegrityError):
}}}"	Bug	closed	Database layer (models, ORM)	1.8	Normal	fixed			Accepted	1	0	0	0	0	0
