summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan Clelland <ian@fullfactor.com>2012-09-26 12:41:47 -0700
committerLuke Plant <L.Plant.98@cantab.net>2012-10-26 02:38:15 +0100
commit7b6978553aa8cde493f02ddd93bbd711afbf28ae (patch)
tree0b6b818cbdc655d9914274a0bd9e98825fa3572d
parentc426d5665095e7b91df69e54d4773180b12ff548 (diff)
[1.5.x] PEP 302 source loaders already decode appropriately
Backport of c11aba1775ba0562251e4b2dba78da6a86ff338c from master
-rw-r--r--django/views/debug.py22
1 files changed, 13 insertions, 9 deletions
diff --git a/django/views/debug.py b/django/views/debug.py
index c59fe31fc8..aaa7e40efe 100644
--- a/django/views/debug.py
+++ b/django/views/debug.py
@@ -354,15 +354,19 @@ class ExceptionReporter(object):
if source is None:
return None, [], None, []
- encoding = 'ascii'
- for line in source[:2]:
- # File coding may be specified. Match pattern from PEP-263
- # (http://www.python.org/dev/peps/pep-0263/)
- match = re.search(br'coding[:=]\s*([-\w.]+)', line)
- if match:
- encoding = match.group(1).decode('ascii')
- break
- source = [six.text_type(sline, encoding, 'replace') for sline in source]
+ # If we just read the source from a file, or if the loader did not
+ # apply tokenize.detect_encoding to decode the source into a Unicode
+ # string, then we should do that ourselves.
+ if isinstance(source[0], six.binary_type):
+ encoding = 'ascii'
+ for line in source[:2]:
+ # File coding may be specified. Match pattern from PEP-263
+ # (http://www.python.org/dev/peps/pep-0263/)
+ match = re.search(br'coding[:=]\s*([-\w.]+)', line)
+ if match:
+ encoding = match.group(1).decode('ascii')
+ break
+ source = [six.text_type(sline, encoding, 'replace') for sline in source]
lower_bound = max(0, lineno - context_lines)
upper_bound = lineno + context_lines