Skip to content

Commit 01438f7

Browse files
committed
Address review: use fut.exception() result, drop the redundant handling, test the timeout case, remove NEWS
1 parent 854047a commit 01438f7

3 files changed

Lines changed: 18 additions & 25 deletions

File tree

Lib/concurrent/futures/_base.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -309,21 +309,14 @@ def wait(fs, timeout=None, return_when=ALL_COMPLETED):
309309
def _result_or_cancel(fut, timeout=None):
310310
try:
311311
try:
312-
if timeout is not None:
313-
# Wait out the timeout separately from retrieving the result, so
314-
# that a TimeoutError raised by the call is not mistaken for the
315-
# map() timeout. Future.exception() raises TimeoutError only
316-
# when the wait itself times out, never for the call's own.
317-
try:
318-
fut.exception(timeout)
319-
except TimeoutError:
320-
raise
321-
except CancelledError:
322-
pass
323-
try:
324-
return (fut.result(), None)
325-
except BaseException as exc:
312+
# fut.exception() returns the call's own error but raises
313+
# TimeoutError only for a map() timeout.
314+
exc = fut.exception(timeout)
315+
if exc is not None:
326316
return (None, exc)
317+
return (fut.result(), None)
318+
except CancelledError as exc:
319+
return (None, exc)
327320
finally:
328321
fut.cancel()
329322
finally:

Lib/test/test_concurrent_futures/executor.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ def raiser(exception, msg='std'):
2929
raise exception(msg)
3030

3131

32-
# Used in test_map_timeout_from_callable
3332
def timeout_on_one(x):
3433
if x == 1:
3534
raise TimeoutError
@@ -96,13 +95,17 @@ def test_map_exception(self):
9695

9796
@warnings_helper.ignore_fork_in_thread_deprecation_warnings()
9897
def test_map_timeout_from_callable(self):
99-
# A TimeoutError from the callable is not the map() timeout.
100-
i = self.executor.map(timeout_on_one, [0, 1, 2, 3])
101-
self.assertEqual(next(i), 0)
102-
self.assertRaises(TimeoutError, next, i)
103-
self.assertEqual(next(i), 2)
104-
self.assertEqual(next(i), 3)
105-
self.assertRaises(StopIteration, next, i)
98+
# A TimeoutError from the callable is not the map() timeout, whether
99+
# or not a map() timeout is set.
100+
for timeout in (None, support.SHORT_TIMEOUT):
101+
with self.subTest(timeout=timeout):
102+
i = self.executor.map(timeout_on_one, [0, 1, 2, 3],
103+
timeout=timeout)
104+
self.assertEqual(next(i), 0)
105+
self.assertRaises(TimeoutError, next, i)
106+
self.assertEqual(next(i), 2)
107+
self.assertEqual(next(i), 3)
108+
self.assertRaises(StopIteration, next, i)
106109

107110
@warnings_helper.ignore_fork_in_thread_deprecation_warnings()
108111
@support.requires_resource('walltime')

Misc/NEWS.d/next/Library/2026-08-15-16-00-00.gh-issue-155852.MapTmo.rst

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)