# HG changeset patch # User hodgestar # Date 1286901706 0 # Node ID a14913740f530ee34853be5f2e614fdf5d484e2a # Parent c00b8e96886978abcfbd20df4d725744ef66fb17 Correctly handle the various possible values of sys.stdout/in.encoding. diff --git a/bitten/build/api.py b/bitten/build/api.py --- a/bitten/build/api.py +++ b/bitten/build/api.py @@ -36,15 +36,21 @@ """Encode input for call. Input must be unicode or utf-8 string.""" if not isinstance(text, unicode): text = unicode(text, 'utf-8') - return text.encode( - sys.getfilesystemencoding() or sys.stdin.encoding, 'replace') + # sys.stdin.encoding might be None (if stdin is directed from a file) + # sys.stdin.encoding might be missing (if it is a StringIO object) + encoding = sys.getfilesystemencoding() or \ + getattr(sys.stdin, 'encoding', None) or 'utf-8' + return text.encode(encoding, 'replace') def _decode(text): """Decode output from call.""" try: return text.decode('utf-8') except UnicodeDecodeError: - return text.decode(getattr(sys.stdout, 'encoding', 'utf-8'), 'replace') + # sys.stdout.encoding might be None (if stdout is directed to a file) + # sys.stdout.encoding might be missing (if it is a StringIO object) + encoding = getattr(sys.stdout, 'encoding', None) or 'utf-8' + return text.decode(encoding, 'replace') class CommandLine(object):