]> granicus.if.org Git - python/commitdiff
Close #19267: Fix support of multibyte encoding (ex: UTF-16) in the logging
authorVictor Stinner <victor.stinner@gmail.com>
Tue, 15 Oct 2013 21:36:56 +0000 (23:36 +0200)
committerVictor Stinner <victor.stinner@gmail.com>
Tue, 15 Oct 2013 21:36:56 +0000 (23:36 +0200)
module.

Lib/logging/__init__.py
Lib/test/test_logging.py
Misc/NEWS

index 0e8e3ddde0daf0da7f924d782df873e1c42ae42d..cda781a4896feab5cd169e72db08536219d2965f 100644 (file)
@@ -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:
index 31bc48e353ad926642770e74a4921f39f7035b15..0c105800ac90403d8f750409066bcf2dfd6975cc 100644 (file)
@@ -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):
 
index 816bd4d18e0f58f95046e465a222d5bcfe6b6d7b..6e4b7fb8565b4358f0f56cabf35e9d4b0ac89759 100644 (file)
--- 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.