Blob Blame History Raw
diff -ru Python-2.6-orig/Lib/logging/__init__.py Python-2.6/Lib/logging/__init__.py
--- Python-2.6-orig/Lib/logging/__init__.py	2008-09-04 03:31:21.000000000 -0400
+++ Python-2.6/Lib/logging/__init__.py	2009-06-08 15:57:30.000000000 -0400
@@ -752,17 +752,29 @@
         """
         try:
             msg = self.format(record)
+            stream = self.stream
             fs = "%s\n"
             if not hasattr(types, "UnicodeType"): #if no unicode support...
-                self.stream.write(fs % msg)
+                stream.write(fs % msg)
             else:
                 try:
-                    if getattr(self.stream, 'encoding', None) is not None:
-                        self.stream.write(fs % msg.encode(self.stream.encoding))
+                    if (isinstance(msg, unicode) and
+                        getattr(stream, 'encoding', None)):
+                        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:
-                        self.stream.write(fs % msg)
+                        stream.write(fs % msg)
                 except UnicodeError:
-                    self.stream.write(fs % msg.encode("UTF-8"))
+                    stream.write(fs % msg.encode("UTF-8"))
             self.flush()
         except (KeyboardInterrupt, SystemExit):
             raise
Only in Python-2.6/Lib/logging: __init__.py~