From: Vinay Sajip Date: Wed, 22 Apr 2009 12:12:43 +0000 (+0000) Subject: Issue #5170: Fixed regression caused when fixing #5768. X-Git-Tag: v2.6.3rc1~397 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ae7c4a0a42e5b41c7f69baf1d2faaa4af933f936;p=python Issue #5170: Fixed regression caused when fixing #5768. --- diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py index 6ce8364c21..f0e5b4a549 100644 --- a/Lib/logging/__init__.py +++ b/Lib/logging/__init__.py @@ -766,7 +766,17 @@ class StreamHandler(Handler): 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: