Issue #5170: Fixed Unicode output bug in logging and added test case. This is a regression which did not occur in 2.5.
This commit is contained in:
parent
dd0989e7df
commit
9c78658670
|
@ -748,17 +748,19 @@ class StreamHandler(Handler):
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
msg = self.format(record)
|
msg = self.format(record)
|
||||||
|
stream = self.stream
|
||||||
fs = "%s\n"
|
fs = "%s\n"
|
||||||
if not hasattr(types, "UnicodeType"): #if no unicode support...
|
if not hasattr(types, "UnicodeType"): #if no unicode support...
|
||||||
self.stream.write(fs % msg)
|
stream.write(fs % msg)
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
if getattr(self.stream, 'encoding', None) is not None:
|
if (isinstance(msg, unicode) or
|
||||||
self.stream.write(fs % msg.encode(self.stream.encoding))
|
getattr(stream, 'encoding', None) is None):
|
||||||
|
stream.write(fs % msg)
|
||||||
else:
|
else:
|
||||||
self.stream.write(fs % msg)
|
stream.write(fs % msg.encode(stream.encoding))
|
||||||
except UnicodeError:
|
except UnicodeError:
|
||||||
self.stream.write(fs % msg.encode("UTF-8"))
|
stream.write(fs % msg.encode("UTF-8"))
|
||||||
self.flush()
|
self.flush()
|
||||||
except (KeyboardInterrupt, SystemExit):
|
except (KeyboardInterrupt, SystemExit):
|
||||||
raise
|
raise
|
||||||
|
|
|
@ -859,6 +859,7 @@ class MemoryTest(BaseTest):
|
||||||
('foo', 'DEBUG', '3'),
|
('foo', 'DEBUG', '3'),
|
||||||
])
|
])
|
||||||
|
|
||||||
|
|
||||||
class EncodingTest(BaseTest):
|
class EncodingTest(BaseTest):
|
||||||
def test_encoding_plain_file(self):
|
def test_encoding_plain_file(self):
|
||||||
# In Python 2.x, a plain file object is treated as having no encoding.
|
# In Python 2.x, a plain file object is treated as having no encoding.
|
||||||
|
@ -885,6 +886,27 @@ class EncodingTest(BaseTest):
|
||||||
if os.path.isfile(fn):
|
if os.path.isfile(fn):
|
||||||
os.remove(fn)
|
os.remove(fn)
|
||||||
|
|
||||||
|
def test_encoding_cyrillic_unicode(self):
|
||||||
|
log = logging.getLogger("test")
|
||||||
|
#Get a message in Unicode: Do svidanya in Cyrillic (meaning goodbye)
|
||||||
|
message = u'\u0434\u043e \u0441\u0432\u0438\u0434\u0430\u043d\u0438\u044f'
|
||||||
|
#Ensure it's written in a Cyrillic encoding
|
||||||
|
writer_class = codecs.getwriter('cp1251')
|
||||||
|
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()
|
||||||
|
# check we wrote exactly those bytes, ignoring trailing \n etc
|
||||||
|
s = stream.getvalue()
|
||||||
|
#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')
|
||||||
|
|
||||||
|
|
||||||
# Set the locale to the platform-dependent default. I have no idea
|
# Set the locale to the platform-dependent default. I have no idea
|
||||||
# why the test does this, but in any case we save the current locale
|
# why the test does this, but in any case we save the current locale
|
||||||
# first and restore it at the end.
|
# first and restore it at the end.
|
||||||
|
|
|
@ -80,6 +80,9 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #5170: Fixed Unicode output bug in logging and added test case.
|
||||||
|
This is a regression which did not occur in 2.5.
|
||||||
|
|
||||||
- Partial fix to issue #1731706: memory leak in Tkapp_Call when calling
|
- Partial fix to issue #1731706: memory leak in Tkapp_Call when calling
|
||||||
from a thread different than the one that created the Tcl interpreter.
|
from a thread different than the one that created the Tcl interpreter.
|
||||||
Patch by Robert Hancock.
|
Patch by Robert Hancock.
|
||||||
|
|
Loading…
Reference in New Issue