Commit 97f095fe authored by Stefan Behnel's avatar Stefan Behnel

clean up decoding error reporting by counting lines instead of reading single...

clean up decoding error reporting by counting lines instead of reading single characters to find the error position
parent 548deaf2
...@@ -351,33 +351,30 @@ class Context(object): ...@@ -351,33 +351,30 @@ class Context(object):
except UnicodeDecodeError, e: except UnicodeDecodeError, e:
#import traceback #import traceback
#traceback.print_exc() #traceback.print_exc()
line = 1 raise self._report_decode_error(source_desc, e)
column = 0
msg = e.args[-1]
position = e.args[2]
encoding = e.args[0]
with open(source_filename, "rb") as f:
byte_data = f.read()
# FIXME: make this at least a little less inefficient
for idx, c in enumerate(byte_data):
if c in (ord('\n'), '\n'):
line += 1
column = 0
if idx == position:
break
column += 1
error((source_desc, line, column),
"Decoding error, missing or incorrect coding=<encoding-name> "
"at top of source (cannot decode with encoding %r: %s)" % (encoding, msg))
if Errors.num_errors > num_errors: if Errors.num_errors > num_errors:
raise CompileError() raise CompileError()
return tree return tree
def _report_decode_error(self, source_desc, exc):
msg = exc.args[-1]
position = exc.args[2]
encoding = exc.args[0]
line = 1
column = idx = 0
with io.open(source_desc.filename, "r", encoding='iso8859-1', newline='') as f:
for line, data in enumerate(f, 1):
idx += len(data)
if idx >= position:
column = position - (idx - len(data)) + 1
break
return error((source_desc, line, column),
"Decoding error, missing or incorrect coding=<encoding-name> "
"at top of source (cannot decode with encoding %r: %s)" % (encoding, msg))
def extract_module_name(self, path, options): def extract_module_name(self, path, options):
# Find fully_qualified module name from the full pathname # Find fully_qualified module name from the full pathname
# of a source file. # of a source file.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment