Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1e2ae4f

Browse files
committed
Fixes 9903: test_concurrent_futures writes on stderr
1 parent 5ad8ed5 commit 1e2ae4f

2 files changed

Lines changed: 33 additions & 22 deletions

File tree

Lib/concurrent/futures/_base.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,8 @@
4040

4141
# Logger for internal use by the futures package.
4242
LOGGER = logging.getLogger("concurrent.futures")
43-
_handler = logging.StreamHandler()
44-
LOGGER.addHandler(_handler)
45-
del _handler
43+
STDERR_HANDLER = logging.StreamHandler()
44+
LOGGER.addHandler(STDERR_HANDLER)
4645

4746
class Error(Exception):
4847
"""Base class for all future-related exceptions."""

Lib/test/test_concurrent_futures.py

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
# without thread support.
1010
test.support.import_module('threading')
1111

12+
import io
13+
import logging
1214
import multiprocessing
1315
import sys
1416
import threading
@@ -21,7 +23,8 @@
2123

2224
from concurrent import futures
2325
from concurrent.futures._base import (
24-
PENDING, RUNNING, CANCELLED, CANCELLED_AND_NOTIFIED, FINISHED, Future, wait)
26+
PENDING, RUNNING, CANCELLED, CANCELLED_AND_NOTIFIED, FINISHED, Future,
27+
LOGGER, STDERR_HANDLER, wait)
2528
import concurrent.futures.process
2629

2730
def create_future(state=PENDING, exception=None, result=None):
@@ -617,24 +620,33 @@ def fn(callback_future):
617620
self.assertTrue(was_cancelled)
618621

619622
def test_done_callback_raises(self):
620-
raising_was_called = False
621-
fn_was_called = False
622-
623-
def raising_fn(callback_future):
624-
nonlocal raising_was_called
625-
raising_was_called = True
626-
raise Exception('doh!')
627-
628-
def fn(callback_future):
629-
nonlocal fn_was_called
630-
fn_was_called = True
631-
632-
f = Future()
633-
f.add_done_callback(raising_fn)
634-
f.add_done_callback(fn)
635-
f.set_result(5)
636-
self.assertTrue(raising_was_called)
637-
self.assertTrue(fn_was_called)
623+
LOGGER.removeHandler(STDERR_HANDLER)
624+
logging_stream = io.StringIO()
625+
handler = logging.StreamHandler(logging_stream)
626+
LOGGER.addHandler(handler)
627+
try:
628+
raising_was_called = False
629+
fn_was_called = False
630+
631+
def raising_fn(callback_future):
632+
nonlocal raising_was_called
633+
raising_was_called = True
634+
raise Exception('doh!')
635+
636+
def fn(callback_future):
637+
nonlocal fn_was_called
638+
fn_was_called = True
639+
640+
f = Future()
641+
f.add_done_callback(raising_fn)
642+
f.add_done_callback(fn)
643+
f.set_result(5)
644+
self.assertTrue(raising_was_called)
645+
self.assertTrue(fn_was_called)
646+
self.assertIn('Exception: doh!', logging_stream.getvalue())
647+
finally:
648+
LOGGER.removeHandler(handler)
649+
LOGGER.addHandler(STDERR_HANDLER)
638650

639651
def test_done_callback_already_successful(self):
640652
callback_result = None

0 commit comments

Comments
 (0)