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

Backport of c11aba1775ba0562251e4b2dba78da6a86ff338c from master
This commit is contained in:
Ian Clelland 2012-09-26 12:41:47 -07:00 committed by Luke Plant
parent c426d56650
commit 7b6978553a
1 changed files with 13 additions and 9 deletions

View File

@ -354,15 +354,19 @@ class ExceptionReporter(object):
if source is None: if source is None:
return None, [], None, [] return None, [], None, []
encoding = 'ascii' # If we just read the source from a file, or if the loader did not
for line in source[:2]: # apply tokenize.detect_encoding to decode the source into a Unicode
# File coding may be specified. Match pattern from PEP-263 # string, then we should do that ourselves.
# (http://www.python.org/dev/peps/pep-0263/) if isinstance(source[0], six.binary_type):
match = re.search(br'coding[:=]\s*([-\w.]+)', line) encoding = 'ascii'
if match: for line in source[:2]:
encoding = match.group(1).decode('ascii') # File coding may be specified. Match pattern from PEP-263
break # (http://www.python.org/dev/peps/pep-0263/)
source = [six.text_type(sline, encoding, 'replace') for sline in source] 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) lower_bound = max(0, lineno - context_lines)
upper_bound = lineno + context_lines upper_bound = lineno + context_lines