04b4d4c
diff -ru Python-2.6-orig/Lib/logging/__init__.py Python-2.6/Lib/logging/__init__.py
04b4d4c
--- Python-2.6-orig/Lib/logging/__init__.py	2008-09-04 03:31:21.000000000 -0400
04b4d4c
+++ Python-2.6/Lib/logging/__init__.py	2009-06-08 15:57:30.000000000 -0400
04b4d4c
@@ -752,17 +752,29 @@
04b4d4c
         """
04b4d4c
         try:
04b4d4c
             msg = self.format(record)
04b4d4c
+            stream = self.stream
04b4d4c
             fs = "%s\n"
04b4d4c
             if not hasattr(types, "UnicodeType"): #if no unicode support...
04b4d4c
-                self.stream.write(fs % msg)
04b4d4c
+                stream.write(fs % msg)
04b4d4c
             else:
04b4d4c
                 try:
04b4d4c
-                    if getattr(self.stream, 'encoding', None) is not None:
04b4d4c
-                        self.stream.write(fs % msg.encode(self.stream.encoding))
04b4d4c
+                    if (isinstance(msg, unicode) and
04b4d4c
+                        getattr(stream, 'encoding', None)):
04b4d4c
+                        fs = fs.decode(stream.encoding)
04b4d4c
+                        try:
04b4d4c
+                            stream.write(fs % msg)
04b4d4c
+                        except UnicodeEncodeError:
04b4d4c
+                            #Printing to terminals sometimes fails. For example,
04b4d4c
+                            #with an encoding of 'cp1251', the above write will
04b4d4c
+                            #work if written to a stream opened or wrapped by
04b4d4c
+                            #the codecs module, but fail when writing to a
04b4d4c
+                            #terminal even when the codepage is set to cp1251.
04b4d4c
+                            #An extra encoding step seems to be needed.
04b4d4c
+                            stream.write((fs % msg).encode(stream.encoding))
04b4d4c
                     else:
04b4d4c
-                        self.stream.write(fs % msg)
04b4d4c
+                        stream.write(fs % msg)
04b4d4c
                 except UnicodeError:
04b4d4c
-                    self.stream.write(fs % msg.encode("UTF-8"))
04b4d4c
+                    stream.write(fs % msg.encode("UTF-8"))
04b4d4c
             self.flush()
04b4d4c
         except (KeyboardInterrupt, SystemExit):
04b4d4c
             raise
04b4d4c
Only in Python-2.6/Lib/logging: __init__.py~