]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #3772: Fixed regression problem in StreamHandler.emit().
authorVinay Sajip <vinay_sajip@yahoo.co.uk>
Thu, 4 Sep 2008 07:31:21 +0000 (07:31 +0000)
committerVinay Sajip <vinay_sajip@yahoo.co.uk>
Thu, 4 Sep 2008 07:31:21 +0000 (07:31 +0000)
Lib/logging/__init__.py
Lib/test/test_logging.py
Misc/NEWS

index 9026ef383b1f54852733e71585637c7a971c140a..7b790d2de12dd6f2d1548a824c945084cc1140f4 100644 (file)
@@ -757,7 +757,7 @@ class StreamHandler(Handler):
                 self.stream.write(fs % msg)
             else:
                 try:
-                    if hasattr(self.stream, 'encoding'):
+                    if getattr(self.stream, 'encoding', None) is not None:
                         self.stream.write(fs % msg.encode(self.stream.encoding))
                     else:
                         self.stream.write(fs % msg)
index 6ed8ca2a43d834358bdfa5505ab5389680f14eb4..5d2b5fd3fd99bb28456ce1e0b3b3136af667fc92 100644 (file)
@@ -859,6 +859,31 @@ 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.
+        log = logging.getLogger("test")
+        fn = tempfile.mktemp(".log")
+        # the non-ascii data we write to the log.
+        data = "foo\x80"
+        try:
+            handler = logging.FileHandler(fn)
+            log.addHandler(handler)
+            try:
+                # write non-ascii data to the log.
+                log.warning(data)
+            finally:
+                log.removeHandler(handler)
+                handler.close()
+            # check we wrote exactly those bytes, ignoring trailing \n etc
+            f = open(fn)
+            try:
+                self.failUnlessEqual(f.read().rstrip(), data)
+            finally:
+                f.close()
+        finally:
+            if os.path.isfile(fn):
+                os.remove(fn)
 
 # 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
@@ -867,7 +892,8 @@ class MemoryTest(BaseTest):
 def test_main():
     run_unittest(BuiltinLevelsTest, BasicFilterTest,
                     CustomLevelsAndFiltersTest, MemoryHandlerTest,
-                    ConfigFileTest, SocketHandlerTest, MemoryTest)
+                    ConfigFileTest, SocketHandlerTest, MemoryTest,
+                    EncodingTest)
 
 if __name__ == "__main__":
     test_main()
index 33866dfa7d16469face17607529aa5b541f9163f..dbb811011779ceceb342a06bc58ebfc5aa72969c 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -56,6 +56,8 @@ C-API
 Library
 -------
 
+- Issue #3772: Fixed regression problem in StreamHandler.emit().
+
 - Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
   to the urlparse one.  Added a PendingDeprecationWarning in the old
   module, it will be deprecated in the future.
@@ -87,7 +89,7 @@ Library
 - Issue #3708: os.urandom no longer goes into an infinite loop when passed a
   non-integer floating point number.
 
-- Issue #3110: multiprocessing fails to compiel on solaris 10 due to missing 
+- Issue #3110: multiprocessing fails to compiel on solaris 10 due to missing
   SEM_VALUE_MAX.
 
 Extension Modules