Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[1.5.x] PEP 302 source loaders already decode appropriately

Backport of c11aba1775ba0562251e4b2dba78da6a86ff338c from master
  • Loading branch information...
commit 7b6978553aa8cde493f02ddd93bbd711afbf28ae 1 parent c426d56
@clelland clelland authored spookylukey committed
Showing with 13 additions and 9 deletions.
  1. +13 −9 django/views/debug.py
View
22 django/views/debug.py
@@ -354,15 +354,19 @@ def _get_lines_from_file(self, filename, lineno, context_lines, loader=None, mod
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
Please sign in to comment.
Something went wrong with that request. Please try again.