diff --git a/platformio/builder/tools/piomisc.py b/platformio/builder/tools/piomisc.py index 20768671..9c7bf548 100644 --- a/platformio/builder/tools/piomisc.py +++ b/platformio/builder/tools/piomisc.py @@ -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( diff --git a/platformio/fs.py b/platformio/fs.py index 017b653a..16dcb3e8 100644 --- a/platformio/fs.py +++ b/platformio/fs.py @@ -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)