Commit 9f672d71 authored by Mark Florisson's avatar Mark Florisson

Provide portable error messages for source file decoding errors

parent 3a533838
...@@ -357,10 +357,28 @@ class Context(object): ...@@ -357,10 +357,28 @@ class Context(object):
tree = Parsing.p_module(s, pxd, full_module_name) tree = Parsing.p_module(s, pxd, full_module_name)
finally: finally:
f.close() f.close()
except UnicodeDecodeError, msg: except UnicodeDecodeError, e:
#import traceback #import traceback
#traceback.print_exc() #traceback.print_exc()
error((source_desc, 0, 0), "Decoding error, missing or incorrect coding=<encoding-name> at top of source (%s)" % msg) line = 1
column = 0
msg = e.args[-1]
position = e.args[2]
encoding = e.args[0]
for idx, c in enumerate(open(source_filename, "rb").read()):
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 > 0: if Errors.num_errors > 0:
raise CompileError raise CompileError
return tree return tree
......
...@@ -6,5 +6,5 @@ Tr ...@@ -6,5 +6,5 @@ Tr
""" """
_ERRORS = u""" _ERRORS = u"""
0:0:Decoding error, missing or incorrect coding=<encoding-name> at top of source ('ascii' codec can't decode byte 0x8f in position 36: ordinal not in range(128)) 5:3: Decoding error, missing or incorrect coding=<encoding-name> at top of source (cannot decode with encoding 'ascii': ordinal not in range(128))
""" """
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