Fix encoding detection on Jython.
Fixes regression caused by
@@ -51,13 +51,11 @@ def console_decode(string, encoding=CONSOLE_ENCODING, force=False):
def console_encode(string, errors='replace', stream=sys.__stdout__):
"""Encodes Unicode to bytes in console or system encoding.
- Uses the encoding of the `stream` if it has one. Otherwise uses the console
- encoding or the system encoding depending is the stream a console (as
- detected by `isatty`) or not.
+ Determines the encoding to use based on the given stream and system
+ configuration. On Python 3 and IronPython returns Unicode, otherwise
+ returns bytes.
"""
- encoding = getattr(stream, 'encoding', None)
- if not encoding:
- encoding = CONSOLE_ENCODING if isatty(stream) else SYSTEM_ENCODING
+ encoding = _get_console_encoding(stream)
if PY3 and encoding != 'UTF-8':
return string.encode(encoding, errors).decode(encoding)
if PY3 or IRONPYTHON:
@@ -65,6 +63,19 @@ def console_encode(string, errors='replace', stream=sys.__stdout__):
return string.encode(encoding, errors)
+def _get_console_encoding(stream):
+ # On Python 2 stdout and stderr don't have encoding set reliably if outputs
+ # are redirected outside Python itself. With Python encoding is None in
+ # this case, and with Jython and IronPython encoding seems to be set to
+ # the same value as when streams are not redirected (which is wrong and
+ # can cause problems on Windows).
+ if PY3 or isatty(stream):
+ encoding = getattr(stream, 'encoding', None)
+ if encoding:
+ return encoding
+ return CONSOLE_ENCODING if isatty(stream) else SYSTEM_ENCODING
+
+
# These interpreters handle communication with system APIs using Unicode.
if PY3 or JYTHON or IRONPYTHON:
