1
1
import asyncio as __asyncio
2
2
import typing as _typing
3
- import sys as _sys
3
+ import sys
4
4
import warnings as _warnings
5
5
6
6
from asyncio .events import BaseDefaultEventLoopPolicy as __BasePolicy
@@ -27,7 +27,7 @@ def new_event_loop() -> Loop:
27
27
28
28
def install () -> None :
29
29
"""A helper function to install uvloop policy."""
30
- if _sys .version_info [:2 ] >= (3 , 12 ):
30
+ if sys .version_info [:2 ] >= (3 , 12 ):
31
31
_warnings .warn (
32
32
'uvloop.install() is deprecated in favor of uvloop.run() '
33
33
'starting with Python 3.12.' ,
@@ -37,15 +37,6 @@ def install() -> None:
37
37
__asyncio .set_event_loop_policy (EventLoopPolicy ())
38
38
39
39
40
- @_typing .overload
41
- def run (
42
- main : _typing .Coroutine [_typing .Any , _typing .Any , _T ],
43
- * ,
44
- debug : _typing .Optional [bool ] = ...,
45
- loop_factory : _typing .Optional [_typing .Callable [[], Loop ]] = ...,
46
- ) -> _T : ...
47
-
48
-
49
40
def run (
50
41
main : _typing .Coroutine [_typing .Any , _typing .Any , _T ],
51
42
* ,
@@ -64,35 +55,15 @@ async def wrapper() -> _T:
64
55
raise TypeError ('uvloop.run() uses a non-uvloop event loop' )
65
56
return await main
66
57
67
- vi = _sys .version_info [:2 ]
68
-
69
- if vi <= (3 , 10 ):
70
- # Copied from python/cpython
71
-
72
- if __asyncio ._get_running_loop () is not None :
73
- raise RuntimeError (
74
- "asyncio.run() cannot be called from a running event loop" )
75
-
76
- if not __asyncio .iscoroutine (main ):
77
- raise ValueError ("a coroutine was expected, got {!r}" .format (main ))
58
+ if sys .version_info >= (3 , 12 ):
59
+ return __asyncio .run (
60
+ wrapper (),
61
+ loop_factory = loop_factory ,
62
+ debug = debug ,
63
+ ** run_kwargs
64
+ )
78
65
79
- loop = loop_factory ()
80
- try :
81
- __asyncio .set_event_loop (loop )
82
- if debug is not None :
83
- loop .set_debug (debug )
84
- return loop .run_until_complete (wrapper ())
85
- finally :
86
- try :
87
- _cancel_all_tasks (loop )
88
- loop .run_until_complete (loop .shutdown_asyncgens ())
89
- if hasattr (loop , 'shutdown_default_executor' ):
90
- loop .run_until_complete (loop .shutdown_default_executor ())
91
- finally :
92
- __asyncio .set_event_loop (None )
93
- loop .close ()
94
-
95
- elif vi == (3 , 11 ):
66
+ if sys .version_info >= (3 , 11 ):
96
67
if __asyncio ._get_running_loop () is not None :
97
68
raise RuntimeError (
98
69
"asyncio.run() cannot be called from a running event loop" )
@@ -104,14 +75,30 @@ async def wrapper() -> _T:
104
75
) as runner :
105
76
return runner .run (wrapper ())
106
77
107
- else :
108
- assert vi >= (3 , 12 )
109
- return __asyncio .run (
110
- wrapper (),
111
- loop_factory = loop_factory ,
112
- debug = debug ,
113
- ** run_kwargs
114
- )
78
+ # Copied from python/cpython
79
+
80
+ if __asyncio ._get_running_loop () is not None :
81
+ raise RuntimeError (
82
+ "asyncio.run() cannot be called from a running event loop" )
83
+
84
+ if not __asyncio .iscoroutine (main ):
85
+ raise ValueError ("a coroutine was expected, got {!r}" .format (main ))
86
+
87
+ loop = loop_factory ()
88
+ try :
89
+ __asyncio .set_event_loop (loop )
90
+ if debug is not None :
91
+ loop .set_debug (debug )
92
+ return loop .run_until_complete (wrapper ())
93
+ finally :
94
+ try :
95
+ _cancel_all_tasks (loop )
96
+ loop .run_until_complete (loop .shutdown_asyncgens ())
97
+ if hasattr (loop , 'shutdown_default_executor' ):
98
+ loop .run_until_complete (loop .shutdown_default_executor ())
99
+ finally :
100
+ __asyncio .set_event_loop (None )
101
+ loop .close ()
115
102
116
103
117
104
def _cancel_all_tasks (loop : __asyncio .AbstractEventLoop ) -> None :
0 commit comments