]> granicus.if.org Git - python/commitdiff
Issue #28634: Fix asyncio.isfuture() to support mocks
authorYury Selivanov <yury@magic.io>
Mon, 7 Nov 2016 21:00:50 +0000 (16:00 -0500)
committerYury Selivanov <yury@magic.io>
Mon, 7 Nov 2016 21:00:50 +0000 (16:00 -0500)
Lib/asyncio/futures.py
Lib/test/test_asyncio/test_futures.py

index bcd4d16b9d158f579decbe700c1467818a728ca8..9ca8d8458bcf6248a54b286d5fd9b7fb719cb7d9 100644 (file)
@@ -2,7 +2,7 @@
 
 __all__ = ['CancelledError', 'TimeoutError',
            'InvalidStateError',
-           'Future', 'wrap_future',
+           'Future', 'wrap_future', 'isfuture',
            ]
 
 import concurrent.futures._base
@@ -117,7 +117,8 @@ def isfuture(obj):
     itself as duck-type compatible by setting _asyncio_future_blocking.
     See comment in Future for more details.
     """
-    return getattr(obj, '_asyncio_future_blocking', None) is not None
+    return (hasattr(obj.__class__, '_asyncio_future_blocking') and
+            obj._asyncio_future_blocking is not None)
 
 
 class Future:
index 153b8ed707f1b3b6c77579335c643195290d3ff4..c306b77e653264ccd4253d7bf8c78cb86adf6ef6 100644 (file)
@@ -101,6 +101,29 @@ class FutureTests(test_utils.TestCase):
         self.loop = self.new_test_loop()
         self.addCleanup(self.loop.close)
 
+    def test_isfuture(self):
+        class MyFuture:
+            _asyncio_future_blocking = None
+
+            def __init__(self):
+                self._asyncio_future_blocking = False
+
+        self.assertFalse(asyncio.isfuture(MyFuture))
+        self.assertTrue(asyncio.isfuture(MyFuture()))
+
+        self.assertFalse(asyncio.isfuture(1))
+        self.assertFalse(asyncio.isfuture(asyncio.Future))
+
+        # As `isinstance(Mock(), Future)` returns `False`
+        self.assertFalse(asyncio.isfuture(mock.Mock()))
+
+        # As `isinstance(Mock(Future), Future)` returns `True`
+        self.assertTrue(asyncio.isfuture(mock.Mock(asyncio.Future)))
+
+        f = asyncio.Future(loop=self.loop)
+        self.assertTrue(asyncio.isfuture(f))
+        f.cancel()
+
     def test_initial_state(self):
         f = asyncio.Future(loop=self.loop)
         self.assertFalse(f.cancelled())