@@ -7,6 +7,15 @@ use pyo3::{prelude::*, py_run};
7
7
#[ path = "../src/tests/common.rs" ]
8
8
mod common;
9
9
10
+ fn handle_windows ( test : & str ) -> String {
11
+ let set_event_loop_policy = r#"
12
+ import asyncio, sys
13
+ if sys.platform == "win32":
14
+ asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
15
+ "# ;
16
+ pyo3:: unindent:: unindent ( set_event_loop_policy) + & pyo3:: unindent:: unindent ( test)
17
+ }
18
+
10
19
#[ test]
11
20
fn noop_coroutine ( ) {
12
21
#[ pyfunction]
@@ -16,7 +25,7 @@ fn noop_coroutine() {
16
25
Python :: with_gil ( |gil| {
17
26
let noop = wrap_pyfunction ! ( noop, gil) . unwrap ( ) ;
18
27
let test = "import asyncio; assert asyncio.run(noop()) == 42" ;
19
- py_run ! ( gil, noop, test) ;
28
+ py_run ! ( gil, noop, & handle_windows ( test) ) ;
20
29
} )
21
30
}
22
31
@@ -38,7 +47,7 @@ fn sleep_0_like_coroutine() {
38
47
Python :: with_gil ( |gil| {
39
48
let sleep_0 = wrap_pyfunction ! ( sleep_0, gil) . unwrap ( ) ;
40
49
let test = "import asyncio; assert asyncio.run(sleep_0()) == 42" ;
41
- py_run ! ( gil, sleep_0, test) ;
50
+ py_run ! ( gil, sleep_0, & handle_windows ( test) ) ;
42
51
} )
43
52
}
44
53
@@ -56,13 +65,8 @@ async fn sleep(seconds: f64) -> usize {
56
65
fn sleep_coroutine ( ) {
57
66
Python :: with_gil ( |gil| {
58
67
let sleep = wrap_pyfunction ! ( sleep, gil) . unwrap ( ) ;
59
- let test = r#"
60
- import asyncio, sys
61
- if sys.platform == "win32":
62
- asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
63
- assert asyncio.run(sleep(0.1)) == 42
64
- "# ;
65
- py_run ! ( gil, sleep, test) ;
68
+ let test = r#"import asyncio; assert asyncio.run(sleep(0.1)) == 42"# ;
69
+ py_run ! ( gil, sleep, & handle_windows( test) ) ;
66
70
} )
67
71
}
68
72
@@ -71,9 +75,7 @@ fn cancelled_coroutine() {
71
75
Python :: with_gil ( |gil| {
72
76
let sleep = wrap_pyfunction ! ( sleep, gil) . unwrap ( ) ;
73
77
let test = r#"
74
- import asyncio, sys
75
- if sys.platform == "win32":
76
- asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
78
+ import asyncio
77
79
async def main():
78
80
task = asyncio.create_task(sleep(1))
79
81
await asyncio.sleep(0)
@@ -84,7 +86,11 @@ fn cancelled_coroutine() {
84
86
let globals = gil. import ( "__main__" ) . unwrap ( ) . dict ( ) ;
85
87
globals. set_item ( "sleep" , sleep) . unwrap ( ) ;
86
88
let err = gil
87
- . run ( & pyo3:: unindent:: unindent ( test) , Some ( globals) , None )
89
+ . run (
90
+ & pyo3:: unindent:: unindent ( & handle_windows ( test) ) ,
91
+ Some ( globals) ,
92
+ None ,
93
+ )
88
94
. unwrap_err ( ) ;
89
95
assert_eq ! ( err. value( gil) . get_type( ) . name( ) . unwrap( ) , "CancelledError" ) ;
90
96
} )
0 commit comments