[Python-checkins] r71794 - python/trunk/Lib/logging/__init__.py

vinay.sajip python-checkins at python.org
Wed Apr 22 14:10:48 CEST 2009


Author: vinay.sajip
Date: Wed Apr 22 14:10:47 2009
New Revision: 71794

Log:
Issue #5170: Fixed regression caused when fixing #5768.


Modified:
   python/trunk/Lib/logging/__init__.py

Modified: python/trunk/Lib/logging/__init__.py
==============================================================================
--- python/trunk/Lib/logging/__init__.py	(original)
+++ python/trunk/Lib/logging/__init__.py	Wed Apr 22 14:10:47 2009
@@ -766,7 +766,17 @@
                 try:
                     if (isinstance(msg, unicode) and
                         getattr(stream, 'encoding', None)):
-                        stream.write(fs.decode(stream.encoding) % msg)
+                        fs = fs.decode(stream.encoding)
+                        try:
+                            stream.write(fs % msg)
+                        except UnicodeEncodeError:
+                            #Printing to terminals sometimes fails. For example,
+                            #with an encoding of 'cp1251', the above write will
+                            #work if written to a stream opened or wrapped by
+                            #the codecs module, but fail when writing to a
+                            #terminal even when the codepage is set to cp1251.
+                            #An extra encoding step seems to be needed.
+                            stream.write((fs % msg).encode(stream.encoding))
                     else:
                         stream.write(fs % msg)
                 except UnicodeError:


More information about the Python-checkins mailing list