summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Lib/logging/__init__.py12
-rw-r--r--Lib/test/test_logging.py27
-rw-r--r--Misc/NEWS9
3 files changed, 38 insertions, 10 deletions
diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py
index b265c21..8773225 100644
--- a/Lib/logging/__init__.py
+++ b/Lib/logging/__init__.py
@@ -758,17 +758,19 @@ class StreamHandler(Handler):
"""
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) or
+ getattr(stream, 'encoding', None) is None):
+ stream.write(fs % msg)
else:
- self.stream.write(fs % msg)
+ stream.write(fs % msg.encode(stream.encoding))
except UnicodeError:
- self.stream.write(fs % msg.encode("UTF-8"))
+ stream.write(fs % msg.encode("UTF-8"))
self.flush()
except (KeyboardInterrupt, SystemExit):
raise
diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py
index 09fdf4b..9c7bf5b 100644
--- a/Lib/test/test_logging.py
+++ b/Lib/test/test_logging.py
@@ -1,6 +1,6 @@
#!/usr/bin/env python
#
-# Copyright 2001-2004 by Vinay Sajip. All Rights Reserved.
+# Copyright 2001-2009 by Vinay Sajip. All Rights Reserved.
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose and without fee is hereby granted,
@@ -18,13 +18,14 @@
"""Test harness for the logging module. Run all tests.
-Copyright (C) 2001-2008 Vinay Sajip. All Rights Reserved.
+Copyright (C) 2001-2009 Vinay Sajip. All Rights Reserved.
"""
import logging
import logging.handlers
import logging.config
+import codecs
import copy
import cPickle
import cStringIO
@@ -860,6 +861,7 @@ class MemoryTest(BaseTest):
('foo', 'DEBUG', '3'),
])
+
class EncodingTest(BaseTest):
def test_encoding_plain_file(self):
# In Python 2.x, a plain file object is treated as having no encoding.
@@ -886,6 +888,27 @@ class EncodingTest(BaseTest):
if os.path.isfile(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')
+
+
class WarningsTest(BaseTest):
def test_warnings(self):
logging.captureWarnings(True)
diff --git a/Misc/NEWS b/Misc/NEWS
index 412f067..c926347 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -149,6 +149,9 @@ Core and Builtins
Library
-------
+- Issue #5170: Fixed Unicode output bug in logging and added test case.
+ This is a regression which did not occur in 2.5.
+
- Issue #4512 (part 2): Promote ``ZipImporter._get_filename()`` to be a
public documented method ``ZipImporter.get_filename()``.
@@ -162,7 +165,7 @@ Library
Tkapp_Call when calling from a thread different than the one that created
the Tcl interpreter. Patch by Robert Hancock.
-- Issue #1520877: Now distutils.sysconfig reads $AR from the
+- Issue #1520877: Now distutils.sysconfig reads $AR from the
environment/Makefile. Patch by Douglas Greiman.
- Issue #4285: Change sys.version_info to be a named tuple. Patch by
@@ -171,7 +174,7 @@ Library
- Issue #1276768: The verbose option was not used in the code of
distutils.file_util and distutils.dir_util.
-- Issue #5132: Fixed trouble building extensions under Solaris with
+- Issue #5132: Fixed trouble building extensions under Solaris with
--enabled-shared activated. Initial patch by Dave Peterson.
- Issue #1581476: Always use the Tcl global namespace when calling into Tcl.
@@ -212,7 +215,7 @@ Library
- Issue #4710: Extract directories properly in the zipfile module;
allow adding directories to a zipfile.
-- Issue #3807: _multiprocessing build fails when configure is passed
+- Issue #3807: _multiprocessing build fails when configure is passed
--without-threads argument. When this occurs, _multiprocessing will
be disabled, and not compiled.