# HG changeset patch # User David Anderson # Date 1392495888 28800 # Node ID 10388f39ac2b2e02ea5e1fb372d35f87766396a5 # Parent 7d323620251fad8d1602bb1c6b3340010cb071b8 Take two at console decoding. diff -r 7d323620251f -r 10388f39ac2b ambuild2/util.py --- a/ambuild2/util.py Sat Feb 15 12:05:52 2014 -0800 +++ b/ambuild2/util.py Sat Feb 15 12:24:48 2014 -0800 @@ -88,8 +88,8 @@ def WaitForProcess(process): out, err = process.communicate() - process.stdoutText = DecodeConsoleText(out) - process.stderrText = DecodeConsoleText(err) + process.stdoutText = DecodeConsoleText(sys.stdout, process.stdout, out) + process.stderrText = DecodeConsoleText(sys.stderr, process.stderr, err) return process.returncode def CreateProcess(argv, executable = None): @@ -364,11 +364,23 @@ ConsoleNormal) raise -def DecodeConsoleText(text): +def try_decode_with_pipe(pipe, text): try: - return text.decode(locale.getpreferredencoding(), 'replace') + if hasattr(pipe, 'encoding') and pipe.encoding: + return text.decode(pipe.encoding, 'replace') except: - return text.decode('utf8') + return None + +def DecodeConsoleText(origin, pipe, text): + result = try_decode_with_pipe(pipe, text) + if result is None: + result = try_decode_with_pipe(origin, text) + if result is None: + try: + result = text.decode(locale.getpreferredencoding(), 'replace') + except: + result = text + return result.encode('utf8') def WriteEncodedText(fd, text): if not hasattr(fd, 'encoding') or fd.encoding == None: