Skip to content
This repository was archived by the owner on Nov 23, 2017. It is now read-only.

Commit 32edb29

Browse files
committed
Replace all checks for isinstance(x, Future) with isfuture(x).
1 parent 09b0e2e commit 32edb29

File tree

4 files changed

+25
-13
lines changed

4 files changed

+25
-13
lines changed

asyncio/base_events.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ def run_until_complete(self, future):
363363
"""
364364
self._check_closed()
365365

366-
new_task = not isinstance(future, futures.Future)
366+
new_task = not futures.isfuture(future)
367367
future = tasks.ensure_future(future, loop=self)
368368
if new_task:
369369
# An exception is raised if the future didn't complete, so there

asyncio/coroutines.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,8 @@ def coroutine(func):
204204
@functools.wraps(func)
205205
def coro(*args, **kw):
206206
res = func(*args, **kw)
207-
if isinstance(res, futures.Future) or inspect.isgenerator(res) or \
208-
isinstance(res, CoroWrapper):
207+
if (futures.isfuture(res) or inspect.isgenerator(res) or
208+
isinstance(res, CoroWrapper)):
209209
res = yield from res
210210
elif _AwaitableABC is not None:
211211
# If 'func' returns an Awaitable (new in 3.5) we

asyncio/futures.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,16 @@ def __del__(self):
110110
self.loop.call_exception_handler({'message': msg})
111111

112112

113+
def isfuture(obj):
114+
"""Check for a Future.
115+
116+
This returns True when obj is a Future instance or is advertising
117+
itself as duck-type compatible by setting _asyncio_future_blocking.
118+
See comment in Future for more details.
119+
"""
120+
return getattr(obj, '_asyncio_future_blocking', None) is not None
121+
122+
113123
class Future:
114124
"""This class is *almost* compatible with concurrent.futures.Future.
115125
@@ -423,15 +433,17 @@ def _chain_future(source, destination):
423433
If destination is cancelled, source gets cancelled too.
424434
Compatible with both asyncio.Future and concurrent.futures.Future.
425435
"""
426-
if not isinstance(source, (Future, concurrent.futures.Future)):
436+
if not isfuture(source) and not isinstance(source,
437+
concurrent.futures.Future):
427438
raise TypeError('A future is required for source argument')
428-
if not isinstance(destination, (Future, concurrent.futures.Future)):
439+
if not isfuture(destination) and not isinstance(destination,
440+
concurrent.futures.Future):
429441
raise TypeError('A future is required for destination argument')
430-
source_loop = source._loop if isinstance(source, Future) else None
431-
dest_loop = destination._loop if isinstance(destination, Future) else None
442+
source_loop = source._loop if isfuture(source) else None
443+
dest_loop = destination._loop if isfuture(destination) else None
432444

433445
def _set_state(future, other):
434-
if isinstance(future, Future):
446+
if isfuture(future):
435447
_copy_future_state(other, future)
436448
else:
437449
_set_concurrent_future_state(future, other)
@@ -455,7 +467,7 @@ def _call_set_state(source):
455467

456468
def wrap_future(future, *, loop=None):
457469
"""Wrap concurrent.futures.Future object."""
458-
if isinstance(future, Future):
470+
if isfuture(future):
459471
return future
460472
assert isinstance(future, concurrent.futures.Future), \
461473
'concurrent.futures.Future is expected, got {!r}'.format(future)

asyncio/tasks.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ def wait(fs, *, loop=None, timeout=None, return_when=ALL_COMPLETED):
333333
Note: This does not raise TimeoutError! Futures that aren't done
334334
when the timeout occurs are returned in the second set.
335335
"""
336-
if isinstance(fs, futures.Future) or coroutines.iscoroutine(fs):
336+
if futures.isfuture(fs) or coroutines.iscoroutine(fs):
337337
raise TypeError("expect a list of futures, not %s" % type(fs).__name__)
338338
if not fs:
339339
raise ValueError('Set of coroutines/Futures is empty.')
@@ -462,7 +462,7 @@ def as_completed(fs, *, loop=None, timeout=None):
462462
463463
Note: The futures 'f' are not necessarily members of fs.
464464
"""
465-
if isinstance(fs, futures.Future) or coroutines.iscoroutine(fs):
465+
if futures.isfuture(fs) or coroutines.iscoroutine(fs):
466466
raise TypeError("expect a list of futures, not %s" % type(fs).__name__)
467467
loop = loop if loop is not None else events.get_event_loop()
468468
todo = {ensure_future(f, loop=loop) for f in set(fs)}
@@ -538,7 +538,7 @@ def ensure_future(coro_or_future, *, loop=None):
538538
539539
If the argument is a Future, it is returned directly.
540540
"""
541-
if isinstance(coro_or_future, futures.Future):
541+
if futures.isfuture(coro_or_future):
542542
if loop is not None and loop is not coro_or_future._loop:
543543
raise ValueError('loop argument must agree with Future')
544544
return coro_or_future
@@ -614,7 +614,7 @@ def gather(*coros_or_futures, loop=None, return_exceptions=False):
614614

615615
arg_to_fut = {}
616616
for arg in set(coros_or_futures):
617-
if not isinstance(arg, futures.Future):
617+
if not futures.isfuture(arg):
618618
fut = ensure_future(arg, loop=loop)
619619
if loop is None:
620620
loop = fut._loop

0 commit comments

Comments
 (0)