Show More
@@ -35,16 +35,20 b' def get_stream_enc(stream, default=None):' | |||||
35 | # to match the environment. |
|
35 | # to match the environment. | |
36 | # Defined here as central function, so if we find better choices, we |
|
36 | # Defined here as central function, so if we find better choices, we | |
37 | # won't need to make changes all over IPython. |
|
37 | # won't need to make changes all over IPython. | |
38 | def getdefaultencoding(): |
|
38 | def getdefaultencoding(prefer_stream=True): | |
39 | """Return IPython's guess for the default encoding for bytes as text. |
|
39 | """Return IPython's guess for the default encoding for bytes as text. | |
40 |
|
40 | |||
41 | Asks for stdin.encoding first, to match the calling Terminal, but that |
|
41 | If prefer_stream is True (default), asks for stdin.encoding first, | |
42 | is often None for subprocesses. Fall back on locale.getpreferredencoding() |
|
42 | to match the calling Terminal, but that is often None for subprocesses. | |
|
43 | ||||
|
44 | Then fall back on locale.getpreferredencoding(), | |||
43 | which should be a sensible platform default (that respects LANG environment), |
|
45 | which should be a sensible platform default (that respects LANG environment), | |
44 | and finally to sys.getdefaultencoding() which is the most conservative option, |
|
46 | and finally to sys.getdefaultencoding() which is the most conservative option, | |
45 | and usually ASCII. |
|
47 | and usually ASCII on Python 2 or UTF8 on Python 3. | |
46 | """ |
|
48 | """ | |
47 | enc = get_stream_enc(sys.stdin) |
|
49 | enc = None | |
|
50 | if prefer_stream: | |||
|
51 | enc = get_stream_enc(sys.stdin) | |||
48 | if not enc or enc=='ascii': |
|
52 | if not enc or enc=='ascii': | |
49 | try: |
|
53 | try: | |
50 | # There are reports of getpreferredencoding raising errors |
|
54 | # There are reports of getpreferredencoding raising errors |
General Comments 0
You need to be logged in to leave comments.
Login now