From bda2ad7ddace63e3f744ea089532b1f3c25d569d 2014-01-22 23:13:20 From: MinRK Date: 2014-01-22 23:13:20 Subject: [PATCH] add prefer_stream arg to utils.encoding.getdefaultencoding sometimes the stream encoding should be ignored, and start with locale (e.g. Popen on Windows Python 2). Default behavior is unchanged. --- diff --git a/IPython/utils/encoding.py b/IPython/utils/encoding.py index 7eb7f2a..387a247 100644 --- a/IPython/utils/encoding.py +++ b/IPython/utils/encoding.py @@ -35,16 +35,20 @@ def get_stream_enc(stream, default=None): # to match the environment. # Defined here as central function, so if we find better choices, we # won't need to make changes all over IPython. -def getdefaultencoding(): +def getdefaultencoding(prefer_stream=True): """Return IPython's guess for the default encoding for bytes as text. - - Asks for stdin.encoding first, to match the calling Terminal, but that - is often None for subprocesses. Fall back on locale.getpreferredencoding() + + If prefer_stream is True (default), asks for stdin.encoding first, + to match the calling Terminal, but that is often None for subprocesses. + + Then fall back on locale.getpreferredencoding(), which should be a sensible platform default (that respects LANG environment), and finally to sys.getdefaultencoding() which is the most conservative option, - and usually ASCII. + and usually ASCII on Python 2 or UTF8 on Python 3. """ - enc = get_stream_enc(sys.stdin) + enc = None + if prefer_stream: + enc = get_stream_enc(sys.stdin) if not enc or enc=='ascii': try: # There are reports of getpreferredencoding raising errors