Skip to content

Commit 49ae17c

Browse files
author
Joseph Perez
committed
fix: fix coroutine tests on windows (2nd try)
1 parent af23bce commit 49ae17c

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

tests/test_coroutine.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ use pyo3::{prelude::*, py_run};
77
#[path = "../src/tests/common.rs"]
88
mod common;
99

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+
1019
#[test]
1120
fn noop_coroutine() {
1221
#[pyfunction]
@@ -16,7 +25,7 @@ fn noop_coroutine() {
1625
Python::with_gil(|gil| {
1726
let noop = wrap_pyfunction!(noop, gil).unwrap();
1827
let test = "import asyncio; assert asyncio.run(noop()) == 42";
19-
py_run!(gil, noop, test);
28+
py_run!(gil, noop, &handle_windows(test));
2029
})
2130
}
2231

@@ -38,7 +47,7 @@ fn sleep_0_like_coroutine() {
3847
Python::with_gil(|gil| {
3948
let sleep_0 = wrap_pyfunction!(sleep_0, gil).unwrap();
4049
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));
4251
})
4352
}
4453

@@ -56,13 +65,8 @@ async fn sleep(seconds: f64) -> usize {
5665
fn sleep_coroutine() {
5766
Python::with_gil(|gil| {
5867
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));
6670
})
6771
}
6872

@@ -71,9 +75,7 @@ fn cancelled_coroutine() {
7175
Python::with_gil(|gil| {
7276
let sleep = wrap_pyfunction!(sleep, gil).unwrap();
7377
let test = r#"
74-
import asyncio, sys
75-
if sys.platform == "win32":
76-
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
78+
import asyncio
7779
async def main():
7880
task = asyncio.create_task(sleep(1))
7981
await asyncio.sleep(0)
@@ -84,7 +86,11 @@ fn cancelled_coroutine() {
8486
let globals = gil.import("__main__").unwrap().dict();
8587
globals.set_item("sleep", sleep).unwrap();
8688
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+
)
8894
.unwrap_err();
8995
assert_eq!(err.value(gil).get_type().name().unwrap(), "CancelledError");
9096
})

0 commit comments

Comments
 (0)