bpo-34421: Improve distutils logging for non-ASCII strings. (GH-9126) (GH-9506)
Use "backslashreplace" instead of "unicode-escape". It is not
implementation depended and escapes only non-encodable characters.
Also simplify the code.
(cherry picked from commit 4b860fd)
diff --git a/Lib/distutils/log.py b/Lib/distutils/log.py
index 3a6602b..8ef6b28 100644
--- a/Lib/distutils/log.py
+++ b/Lib/distutils/log.py
@@ -27,14 +27,13 @@
stream = sys.stderr
else:
stream = sys.stdout
- if stream.errors == 'strict':
- # emulate backslashreplace error handler
- encoding = stream.encoding
- msg = msg.encode(encoding, "backslashreplace").decode(encoding)
try:
stream.write('%s\n' % msg)
except UnicodeEncodeError:
- stream.write('%s\n' % msg.encode('unicode-escape').decode('ascii'))
+ # emulate backslashreplace error handler
+ encoding = stream.encoding
+ msg = msg.encode(encoding, "backslashreplace").decode(encoding)
+ stream.write('%s\n' % msg)
stream.flush()
def log(self, level, msg, *args):