Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.1.X] Fixed #11461: Ensured complete traceback is available on the …

…debug page when an exception is encountered during template rendering, even when running on Python 2.6 or higher. Thanks Glenn.

r12725 and r12726 from trunk.


git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.1.X@12727 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 758123a1f9b05cc3a5046311def90c9f2b1b3dbe 1 parent 9bdfe17
Karen Tracey authored
15  django/template/__init__.py
@@ -103,20 +103,7 @@
103 103
 invalid_var_format_string = None
104 104
 
105 105
 class TemplateSyntaxError(Exception):
106  
-    def __str__(self):
107  
-        try:
108  
-            import cStringIO as StringIO
109  
-        except ImportError:
110  
-            import StringIO
111  
-        output = StringIO.StringIO()
112  
-        output.write(Exception.__str__(self))
113  
-        # Check if we wrapped an exception and print that too.
114  
-        if hasattr(self, 'exc_info'):
115  
-            import traceback
116  
-            output.write('\n\nOriginal ')
117  
-            e = self.exc_info
118  
-            traceback.print_exception(e[0], e[1], e[2], 500, output)
119  
-        return output.getvalue()
  106
+    pass
120 107
 
121 108
 class TemplateDoesNotExist(Exception):
122 109
     pass
5  django/template/debug.py
@@ -75,10 +75,11 @@ def render_node(self, node, context):
75 75
             raise
76 76
         except Exception, e:
77 77
             from sys import exc_info
78  
-            wrapped = TemplateSyntaxError(u'Caught an exception while rendering: %s' % force_unicode(e, errors='replace'))
  78
+            wrapped = TemplateSyntaxError(u'Caught %s while rendering: %s' %
  79
+                (e.__class__.__name__, force_unicode(e, errors='replace')))
79 80
             wrapped.source = node.source
80 81
             wrapped.exc_info = exc_info()
81  
-            raise wrapped
  82
+            raise wrapped, None, wrapped.exc_info[2]
82 83
         return result
83 84
 
84 85
 class DebugVariableNode(VariableNode):
2  tests/regressiontests/templates/tests.py
@@ -176,7 +176,7 @@ def test_url_reverse_no_settings_module(self):
176 176
         except TemplateSyntaxError, e:
177 177
             # Assert that we are getting the template syntax error and not the
178 178
             # string encoding error.
179  
-            self.assertEquals(e.args[0], "Caught an exception while rendering: Reverse for 'will_not_match' with arguments '()' and keyword arguments '{}' not found.")
  179
+            self.assertEquals(e.args[0], "Caught NoReverseMatch while rendering: Reverse for 'will_not_match' with arguments '()' and keyword arguments '{}' not found.")
180 180
 
181 181
         settings.SETTINGS_MODULE = old_settings_module
182 182
         settings.TEMPLATE_DEBUG = old_template_debug
2  tests/regressiontests/views/templates/debug/template_exception.html
... ...
@@ -0,0 +1,2 @@
  1
+{% load debugtags %}
  2
+{% go_boom arg %}
0  tests/regressiontests/views/templatetags/__init__.py
No changes.
10  tests/regressiontests/views/templatetags/debugtags.py
... ...
@@ -0,0 +1,10 @@
  1
+from django import template
  2
+ 
  3
+from regressiontests.views import BrokenException
  4
+
  5
+register = template.Library()
  6
+
  7
+@register.simple_tag
  8
+def go_boom(arg):
  9
+    raise BrokenException(arg)
  10
+
16  tests/regressiontests/views/tests/debug.py
... ...
@@ -1,7 +1,10 @@
  1
+import inspect
  2
+
1 3
 from django.conf import settings
2 4
 from django.core.files.uploadedfile import SimpleUploadedFile
3 5
 from django.test import TestCase
4 6
 from django.core.urlresolvers import reverse
  7
+from django.template import TemplateSyntaxError
5 8
 
6 9
 from regressiontests.views import BrokenException, except_args
7 10
 
@@ -9,9 +12,12 @@ class DebugViewTests(TestCase):
9 12
     def setUp(self):
10 13
         self.old_debug = settings.DEBUG
11 14
         settings.DEBUG = True
  15
+        self.old_template_debug = settings.TEMPLATE_DEBUG 
  16
+        settings.TEMPLATE_DEBUG = True 
12 17
 
13 18
     def tearDown(self):
14 19
         settings.DEBUG = self.old_debug
  20
+        settings.TEMPLATE_DEBUG = self.old_template_debug
15 21
 
16 22
     def test_files(self):
17 23
         response = self.client.get('/views/raises/')
@@ -33,3 +39,13 @@ def test_view_exceptions(self):
33 39
             self.assertRaises(BrokenException, self.client.get,
34 40
                 reverse('view_exception', args=(n,)))
35 41
 
  42
+    def test_template_exceptions(self):
  43
+        for n in range(len(except_args)):
  44
+            try:
  45
+                self.client.get(reverse('template_exception', args=(n,)))
  46
+            except TemplateSyntaxError, e:
  47
+                raising_loc = inspect.trace()[-1][-2][0].strip()
  48
+                self.failIf(raising_loc.find('raise BrokenException') == -1,
  49
+                    "Failed to find 'raise BrokenException' in last frame of traceback, instead found: %s" % 
  50
+                        raising_loc) 
  51
+
1  tests/regressiontests/views/urls.py
@@ -109,4 +109,5 @@
109 109
 
110 110
 urlpatterns += patterns('regressiontests.views.views',
111 111
     url(r'view_exception/(?P<n>\d+)/$', 'view_exception', name='view_exception'),
  112
+    url(r'template_exception/(?P<n>\d+)/$', 'template_exception', name='template_exception'),
112 113
 )
5  tests/regressiontests/views/views.py
@@ -5,6 +5,7 @@
5 5
 from django.views.debug import technical_500_response
6 6
 from django.views.generic.create_update import create_object
7 7
 from django.core.urlresolvers import get_resolver
  8
+from django.shortcuts import render_to_response
8 9
 
9 10
 from regressiontests.views import BrokenException, except_args
10 11
 
@@ -52,3 +53,7 @@ def redirect(request):
52 53
 def view_exception(request, n):
53 54
     raise BrokenException(except_args[int(n)])
54 55
 
  56
+def template_exception(request, n):
  57
+    return render_to_response('debug/template_exception.html',
  58
+        {'arg': except_args[int(n)]})
  59
+

0 notes on commit 758123a

Please sign in to comment.
Something went wrong with that request. Please try again.