Better explanation about encoding error // Resolve #2796

This commit is contained in:
Ivan Kravets
2019-10-18 15:56:50 +03:00
parent 4d84d03a63
commit 77f8414c63
2 changed files with 13 additions and 4 deletions

View File

@@ -80,12 +80,14 @@ class InoToCPPConverter(object):
assert self._gcc_preprocess(contents, out_file)
contents = fs.get_file_contents(out_file)
contents = self._join_multiline_strings(contents)
fs.write_file_contents(out_file, self.append_prototypes(contents))
fs.write_file_contents(
out_file, self.append_prototypes(contents), errors="backslashreplace"
)
return out_file
def _gcc_preprocess(self, contents, out_file):
tmp_path = mkstemp()[1]
fs.write_file_contents(tmp_path, contents)
fs.write_file_contents(tmp_path, contents, errors="backslashreplace")
self.env.Execute(
self.env.VerboseAction(
'$CXX -o "{0}" -x c++ -fpreprocessed -dD -E "{1}"'.format(

View File

@@ -58,12 +58,19 @@ def get_file_contents(path):
return fp.read()
def write_file_contents(path, contents):
def write_file_contents(path, contents, errors=None):
try:
with open(path, "w") as fp:
return fp.write(contents)
except UnicodeEncodeError:
with io.open(path, "w", encoding="latin-1", errors="backslashreplace") as fp:
if errors:
click.secho(
"Warning! There is a problem with contents encoding, please remove "
"invalid characters (non-ASCII or non-UT8) in %s" % path,
fg="yellow",
err=True,
)
with io.open(path, "w", encoding="latin-1", errors=errors) as fp:
return fp.write(contents)