Console and system encoding tuning.
Mainly to support standalone JAR w/ Jython 2.7.2.
@@ -25,10 +25,7 @@ from .unic import unic
CONSOLE_ENCODING = get_console_encoding()
SYSTEM_ENCODING = get_system_encoding()
-# IronPython and Jython streams have wrong encoding if outputs are redirected.
-# Jython gets it right if PYTHONIOENCODING is set, though.
-NON_TTY_ENCODING_CAN_BE_TRUSTED = \
- not (IRONPYTHON or JYTHON and not os.getenv('PYTHONIOENCODING'))
+PYTHONIOENCODING = os.getenv('PYTHONIOENCODING')
def console_decode(string, encoding=CONSOLE_ENCODING, force=False):
@@ -70,9 +67,14 @@ def console_encode(string, errors='replace', stream=sys.__stdout__):
def _get_console_encoding(stream):
encoding = getattr(stream, 'encoding', None)
- if encoding and (NON_TTY_ENCODING_CAN_BE_TRUSTED or isatty(stream)):
+ if isatty(stream):
+ return encoding or CONSOLE_ENCODING
+ if PYTHONIOENCODING:
+ return PYTHONIOENCODING
+ # Jython and IronPython have wrong encoding if outputs are redirected.
+ if encoding and not (JYTHON or IRONPYTHON):
return encoding
- return CONSOLE_ENCODING if isatty(stream) else SYSTEM_ENCODING
+ return SYSTEM_ENCODING
# These interpreters handle communication with system APIs using Unicode.
@@ -92,7 +94,12 @@ else:
_SYSTEM_ENCODING = SYSTEM_ENCODING if not JYTHON else 'UTF-8'
def system_decode(string):
- """Decodes bytes from system (e.g. cli args or env vars) to Unicode."""
+ """Decodes bytes from system (e.g. cli args or env vars) to Unicode.
+
+ Depending on the usage, at least cli args may already be Unicode.
+ """
+ if is_unicode(string):
+ return string
try:
return string.decode(_SYSTEM_ENCODING)
except UnicodeError:
