(self)
| 840 | support.gc_collect() # Kill CoroLike to clean-up ABCMeta cache |
| 841 | |
| 842 | def test_Coroutine(self): |
| 843 | def gen(): |
| 844 | yield |
| 845 | |
| 846 | @types.coroutine |
| 847 | def coro(): |
| 848 | yield |
| 849 | |
| 850 | async def new_coro(): |
| 851 | pass |
| 852 | |
| 853 | class Bar: |
| 854 | def __await__(self): |
| 855 | yield |
| 856 | |
| 857 | class MinimalCoro(Coroutine): |
| 858 | def send(self, value): |
| 859 | return value |
| 860 | def throw(self, typ, val=None, tb=None): |
| 861 | super().throw(typ, val, tb) |
| 862 | def __await__(self): |
| 863 | yield |
| 864 | |
| 865 | self.validate_abstract_methods(Coroutine, '__await__', 'send', 'throw') |
| 866 | |
| 867 | non_samples = [None, int(), gen(), object(), Bar()] |
| 868 | for x in non_samples: |
| 869 | self.assertNotIsInstance(x, Coroutine) |
| 870 | self.assertNotIsSubclass(type(x), Coroutine) |
| 871 | |
| 872 | samples = [MinimalCoro()] |
| 873 | for x in samples: |
| 874 | self.assertIsInstance(x, Awaitable) |
| 875 | self.assertIsSubclass(type(x), Awaitable) |
| 876 | |
| 877 | c = coro() |
| 878 | # Iterable coroutines (generators with CO_ITERABLE_COROUTINE |
| 879 | # flag don't have '__await__' method, hence can't be instances |
| 880 | # of Coroutine. Use inspect.isawaitable to detect them. |
| 881 | self.assertNotIsInstance(c, Coroutine) |
| 882 | |
| 883 | c = new_coro() |
| 884 | self.assertIsInstance(c, Coroutine) |
| 885 | c.close() # avoid RuntimeWarning that coro() was not awaited |
| 886 | |
| 887 | class CoroLike: |
| 888 | def send(self, value): |
| 889 | pass |
| 890 | def throw(self, typ, val=None, tb=None): |
| 891 | pass |
| 892 | def close(self): |
| 893 | pass |
| 894 | def __await__(self): |
| 895 | pass |
| 896 | self.assertIsInstance(CoroLike(), Coroutine) |
| 897 | self.assertIsSubclass(CoroLike, Coroutine) |
| 898 | |
| 899 | class CoroLike: |
nothing calls this directly
no test coverage detected