Fix UnicodeEncodeError when writing Unicode text to redirected stdout or stderr under Python 2.x (r=dvander).
authorScott Ehlert <ds@alliedmods.net>
Sat Feb 01 02:12:32 2014 -0600 (2014-02-01)
changeset 22411f656dae71a
parent 223 0cf7fb24b5fa
child 225 0b768cfdbace
Fix UnicodeEncodeError when writing Unicode text to redirected stdout or stderr under Python 2.x (r=dvander).
ambuild2/task.py
ambuild2/util.py
     1.1 --- a/ambuild2/task.py	Fri Jan 31 23:58:52 2014 -0800
     1.2 +++ b/ambuild2/task.py	Sat Feb 01 02:12:32 2014 -0600
     1.3 @@ -477,11 +477,11 @@
     1.4        util.ConsoleNormal
     1.5      )
     1.6      if len(message['stdout']):
     1.7 -      sys.stdout.write(message['stdout'])
     1.8 +      util.WriteEncodedText(sys.stdout, message['stdout'])
     1.9        if message['stdout'][-1] != '\n':
    1.10          sys.stdout.write('\n')
    1.11      if len(message['stderr']):
    1.12 -      sys.stderr.write(message['stderr'])
    1.13 +      util.WriteEncodedText(sys.stderr, message['stderr'])
    1.14        if message['stderr'][-1] != '\n':
    1.15          sys.stderr.write('\n')
    1.16  
     2.1 --- a/ambuild2/util.py	Fri Jan 31 23:58:52 2014 -0800
     2.2 +++ b/ambuild2/util.py	Sat Feb 01 02:12:32 2014 -0600
     2.3 @@ -1,7 +1,7 @@
     2.4  # vim: set sts=2 ts=8 sw=2 tw=99 et: 
     2.5  import errno
     2.6  import subprocess
     2.7 -import re, os, sys
     2.8 +import re, os, sys, locale
     2.9  import multiprocessing
    2.10  try:
    2.11    import __builtin__ as builtins
    2.12 @@ -353,3 +353,7 @@
    2.13                ConsoleNormal)
    2.14        raise
    2.15  
    2.16 +def WriteEncodedText(fd, text):
    2.17 +  if not hasattr(fd, 'encoding') or fd.encoding == None:
    2.18 +    text = text.encode(locale.getpreferredencoding(), 'replace')
    2.19 +  fd.write(text)