Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #11461: Ensured complete traceback is available on the debug pa…

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

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12725 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 50e46c017e8a904725721ec45a3a214045bd688f 1 parent 5a35619
Karen Tracey authored March 08, 2010
15  django/template/__init__.py
@@ -104,20 +104,7 @@
104 104
 invalid_var_format_string = None
105 105
 
106 106
 class TemplateSyntaxError(Exception):
107  
-    def __str__(self):
108  
-        try:
109  
-            import cStringIO as StringIO
110  
-        except ImportError:
111  
-            import StringIO
112  
-        output = StringIO.StringIO()
113  
-        output.write(Exception.__str__(self))
114  
-        # Check if we wrapped an exception and print that too.
115  
-        if hasattr(self, 'exc_info'):
116  
-            import traceback
117  
-            output.write('\n\nOriginal ')
118  
-            e = self.exc_info
119  
-            traceback.print_exception(e[0], e[1], e[2], 500, output)
120  
-        return output.getvalue()
  107
+    pass
121 108
 
122 109
 class TemplateDoesNotExist(Exception):
123 110
     pass
5  django/template/debug.py
@@ -76,10 +76,11 @@ def render_node(self, node, context):
76 76
             raise
77 77
         except Exception, e:
78 78
             from sys import exc_info
79  
-            wrapped = TemplateSyntaxError(u'Caught an exception while rendering: %s' % force_unicode(e, errors='replace'))
  79
+            wrapped = TemplateSyntaxError(u'Caught %s while rendering: %s' %
  80
+                (e.__class__.__name__, force_unicode(e, errors='replace')))
80 81
             wrapped.source = node.source
81 82
             wrapped.exc_info = exc_info()
82  
-            raise wrapped
  83
+            raise wrapped, None, wrapped.exc_info[2]
83 84
         return result
84 85
 
85 86
 class DebugVariableNode(VariableNode):
2  tests/regressiontests/templates/tests.py
@@ -216,7 +216,7 @@ def test_url_reverse_no_settings_module(self):
216 216
         except TemplateSyntaxError, e:
217 217
             # Assert that we are getting the template syntax error and not the
218 218
             # string encoding error.
219  
-            self.assertEquals(e.args[0], "Caught an exception while rendering: Reverse for 'will_not_match' with arguments '()' and keyword arguments '{}' not found.")
  219
+            self.assertEquals(e.args[0], "Caught NoReverseMatch while rendering: Reverse for 'will_not_match' with arguments '()' and keyword arguments '{}' not found.")
220 220
 
221 221
         settings.SETTINGS_MODULE = old_settings_module
222 222
         settings.TEMPLATE_DEBUG = old_template_debug
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 50e46c0

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