Consistent behavior.

This commit is contained in:
Itamar Turner-Trauring 2021-05-27 13:26:46 -04:00
parent 0425b64041
commit 8aa3391276
2 changed files with 8 additions and 3 deletions

View File

@ -12,6 +12,7 @@ if PY2:
from future.builtins import filter, map, zip, ascii, chr, hex, input, next, oct, open, pow, round, super, dict, list, object, range, str, max, min # noqa: F401
import os
import sys
import time
import signal
from random import randrange
@ -85,7 +86,7 @@ def run_cli_native(verb, *args, **kwargs):
bytes.
"""
nodeargs = kwargs.pop("nodeargs", [])
encoding = kwargs.pop("encoding", None) or "utf-8"
encoding = kwargs.pop("encoding", None) or getattr(sys.stdout, "encoding") or "utf-8"
return_bytes = kwargs.pop("return_bytes", False)
verb = maybe_unicode_to_argv(verb)
args = [maybe_unicode_to_argv(a) for a in args]

View File

@ -256,7 +256,11 @@ def quote_output_u(*args, **kwargs):
result = quote_output(*args, **kwargs)
if isinstance(result, unicode):
return result
return result.decode(kwargs.get("encoding", None) or io_encoding)
# Since we're quoting, the assumption is this will be read by a human, and
# therefore printed, so stdout's encoding is the plausible one. io_encoding
# is now always utf-8.
return result.decode(kwargs.get("encoding", None) or
getattr(sys.stdout, "encoding") or io_encoding)
def quote_output(s, quotemarks=True, quote_newlines=None, encoding=None):
@ -287,7 +291,7 @@ def quote_output(s, quotemarks=True, quote_newlines=None, encoding=None):
def _encode(s):
if isinstance(s, bytes):
try:
s = s.decode('utf-8')
s = s.decode("utf-8")
except UnicodeDecodeError:
return b'b"%s"' % (ESCAPABLE_8BIT.sub(lambda m: _bytes_escape(m, quote_newlines), s),)