From a5cd255a7cb15c7920171f0796cf163a00a5ecaf Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 15 Oct 2013 23:36:56 +0200 Subject: [PATCH] Close #19267: Fix support of multibyte encoding (ex: UTF-16) in the logging module. --- Lib/logging/__init__.py | 2 +- Lib/test/test_logging.py | 18 ++++++++++++++++++ Misc/NEWS | 3 +++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py index 0e8e3ddde0..cda781a489 100644 --- a/Lib/logging/__init__.py +++ b/Lib/logging/__init__.py @@ -857,7 +857,7 @@ class StreamHandler(Handler): try: if (isinstance(msg, unicode) and getattr(stream, 'encoding', None)): - ufs = fs.decode(stream.encoding) + ufs = u'%s\n' try: stream.write(ufs % msg) except UnicodeEncodeError: diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py index 31bc48e353..0c105800ac 100644 --- a/Lib/test/test_logging.py +++ b/Lib/test/test_logging.py @@ -1060,6 +1060,24 @@ class EncodingTest(BaseTest): #Compare against what the data should be when encoded in CP-1251 self.assertEqual(s, '\xe4\xee \xf1\xe2\xe8\xe4\xe0\xed\xe8\xff\n') + def test_encoding_utf16_unicode(self): + # Issue #19267 + log = logging.getLogger("test") + message = u'b\u0142\u0105d' + writer_class = codecs.getwriter('utf-16-le') + writer_class.encoding = 'utf-16-le' + stream = cStringIO.StringIO() + writer = writer_class(stream, 'strict') + handler = logging.StreamHandler(writer) + log.addHandler(handler) + try: + log.warning(message) + finally: + log.removeHandler(handler) + handler.close() + s = stream.getvalue() + self.assertEqual(s, 'b\x00B\x01\x05\x01d\x00\n\x00') + class WarningsTest(BaseTest): diff --git a/Misc/NEWS b/Misc/NEWS index 816bd4d18e..6e4b7fb856 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -370,6 +370,9 @@ Library - Issue #17926: Fix dbm.__contains__ on 64-bit big-endian machines. +- Issue #19267: Fix support of multibyte encoding (ex: UTF-16) in the logging + module. + - Issue #17918: When using SSLSocket.accept(), if the SSL handshake failed on the new socket, the socket would linger indefinitely. Thanks to Peter Saveliev for reporting. -- 2.50.1