[Python-checkins] r85288 - in python/branches/py3k/Lib: concurrent/futures/_base.py test/test_concurrent_futures.py

brian.quinlan python-checkins at python.org
Wed Oct 6 15:05:45 CEST 2010


Author: brian.quinlan
Date: Wed Oct  6 15:05:45 2010
New Revision: 85288

Log:
Fixes 9903: test_concurrent_futures writes on stderr

Modified:
   python/branches/py3k/Lib/concurrent/futures/_base.py
   python/branches/py3k/Lib/test/test_concurrent_futures.py

Modified: python/branches/py3k/Lib/concurrent/futures/_base.py
==============================================================================
--- python/branches/py3k/Lib/concurrent/futures/_base.py	(original)
+++ python/branches/py3k/Lib/concurrent/futures/_base.py	Wed Oct  6 15:05:45 2010
@@ -40,9 +40,8 @@
 
 # Logger for internal use by the futures package.
 LOGGER = logging.getLogger("concurrent.futures")
-_handler = logging.StreamHandler()
-LOGGER.addHandler(_handler)
-del _handler
+STDERR_HANDLER = logging.StreamHandler()
+LOGGER.addHandler(STDERR_HANDLER)
 
 class Error(Exception):
     """Base class for all future-related exceptions."""

Modified: python/branches/py3k/Lib/test/test_concurrent_futures.py
==============================================================================
--- python/branches/py3k/Lib/test/test_concurrent_futures.py	(original)
+++ python/branches/py3k/Lib/test/test_concurrent_futures.py	Wed Oct  6 15:05:45 2010
@@ -9,6 +9,8 @@
 # without thread support.
 test.support.import_module('threading')
 
+import io
+import logging
 import multiprocessing
 import sys
 import threading
@@ -21,7 +23,8 @@
 
 from concurrent import futures
 from concurrent.futures._base import (
-    PENDING, RUNNING, CANCELLED, CANCELLED_AND_NOTIFIED, FINISHED, Future, wait)
+    PENDING, RUNNING, CANCELLED, CANCELLED_AND_NOTIFIED, FINISHED, Future,
+    LOGGER, STDERR_HANDLER, wait)
 import concurrent.futures.process
 
 def create_future(state=PENDING, exception=None, result=None):
@@ -617,24 +620,33 @@
         self.assertTrue(was_cancelled)
 
     def test_done_callback_raises(self):
-        raising_was_called = False
-        fn_was_called = False
-
-        def raising_fn(callback_future):
-            nonlocal raising_was_called
-            raising_was_called = True
-            raise Exception('doh!')
-
-        def fn(callback_future):
-            nonlocal fn_was_called
-            fn_was_called = True
-
-        f = Future()
-        f.add_done_callback(raising_fn)
-        f.add_done_callback(fn)
-        f.set_result(5)
-        self.assertTrue(raising_was_called)
-        self.assertTrue(fn_was_called)
+        LOGGER.removeHandler(STDERR_HANDLER)
+        logging_stream = io.StringIO()
+        handler = logging.StreamHandler(logging_stream)
+        LOGGER.addHandler(handler)
+        try:
+            raising_was_called = False
+            fn_was_called = False
+
+            def raising_fn(callback_future):
+                nonlocal raising_was_called
+                raising_was_called = True
+                raise Exception('doh!')
+
+            def fn(callback_future):
+                nonlocal fn_was_called
+                fn_was_called = True
+
+            f = Future()
+            f.add_done_callback(raising_fn)
+            f.add_done_callback(fn)
+            f.set_result(5)
+            self.assertTrue(raising_was_called)
+            self.assertTrue(fn_was_called)
+            self.assertIn('Exception: doh!', logging_stream.getvalue())
+        finally:
+            LOGGER.removeHandler(handler)
+            LOGGER.addHandler(STDERR_HANDLER)
 
     def test_done_callback_already_successful(self):
         callback_result = None


More information about the Python-checkins mailing list