Skip to content

Commit efb257a

Browse files
ngnpopecarltongibson
authored andcommitted
Fixed #30324 -- Forced utf-8 encoding when loading the template for the technical 500 debug page.
Regression in 50b8493. Related to ea542a9.
1 parent 9012033 commit efb257a

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

django/views/debug.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,14 +328,14 @@ def get_traceback_data(self):
328328

329329
def get_traceback_html(self):
330330
"""Return HTML version of debug 500 HTTP error page."""
331-
with Path(CURRENT_DIR, 'templates', 'technical_500.html').open() as fh:
331+
with Path(CURRENT_DIR, 'templates', 'technical_500.html').open(encoding='utf-8') as fh:
332332
t = DEBUG_ENGINE.from_string(fh.read())
333333
c = Context(self.get_traceback_data(), use_l10n=False)
334334
return t.render(c)
335335

336336
def get_traceback_text(self):
337337
"""Return plain text version of debug 500 HTTP error page."""
338-
with Path(CURRENT_DIR, 'templates', 'technical_500.txt').open() as fh:
338+
with Path(CURRENT_DIR, 'templates', 'technical_500.txt').open(encoding='utf-8') as fh:
339339
t = DEBUG_ENGINE.from_string(fh.read())
340340
c = Context(self.get_traceback_data(), autoescape=False, use_l10n=False)
341341
return t.render(c)

docs/releases/2.2.1.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@ Bugfixes
1414
(:ticket:`30307`).
1515

1616
* Added compatibility for ``psycopg2`` 2.8 (:ticket:`30331`).
17+
18+
* Fixed a regression in Django 2.2 that caused a crash when loading the
19+
template for the technical 500 debug page (:ticket:`30324`).

tests/view_tests/tests/test_debug.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import threading
88
from io import StringIO
99
from pathlib import Path
10+
from unittest import mock
1011

1112
from django.core import mail
1213
from django.core.files.uploadedfile import SimpleUploadedFile
@@ -20,7 +21,8 @@
2021
from django.utils.safestring import mark_safe
2122
from django.views.debug import (
2223
CLEANSED_SUBSTITUTE, CallableSettingWrapper, ExceptionReporter,
23-
cleanse_setting, technical_500_response,
24+
Path as DebugPath, cleanse_setting, default_urlconf,
25+
technical_404_response, technical_500_response,
2426
)
2527

2628
from ..views import (
@@ -648,6 +650,20 @@ def __str__(self):
648650
text = reporter.get_traceback_text()
649651
self.assertIn('USER: [unable to retrieve the current user]', text)
650652

653+
def test_template_encoding(self):
654+
"""
655+
The templates are loaded directly, not via a template loader, and
656+
should be opened as utf-8 charset as is the default specified on
657+
template engines.
658+
"""
659+
reporter = ExceptionReporter(None, None, None, None)
660+
with mock.patch.object(DebugPath, 'open') as m:
661+
reporter.get_traceback_html()
662+
m.assert_called_once_with(encoding='utf-8')
663+
m.reset_mock()
664+
reporter.get_traceback_text()
665+
m.assert_called_once_with(encoding='utf-8')
666+
651667

652668
class PlainTextReportTests(SimpleTestCase):
653669
rf = RequestFactory()

0 commit comments

Comments
 (0)