| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent dbacfc2 commit 68b34a7
12 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -916,12 +916,13 @@ enforced. | |||
| 916 | 916 | async def main(): | |
| 917 | 917 | await old_style_coroutine() | |
| 918 | 918 | ||
| 919 | - This decorator is **deprecated** and is scheduled for removal in | ||
| 920 | - Python 3.10. | ||
| 921 | - | ||
| 922 | 919 | This decorator should not be used for :keyword:`async def` | |
| 923 | 920 | coroutines. | |
| 924 | 921 | ||
| 922 | + .. deprecated-removed:: 3.8 3.10 | ||
| 923 | + | ||
| 924 | + Use :keyword:`async def` instead. | ||
| 925 | + | ||
| 925 | 926 | .. function:: iscoroutine(obj) | |
| 926 | 927 | ||
| 927 | 928 | Return ``True`` if *obj* is a :ref:`coroutine object <coroutine>`. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,6 +7,7 @@ | |||
| 7 | 7 | import sys | |
| 8 | 8 | import traceback | |
| 9 | 9 | import types | |
| 10 | + import warnings | ||
| 10 | 11 | ||
| 11 | 12 | from . import base_futures | |
| 12 | 13 | from . import constants | |
@@ -107,6 +108,9 @@ def coroutine(func): | |||
| 107 | 108 | If the coroutine is not yielded from before it is destroyed, | |
| 108 | 109 | an error message is logged. | |
| 109 | 110 | """ | |
| 111 | + warnings.warn('"@coroutine" decorator is deprecated since Python 3.8, use "async def" instead', | ||
| 112 | + DeprecationWarning, | ||
| 113 | + stacklevel=2) | ||
| 110 | 114 | if inspect.iscoroutinefunction(func): | |
| 111 | 115 | # In Python 3.5 that's all we need to do for coroutines | |
| 112 | 116 | # defined with "async def". | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,12 +3,13 @@ | |||
| 3 | 3 | __all__ = ('Lock', 'Event', 'Condition', 'Semaphore', 'BoundedSemaphore') | |
| 4 | 4 | ||
| 5 | 5 | import collections | |
| 6 | + import types | ||
| 6 | 7 | import warnings | |
| 7 | 8 | ||
| 8 | 9 | from . import events | |
| 9 | 10 | from . import futures | |
| 10 | 11 | from . import exceptions | |
| 11 | - from .coroutines import coroutine | ||
| 12 | + from .import coroutines | ||
| 12 | 13 | ||
| 13 | 14 | ||
| 14 | 15 | class _ContextManager: | |
@@ -55,7 +56,7 @@ def __exit__(self, *args): | |||
| 55 | 56 | # always raises; that's how the with-statement works. | |
| 56 | 57 | pass | |
| 57 | 58 | ||
| 58 | - @coroutine | ||
| 59 | + @types.coroutine | ||
| 59 | 60 | def __iter__(self): | |
| 60 | 61 | # This is not a coroutine. It is meant to enable the idiom: | |
| 61 | 62 | # | |
@@ -78,6 +79,9 @@ def __iter__(self): | |||
| 78 | 79 | yield from self.acquire() | |
| 79 | 80 | return _ContextManager(self) | |
| 80 | 81 | ||
| 82 | + # The flag is needed for legacy asyncio.iscoroutine() | ||
| 83 | + __iter__._is_coroutine = coroutines._is_coroutine | ||
| 84 | + | ||
| 81 | 85 | async def __acquire_ctx(self): | |
| 82 | 86 | await self.acquire() | |
| 83 | 87 | return _ContextManager(self) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -23,7 +23,7 @@ | |||
| 23 | 23 | from . import events | |
| 24 | 24 | from . import exceptions | |
| 25 | 25 | from . import futures | |
| 26 | - from .coroutines import coroutine | ||
| 26 | + from .coroutines import _is_coroutine | ||
| 27 | 27 | ||
| 28 | 28 | # Helper to generate new task names | |
| 29 | 29 | # This uses itertools.count() instead of a "+= 1" operation because the latter | |
@@ -638,7 +638,7 @@ def ensure_future(coro_or_future, *, loop=None): | |||
| 638 | 638 | 'required') | |
| 639 | 639 | ||
| 640 | 640 | ||
| 641 | - @coroutine | ||
| 641 | + @types.coroutine | ||
| 642 | 642 | def _wrap_awaitable(awaitable): | |
| 643 | 643 | """Helper for asyncio.ensure_future(). | |
| 644 | 644 | ||
@@ -647,6 +647,8 @@ def _wrap_awaitable(awaitable): | |||
| 647 | 647 | """ | |
| 648 | 648 | return (yield from awaitable.__await__()) | |
| 649 | 649 | ||
| 650 | + _wrap_awaitable._is_coroutine = _is_coroutine | ||
| 651 | + | ||
| 650 | 652 | ||
| 651 | 653 | class _GatheringFuture(futures.Future): | |
| 652 | 654 | """Helper for gather(). | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -575,9 +575,8 @@ def zero_error(fut): | |||
| 575 | 575 | def test_default_exc_handler_coro(self): | |
| 576 | 576 | self.loop._process_events = mock.Mock() | |
| 577 | 577 | ||
| 578 | - @asyncio.coroutine | ||
| 579 | - def zero_error_coro(): | ||
| 580 | - yield from asyncio.sleep(0.01) | ||
| 578 | + async def zero_error_coro(): | ||
| 579 | + await asyncio.sleep(0.01) | ||
| 581 | 580 | 1/0 | |
| 582 | 581 | ||
| 583 | 582 | # Test Future.__del__ | |
@@ -723,8 +722,7 @@ def test_set_task_factory(self): | |||
| 723 | 722 | class MyTask(asyncio.Task): | |
| 724 | 723 | pass | |
| 725 | 724 | ||
| 726 | - @asyncio.coroutine | ||
| 727 | - def coro(): | ||
| 725 | + async def coro(): | ||
| 728 | 726 | pass | |
| 729 | 727 | ||
| 730 | 728 | factory = lambda loop, coro: MyTask(coro, loop=loop) | |
@@ -779,8 +777,7 @@ def test_create_task(self): | |||
| 779 | 777 | class MyTask(asyncio.Task): | |
| 780 | 778 | pass | |
| 781 | 779 | ||
| 782 | - @asyncio.coroutine | ||
| 783 | - def test(): | ||
| 780 | + async def test(): | ||
| 784 | 781 | pass | |
| 785 | 782 | ||
| 786 | 783 | class EventLoop(base_events.BaseEventLoop): | |
@@ -830,8 +827,7 @@ def test_run_forever_keyboard_interrupt(self): | |||
| 830 | 827 | # Python issue #22601: ensure that the temporary task created by | |
| 831 | 828 | # run_forever() consumes the KeyboardInterrupt and so don't log | |
| 832 | 829 | # a warning | |
| 833 | - @asyncio.coroutine | ||
| 834 | - def raise_keyboard_interrupt(): | ||
| 830 | + async def raise_keyboard_interrupt(): | ||
| 835 | 831 | raise KeyboardInterrupt | |
| 836 | 832 | ||
| 837 | 833 | self.loop._process_events = mock.Mock() | |
@@ -849,8 +845,7 @@ def raise_keyboard_interrupt(): | |||
| 849 | 845 | def test_run_until_complete_baseexception(self): | |
| 850 | 846 | # Python issue #22429: run_until_complete() must not schedule a pending | |
| 851 | 847 | # call to stop() if the future raised a BaseException | |
| 852 | - @asyncio.coroutine | ||
| 853 | - def raise_keyboard_interrupt(): | ||
| 848 | + async def raise_keyboard_interrupt(): | ||
| 854 | 849 | raise KeyboardInterrupt | |
| 855 | 850 | ||
| 856 | 851 | self.loop._process_events = mock.Mock() | |
@@ -1070,9 +1065,7 @@ def test_create_connection_multiple_errors(self, m_socket): | |||
| 1070 | 1065 | class MyProto(asyncio.Protocol): | |
| 1071 | 1066 | pass | |
| 1072 | 1067 | ||
| 1073 | - @asyncio.coroutine | ||
| 1074 | - def getaddrinfo(*args, **kw): | ||
| 1075 | - yield from [] | ||
| 1068 | + async def getaddrinfo(*args, **kw): | ||
| 1076 | 1069 | return [(2, 1, 6, '', ('107.6.106.82', 80)), | |
| 1077 | 1070 | (2, 1, 6, '', ('107.6.106.82', 80))] | |
| 1078 | 1071 | ||
@@ -1191,9 +1184,8 @@ def test_create_connection_no_host_port_sock(self): | |||
| 1191 | 1184 | self.assertRaises(ValueError, self.loop.run_until_complete, coro) | |
| 1192 | 1185 | ||
| 1193 | 1186 | def test_create_connection_no_getaddrinfo(self): | |
| 1194 | - @asyncio.coroutine | ||
| 1195 | - def getaddrinfo(*args, **kw): | ||
| 1196 | - yield from [] | ||
| 1187 | + async def getaddrinfo(*args, **kw): | ||
| 1188 | + return [] | ||
| 1197 | 1189 | ||
| 1198 | 1190 | def getaddrinfo_task(*args, **kwds): | |
| 1199 | 1191 | return asyncio.Task(getaddrinfo(*args, **kwds), loop=self.loop) | |
@@ -1219,8 +1211,7 @@ def getaddrinfo_task(*args, **kwds): | |||
| 1219 | 1211 | OSError, self.loop.run_until_complete, coro) | |
| 1220 | 1212 | ||
| 1221 | 1213 | def test_create_connection_multiple(self): | |
| 1222 | - @asyncio.coroutine | ||
| 1223 | - def getaddrinfo(*args, **kw): | ||
| 1214 | + async def getaddrinfo(*args, **kw): | ||
| 1224 | 1215 | return [(2, 1, 6, '', ('0.0.0.1', 80)), | |
| 1225 | 1216 | (2, 1, 6, '', ('0.0.0.2', 80))] | |
| 1226 | 1217 | ||
@@ -1247,8 +1238,7 @@ def bind(addr): | |||
| 1247 | 1238 | ||
| 1248 | 1239 | m_socket.socket.return_value.bind = bind | |
| 1249 | 1240 | ||
| 1250 | - @asyncio.coroutine | ||
| 1251 | - def getaddrinfo(*args, **kw): | ||
| 1241 | + async def getaddrinfo(*args, **kw): | ||
| 1252 | 1242 | return [(2, 1, 6, '', ('0.0.0.1', 80)), | |
| 1253 | 1243 | (2, 1, 6, '', ('0.0.0.2', 80))] | |
| 1254 | 1244 | ||
@@ -1349,8 +1339,7 @@ def test_create_connection_service_name(self, m_socket): | |||
| 1349 | 1339 | self.loop.run_until_complete(coro) | |
| 1350 | 1340 | ||
| 1351 | 1341 | def test_create_connection_no_local_addr(self): | |
| 1352 | - @asyncio.coroutine | ||
| 1353 | - def getaddrinfo(host, *args, **kw): | ||
| 1342 | + async def getaddrinfo(host, *args, **kw): | ||
| 1354 | 1343 | if host == 'example.com': | |
| 1355 | 1344 | return [(2, 1, 6, '', ('107.6.106.82', 80)), | |
| 1356 | 1345 | (2, 1, 6, '', ('107.6.106.82', 80))] | |
@@ -1488,11 +1477,10 @@ def test_create_server_empty_host(self): | |||
| 1488 | 1477 | # if host is empty string use None instead | |
| 1489 | 1478 | host = object() | |
| 1490 | 1479 | ||
| 1491 | - @asyncio.coroutine | ||
| 1492 | - def getaddrinfo(*args, **kw): | ||
| 1480 | + async def getaddrinfo(*args, **kw): | ||
| 1493 | 1481 | nonlocal host | |
| 1494 | 1482 | host = args[0] | |
| 1495 | - yield from [] | ||
| 1483 | + return [] | ||
| 1496 | 1484 | ||
| 1497 | 1485 | def getaddrinfo_task(*args, **kwds): | |
| 1498 | 1486 | return asyncio.Task(getaddrinfo(*args, **kwds), loop=self.loop) | |
@@ -1854,9 +1842,10 @@ def test_accept_connection_exception(self, m_log): | |||
| 1854 | 1842 | MyProto, sock, None, None, mock.ANY, mock.ANY) | |
| 1855 | 1843 | ||
| 1856 | 1844 | def test_call_coroutine(self): | |
| 1857 | - @asyncio.coroutine | ||
| 1858 | - def simple_coroutine(): | ||
| 1859 | - pass | ||
| 1845 | + with self.assertWarns(DeprecationWarning): | ||
| 1846 | + @asyncio.coroutine | ||
| 1847 | + def simple_coroutine(): | ||
| 1848 | + pass | ||
| 1860 | 1849 | ||
| 1861 | 1850 | self.loop.set_debug(True) | |
| 1862 | 1851 | coro_func = simple_coroutine | |
@@ -1880,9 +1869,7 @@ def test_log_slow_callbacks(self, m_logger): | |||
| 1880 | 1869 | def stop_loop_cb(loop): | |
| 1881 | 1870 | loop.stop() | |
| 1882 | 1871 | ||
| 1883 | - @asyncio.coroutine | ||
| 1884 | - def stop_loop_coro(loop): | ||
| 1885 | - yield from () | ||
| 1872 | + async def stop_loop_coro(loop): | ||
| 1886 | 1873 | loop.stop() | |
| 1887 | 1874 | ||
| 1888 | 1875 | asyncio.set_event_loop(self.loop) | |
@@ -1909,8 +1896,7 @@ def stop_loop_coro(loop): | |||
| 1909 | 1896 | class RunningLoopTests(unittest.TestCase): | |
| 1910 | 1897 | ||
| 1911 | 1898 | def test_running_loop_within_a_loop(self): | |
| 1912 | - @asyncio.coroutine | ||
| 1913 | - def runner(loop): | ||
| 1899 | + async def runner(loop): | ||
| 1914 | 1900 | loop.run_forever() | |
| 1915 | 1901 | ||
| 1916 | 1902 | loop = asyncio.new_event_loop() | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -253,12 +253,10 @@ def tearDown(self): | |||
| 253 | 253 | super().tearDown() | |
| 254 | 254 | ||
| 255 | 255 | def test_run_until_complete_nesting(self): | |
| 256 | - @asyncio.coroutine | ||
| 257 | - def coro1(): | ||
| 258 | - yield | ||
| 256 | + async def coro1(): | ||
| 257 | + await asyncio.sleep(0) | ||
| 259 | 258 | ||
| 260 | - @asyncio.coroutine | ||
| 261 | - def coro2(): | ||
| 259 | + async def coro2(): | ||
| 262 | 260 | self.assertTrue(self.loop.is_running()) | |
| 263 | 261 | self.loop.run_until_complete(coro1()) | |
| 264 | 262 | ||
@@ -735,8 +733,7 @@ def test_connect_accepted_socket_ssl_timeout_for_plain_socket(self): | |||
| 735 | 733 | ||
| 736 | 734 | @mock.patch('asyncio.base_events.socket') | |
| 737 | 735 | def create_server_multiple_hosts(self, family, hosts, mock_sock): | |
| 738 | - @asyncio.coroutine | ||
| 739 | - def getaddrinfo(host, port, *args, **kw): | ||
| 736 | + async def getaddrinfo(host, port, *args, **kw): | ||
| 740 | 737 | if family == socket.AF_INET: | |
| 741 | 738 | return [(family, socket.SOCK_STREAM, 6, '', (host, port))] | |
| 742 | 739 | else: | |
@@ -1662,8 +1659,7 @@ def test_add_fds_after_closing(self): | |||
| 1662 | 1659 | loop.add_writer(w, callback) | |
| 1663 | 1660 | ||
| 1664 | 1661 | def test_close_running_event_loop(self): | |
| 1665 | - @asyncio.coroutine | ||
| 1666 | - def close_loop(loop): | ||
| 1662 | + async def close_loop(loop): | ||
| 1667 | 1663 | self.loop.close() | |
| 1668 | 1664 | ||
| 1669 | 1665 | coro = close_loop(self.loop) | |
@@ -1673,8 +1669,7 @@ def close_loop(loop): | |||
| 1673 | 1669 | def test_close(self): | |
| 1674 | 1670 | self.loop.close() | |
| 1675 | 1671 | ||
| 1676 | - @asyncio.coroutine | ||
| 1677 | - def test(): | ||
| 1672 | + async def test(): | ||
| 1678 | 1673 | pass | |
| 1679 | 1674 | ||
| 1680 | 1675 | func = lambda: False | |
@@ -2142,7 +2137,8 @@ def test_handle_repr(self): | |||
| 2142 | 2137 | '<Handle cancelled>') | |
| 2143 | 2138 | ||
| 2144 | 2139 | # decorated function | |
| 2145 | - cb = asyncio.coroutine(noop) | ||
| 2140 | + with self.assertWarns(DeprecationWarning): | ||
| 2141 | + cb = asyncio.coroutine(noop) | ||
| 2146 | 2142 | h = asyncio.Handle(cb, (), self.loop) | |
| 2147 | 2143 | self.assertEqual(repr(h), | |
| 2148 | 2144 | '<Handle noop() at %s:%s>' | |
| Back | FazBrowse Home | New Git URL |
0 commit comments