Code

Ticket #19827: 19827-stacktrace_on_reraise.diff

File 19827-stacktrace_on_reraise.diff, 2.3 KB (added by regebro, 17 months ago)

Patch using six.

Line 
1diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py
2index c1ca947..1da6230 100644
3--- a/django/template/defaulttags.py
4+++ b/django/template/defaulttags.py
5@@ -409,7 +409,8 @@ class URLNode(Node):
6         url = ''
7         try:
8             url = reverse(view_name, args=args, kwargs=kwargs, current_app=context.current_app)
9-        except NoReverseMatch as e:
10+        except NoReverseMatch:
11+            exc_info = sys.exc_info()
12             if settings.SETTINGS_MODULE:
13                 project_name = settings.SETTINGS_MODULE.split('.')[0]
14                 try:
15@@ -421,10 +422,10 @@ class URLNode(Node):
16                         # Re-raise the original exception, not the one with
17                         # the path relative to the project. This makes a
18                         # better error message.
19-                        raise e
20+                        six.reraise(*exc_info)
21             else:
22                 if self.asvar is None:
23-                    raise e
24+                    six.reraise(*exc_info)
25 
26         if self.asvar:
27             context[self.asvar] = url
28diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py
29index 02d3460..267d5cc 100644
30--- a/tests/regressiontests/templates/tests.py
31+++ b/tests/regressiontests/templates/tests.py
32@@ -372,6 +372,23 @@ class Templates(TestCase):
33         with self.assertRaises(urlresolvers.NoReverseMatch):
34             t.render(c)
35 
36+    @override_settings(TEMPLATE_STRING_IF_INVALID='%s is invalid', SETTINGS_MODULE='also_something')
37+    def test_url_reverse_view_name(self):
38+        # Regression test for #19827
39+        from django.template import Template, Context
40+
41+        t = Template('{% url will_not_match %}')
42+        c = Context()
43+        try:
44+            t.render(c)
45+        except urlresolvers.NoReverseMatch:
46+            tb = sys.exc_info()[2]
47+            depth = 0
48+            while tb.tb_next is not None:
49+                tb = tb.tb_next
50+                depth += 1
51+            self.assertTrue(depth > 5, "The traceback context was lost when reraising the traceback. See #19827")
52+
53     def test_url_explicit_exception_for_old_syntax_at_run_time(self):
54         # Regression test for #19280
55         t = Template('{% url path.to.view %}')      # not quoted = old syntax