[Python-checkins] cpython (2.7): Close #19267: Fix support of multibyte encoding (ex: UTF-16) in the logging

victor.stinner python-checkins at python.org
Tue Oct 15 23:48:19 CEST 2013


http://hg.python.org/cpython/rev/e94e29dab32c
changeset:   86384:e94e29dab32c
branch:      2.7
parent:      86377:d7ebe03fa752
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Tue Oct 15 23:36:56 2013 +0200
summary:
  Close #19267: Fix support of multibyte encoding (ex: UTF-16) in the logging
module.

files:
  Lib/logging/__init__.py  |   2 +-
  Lib/test/test_logging.py |  18 ++++++++++++++++++
  Misc/NEWS                |   3 +++
  3 files changed, 22 insertions(+), 1 deletions(-)


diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py
--- a/Lib/logging/__init__.py
+++ b/Lib/logging/__init__.py
@@ -857,7 +857,7 @@
                 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
--- a/Lib/test/test_logging.py
+++ b/Lib/test/test_logging.py
@@ -1060,6 +1060,24 @@
         #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
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -370,6 +370,9 @@
 
 - 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.

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list