diff --git a/django/template/__init__.py b/django/template/__init__.py index 33ea0aa60f..5d238d427c 100644 --- a/django/template/__init__.py +++ b/django/template/__init__.py @@ -104,20 +104,7 @@ builtins = [] invalid_var_format_string = None class TemplateSyntaxError(Exception): - def __str__(self): - try: - import cStringIO as StringIO - except ImportError: - import StringIO - output = StringIO.StringIO() - output.write(Exception.__str__(self)) - # Check if we wrapped an exception and print that too. - if hasattr(self, 'exc_info'): - import traceback - output.write('\n\nOriginal ') - e = self.exc_info - traceback.print_exception(e[0], e[1], e[2], 500, output) - return output.getvalue() + pass class TemplateDoesNotExist(Exception): pass diff --git a/django/template/debug.py b/django/template/debug.py index 382fb75ebd..c21fb50b3e 100644 --- a/django/template/debug.py +++ b/django/template/debug.py @@ -76,10 +76,11 @@ class DebugNodeList(NodeList): raise except Exception, e: from sys import exc_info - wrapped = TemplateSyntaxError(u'Caught an exception while rendering: %s' % force_unicode(e, errors='replace')) + wrapped = TemplateSyntaxError(u'Caught %s while rendering: %s' % + (e.__class__.__name__, force_unicode(e, errors='replace'))) wrapped.source = node.source wrapped.exc_info = exc_info() - raise wrapped + raise wrapped, None, wrapped.exc_info[2] return result class DebugVariableNode(VariableNode): diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py index 0d283dd9b7..1af3637158 100644 --- a/tests/regressiontests/templates/tests.py +++ b/tests/regressiontests/templates/tests.py @@ -216,7 +216,7 @@ class Templates(unittest.TestCase): except TemplateSyntaxError, e: # Assert that we are getting the template syntax error and not the # string encoding error. - self.assertEquals(e.args[0], "Caught an exception while rendering: Reverse for 'will_not_match' with arguments '()' and keyword arguments '{}' not found.") + self.assertEquals(e.args[0], "Caught NoReverseMatch while rendering: Reverse for 'will_not_match' with arguments '()' and keyword arguments '{}' not found.") settings.SETTINGS_MODULE = old_settings_module settings.TEMPLATE_DEBUG = old_template_debug diff --git a/tests/regressiontests/views/templatetags/__init__.py b/tests/regressiontests/views/templatetags/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/regressiontests/views/templatetags/debugtags.py b/tests/regressiontests/views/templatetags/debugtags.py new file mode 100644 index 0000000000..9b2c6611d8 --- /dev/null +++ b/tests/regressiontests/views/templatetags/debugtags.py @@ -0,0 +1,10 @@ +from django import template + +from regressiontests.views import BrokenException + +register = template.Library() + +@register.simple_tag +def go_boom(arg): + raise BrokenException(arg) + diff --git a/tests/regressiontests/views/tests/debug.py b/tests/regressiontests/views/tests/debug.py index 0ff4ec3d6b..e072d4e154 100644 --- a/tests/regressiontests/views/tests/debug.py +++ b/tests/regressiontests/views/tests/debug.py @@ -1,7 +1,10 @@ +import inspect + from django.conf import settings from django.core.files.uploadedfile import SimpleUploadedFile from django.test import TestCase from django.core.urlresolvers import reverse +from django.template import TemplateSyntaxError from regressiontests.views import BrokenException, except_args @@ -9,9 +12,12 @@ class DebugViewTests(TestCase): def setUp(self): self.old_debug = settings.DEBUG settings.DEBUG = True + self.old_template_debug = settings.TEMPLATE_DEBUG + settings.TEMPLATE_DEBUG = True def tearDown(self): settings.DEBUG = self.old_debug + settings.TEMPLATE_DEBUG = self.old_template_debug def test_files(self): response = self.client.get('/views/raises/') @@ -33,3 +39,13 @@ class DebugViewTests(TestCase): self.assertRaises(BrokenException, self.client.get, reverse('view_exception', args=(n,))) + def test_template_exceptions(self): + for n in range(len(except_args)): + try: + self.client.get(reverse('template_exception', args=(n,))) + except TemplateSyntaxError, e: + raising_loc = inspect.trace()[-1][-2][0].strip() + self.failIf(raising_loc.find('raise BrokenException') == -1, + "Failed to find 'raise BrokenException' in last frame of traceback, instead found: %s" % + raising_loc) + diff --git a/tests/regressiontests/views/urls.py b/tests/regressiontests/views/urls.py index 6d36bb9f62..a072e77131 100644 --- a/tests/regressiontests/views/urls.py +++ b/tests/regressiontests/views/urls.py @@ -109,4 +109,5 @@ urlpatterns += patterns('django.views.generic.simple', urlpatterns += patterns('regressiontests.views.views', url(r'view_exception/(?P\d+)/$', 'view_exception', name='view_exception'), + url(r'template_exception/(?P\d+)/$', 'template_exception', name='template_exception'), ) diff --git a/tests/regressiontests/views/views.py b/tests/regressiontests/views/views.py index e1167f1b63..445b4ed312 100644 --- a/tests/regressiontests/views/views.py +++ b/tests/regressiontests/views/views.py @@ -5,6 +5,7 @@ from django import forms from django.views.debug import technical_500_response from django.views.generic.create_update import create_object from django.core.urlresolvers import get_resolver +from django.shortcuts import render_to_response from regressiontests.views import BrokenException, except_args @@ -52,3 +53,7 @@ def redirect(request): def view_exception(request, n): raise BrokenException(except_args[int(n)]) +def template_exception(request, n): + return render_to_response('debug/template_exception.html', + {'arg': except_args[int(n)]}) +