Skip to content

Commit ab65a06

Browse files
committed
update examples and tests
1 parent e3883da commit ab65a06

18 files changed

+111
-102
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ use pyo3::prelude::*;
6767
#[pyo3_async_runtimes::async_std::main]
6868
async fn main() -> PyResult<()> {
6969
let fut = Python::with_gil(|py| {
70-
let asyncio = py.import_bound("asyncio")?;
70+
let asyncio = py.import("asyncio")?;
7171
// convert asyncio.sleep into a Rust Future
7272
pyo3_async_runtimes::async_std::into_future(asyncio.call_method1("sleep", (1.into_py(py),))?)
7373
})?;
@@ -97,7 +97,7 @@ use pyo3::prelude::*;
9797
#[pyo3_async_runtimes::tokio::main]
9898
async fn main() -> PyResult<()> {
9999
let fut = Python::with_gil(|py| {
100-
let asyncio = py.import_bound("asyncio")?;
100+
let asyncio = py.import("asyncio")?;
101101
// convert asyncio.sleep into a Rust Future
102102
pyo3_async_runtimes::tokio::into_future(asyncio.call_method1("sleep", (1.into_py(py),))?)
103103
})?;
@@ -240,7 +240,7 @@ use pyo3::prelude::*;
240240
async fn main() -> PyResult<()> {
241241
let future = Python::with_gil(|py| -> PyResult<_> {
242242
// import the module containing the py_sleep function
243-
let example = py.import_bound("example")?;
243+
let example = py.import("example")?;
244244

245245
// calling the py_sleep method like a normal function
246246
// returns a coroutine
@@ -359,7 +359,7 @@ async fn main() -> PyResult<()> {
359359
// PyO3 is initialized - Ready to go
360360
361361
let fut = Python::with_gil(|py| -> PyResult<_> {
362-
let asyncio = py.import_bound("asyncio")?;
362+
let asyncio = py.import("asyncio")?;
363363
364364
// convert asyncio.sleep into a Rust Future
365365
pyo3_async_runtimes::async_std::into_future(
@@ -507,7 +507,7 @@ fn main() -> PyResult<()> {
507507
pyo3::prepare_freethreaded_python();
508508

509509
Python::with_gil(|py| {
510-
let uvloop = py.import_bound("uvloop")?;
510+
let uvloop = py.import("uvloop")?;
511511
uvloop.call_method0("install")?;
512512

513513
// store a reference for the assertion
@@ -604,7 +604,7 @@ To make things a bit easier, I decided to keep most of the old API alongside the
604604
pyo3::prepare_freethreaded_python();
605605

606606
Python::with_gil(|py| {
607-
let asyncio = py.import_bound("asyncio")?;
607+
let asyncio = py.import("asyncio")?;
608608

609609
let event_loop = asyncio.call_method0("new_event_loop")?;
610610
asyncio.call_method1("set_event_loop", (&event_loop,))?;

examples/async_std.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ use pyo3::prelude::*;
33
#[pyo3_async_runtimes::async_std::main]
44
async fn main() -> PyResult<()> {
55
let fut = Python::with_gil(|py| {
6-
let asyncio = py.import_bound("asyncio")?;
6+
let asyncio = py.import("asyncio")?;
77

88
// convert asyncio.sleep into a Rust Future
99
pyo3_async_runtimes::async_std::into_future(
10-
asyncio.call_method1("sleep", (1.into_py(py),))?,
10+
asyncio.call_method1("sleep", (1.into_pyobject(py).unwrap(),))?,
1111
)
1212
})?;
1313

examples/tokio.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ use pyo3::prelude::*;
33
#[pyo3_async_runtimes::tokio::main]
44
async fn main() -> PyResult<()> {
55
let fut = Python::with_gil(|py| {
6-
let asyncio = py.import_bound("asyncio")?;
6+
let asyncio = py.import("asyncio")?;
77

88
// convert asyncio.sleep into a Rust Future
9-
pyo3_async_runtimes::tokio::into_future(asyncio.call_method1("sleep", (1.into_py(py),))?)
9+
pyo3_async_runtimes::tokio::into_future(
10+
asyncio.call_method1("sleep", (1.into_pyobject(py).unwrap(),))?,
11+
)
1012
})?;
1113

1214
println!("sleeping for 1s");

examples/tokio_current_thread.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ use pyo3::prelude::*;
33
#[pyo3_async_runtimes::tokio::main(flavor = "current_thread")]
44
async fn main() -> PyResult<()> {
55
let fut = Python::with_gil(|py| {
6-
let asyncio = py.import_bound("asyncio")?;
6+
let asyncio = py.import("asyncio")?;
77

88
// convert asyncio.sleep into a Rust Future
9-
pyo3_async_runtimes::tokio::into_future(asyncio.call_method1("sleep", (1.into_py(py),))?)
9+
pyo3_async_runtimes::tokio::into_future(
10+
asyncio.call_method1("sleep", (1.into_pyobject(py).unwrap(),))?,
11+
)
1012
})?;
1113

1214
println!("sleeping for 1s");

examples/tokio_multi_thread.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ use pyo3::prelude::*;
33
#[pyo3_async_runtimes::tokio::main(flavor = "multi_thread", worker_threads = 10)]
44
async fn main() -> PyResult<()> {
55
let fut = Python::with_gil(|py| {
6-
let asyncio = py.import_bound("asyncio")?;
6+
let asyncio = py.import("asyncio")?;
77

88
// convert asyncio.sleep into a Rust Future
9-
pyo3_async_runtimes::tokio::into_future(asyncio.call_method1("sleep", (1.into_py(py),))?)
9+
pyo3_async_runtimes::tokio::into_future(
10+
asyncio.call_method1("sleep", (1.into_pyobject(py).unwrap(),))?,
11+
)
1012
})?;
1113

1214
println!("sleeping for 1s");

pytests/common/mod.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
use std::ffi::CString;
12
use std::{thread, time::Duration};
23

34
use pyo3::prelude::*;
45
use pyo3_async_runtimes::TaskLocals;
56

6-
pub(super) const TEST_MOD: &'static str = r#"
7+
pub(super) const TEST_MOD: &str = r#"
78
import asyncio
89
910
async def py_sleep(duration):
@@ -15,12 +16,16 @@ async def sleep_for_1s(sleep_for):
1516

1617
pub(super) async fn test_into_future(event_loop: PyObject) -> PyResult<()> {
1718
let fut = Python::with_gil(|py| {
18-
let test_mod =
19-
PyModule::from_code_bound(py, TEST_MOD, "test_rust_coroutine/test_mod.py", "test_mod")?;
19+
let test_mod = PyModule::from_code(
20+
py,
21+
&CString::new(TEST_MOD).unwrap(),
22+
&CString::new("test_rust_coroutine/test_mod.py").unwrap(),
23+
&CString::new("test_mod").unwrap(),
24+
)?;
2025

2126
pyo3_async_runtimes::into_future_with_locals(
2227
&TaskLocals::new(event_loop.into_bound(py)),
23-
test_mod.call_method1("py_sleep", (1.into_py(py),))?,
28+
test_mod.call_method1("py_sleep", (1.into_pyobject(py).unwrap(),))?,
2429
)
2530
})?;
2631

@@ -36,8 +41,8 @@ pub(super) fn test_blocking_sleep() -> PyResult<()> {
3641

3742
pub(super) async fn test_other_awaitables(event_loop: PyObject) -> PyResult<()> {
3843
let fut = Python::with_gil(|py| {
39-
let functools = py.import_bound("functools")?;
40-
let time = py.import_bound("time")?;
44+
let functools = py.import("functools")?;
45+
let time = py.import("time")?;
4146

4247
// spawn a blocking sleep in the threadpool executor - returns a task, not a coroutine
4348
let task = event_loop.bind(py).call_method1(

pytests/test_async_std_asyncio.rs

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
mod common;
22

3+
use std::ffi::CString;
34
use std::{
45
rc::Rc,
56
sync::{Arc, Mutex},
@@ -30,15 +31,15 @@ fn sleep<'p>(py: Python<'p>, secs: Bound<'p, PyAny>) -> PyResult<Bound<'p, PyAny
3031
#[pyo3_async_runtimes::async_std::test]
3132
async fn test_future_into_py() -> PyResult<()> {
3233
let fut = Python::with_gil(|py| {
33-
let sleeper_mod = PyModule::new_bound(py, "rust_sleeper")?;
34+
let sleeper_mod = PyModule::new(py, "rust_sleeper")?;
3435

3536
sleeper_mod.add_wrapped(wrap_pyfunction!(sleep))?;
3637

37-
let test_mod = PyModule::from_code_bound(
38+
let test_mod = PyModule::from_code(
3839
py,
39-
common::TEST_MOD,
40-
"test_future_into_py_mod.py",
41-
"test_future_into_py_mod",
40+
&CString::new(common::TEST_MOD).unwrap(),
41+
&CString::new("test_future_into_py_mod.py").unwrap(),
42+
&CString::new("test_future_into_py_mod").unwrap(),
4243
)?;
4344

4445
pyo3_async_runtimes::async_std::into_future(
@@ -53,10 +54,7 @@ async fn test_future_into_py() -> PyResult<()> {
5354

5455
#[pyo3_async_runtimes::async_std::test]
5556
async fn test_async_sleep() -> PyResult<()> {
56-
let asyncio = Python::with_gil(|py| {
57-
py.import_bound("asyncio")
58-
.map(|asyncio| PyObject::from(asyncio))
59-
})?;
57+
let asyncio = Python::with_gil(|py| py.import("asyncio").map(PyObject::from))?;
6058

6159
task::sleep(Duration::from_secs(1)).await;
6260

@@ -157,8 +155,8 @@ async fn test_cancel() -> PyResult<()> {
157155
.await
158156
{
159157
Python::with_gil(|py| -> PyResult<()> {
160-
assert!(e.value_bound(py).is_instance(
161-
py.import_bound("asyncio")?
158+
assert!(e.value(py).is_instance(
159+
py.import("asyncio")?
162160
.getattr("CancelledError")?
163161
.downcast::<PyType>()
164162
.unwrap()
@@ -191,18 +189,18 @@ async def gen():
191189
#[pyo3_async_runtimes::async_std::test]
192190
async fn test_async_gen_v1() -> PyResult<()> {
193191
let stream = Python::with_gil(|py| {
194-
let test_mod = PyModule::from_code_bound(
192+
let test_mod = PyModule::from_code(
195193
py,
196-
ASYNC_STD_TEST_MOD,
197-
"test_rust_coroutine/async_std_test_mod.py",
198-
"async_std_test_mod",
194+
&CString::new(ASYNC_STD_TEST_MOD).unwrap(),
195+
&CString::new("test_rust_coroutine/async_std_test_mod.py").unwrap(),
196+
&CString::new("async_std_test_mod").unwrap(),
199197
)?;
200198

201199
pyo3_async_runtimes::async_std::into_stream_v1(test_mod.call_method0("gen")?)
202200
})?;
203201

204202
let vals = stream
205-
.map(|item| Python::with_gil(|py| -> PyResult<i32> { Ok(item?.bind(py).extract()?) }))
203+
.map(|item| Python::with_gil(|py| -> PyResult<i32> { item?.bind(py).extract() }))
206204
.try_collect::<Vec<i32>>()
207205
.await?;
208206

@@ -214,7 +212,7 @@ async fn test_async_gen_v1() -> PyResult<()> {
214212
#[pyo3_async_runtimes::async_std::test]
215213
fn test_local_cancel(event_loop: PyObject) -> PyResult<()> {
216214
let locals = Python::with_gil(|py| -> PyResult<TaskLocals> {
217-
Ok(TaskLocals::new(event_loop.into_bound(py)).copy_context(py)?)
215+
TaskLocals::new(event_loop.into_bound(py)).copy_context(py)
218216
})?;
219217
async_std::task::block_on(pyo3_async_runtimes::async_std::scope_local(locals, async {
220218
let completed = Arc::new(Mutex::new(false));
@@ -239,8 +237,8 @@ fn test_local_cancel(event_loop: PyObject) -> PyResult<()> {
239237
.await
240238
{
241239
Python::with_gil(|py| -> PyResult<()> {
242-
assert!(e.value_bound(py).is_instance(
243-
py.import_bound("asyncio")?
240+
assert!(e.value(py).is_instance(
241+
py.import("asyncio")?
244242
.getattr("CancelledError")?
245243
.downcast::<PyType>()
246244
.unwrap()
@@ -297,13 +295,13 @@ fn test_multiple_asyncio_run() -> PyResult<()> {
297295
})?;
298296

299297
let d = [
300-
("asyncio", py.import_bound("asyncio")?.into()),
298+
("asyncio", py.import("asyncio")?.into()),
301299
("test_mod", wrap_pymodule!(test_mod)(py)),
302300
]
303-
.into_py_dict_bound(py);
301+
.into_py_dict(py)?;
304302

305-
py.run_bound(MULTI_ASYNCIO_CODE, Some(&d), None)?;
306-
py.run_bound(MULTI_ASYNCIO_CODE, Some(&d), None)?;
303+
py.run(&CString::new(MULTI_ASYNCIO_CODE).unwrap(), Some(&d), None)?;
304+
py.run(&CString::new(MULTI_ASYNCIO_CODE).unwrap(), Some(&d), None)?;
307305
Ok(())
308306
})
309307
}
@@ -332,18 +330,18 @@ fn cvars_mod(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
332330
#[pyo3_async_runtimes::async_std::test]
333331
async fn test_async_gen_v2() -> PyResult<()> {
334332
let stream = Python::with_gil(|py| {
335-
let test_mod = PyModule::from_code_bound(
333+
let test_mod = PyModule::from_code(
336334
py,
337-
ASYNC_STD_TEST_MOD,
338-
"test_rust_coroutine/async_std_test_mod.py",
339-
"async_std_test_mod",
335+
&CString::new(ASYNC_STD_TEST_MOD).unwrap(),
336+
&CString::new("test_rust_coroutine/async_std_test_mod.py").unwrap(),
337+
&CString::new("async_std_test_mod").unwrap(),
340338
)?;
341339

342340
pyo3_async_runtimes::async_std::into_stream_v2(test_mod.call_method0("gen")?)
343341
})?;
344342

345343
let vals = stream
346-
.map(|item| Python::with_gil(|py| -> PyResult<i32> { Ok(item.bind(py).extract()?) }))
344+
.map(|item| Python::with_gil(|py| -> PyResult<i32> { item.bind(py).extract() }))
347345
.try_collect::<Vec<i32>>()
348346
.await?;
349347

@@ -369,14 +367,14 @@ asyncio.run(main())
369367
fn test_contextvars() -> PyResult<()> {
370368
Python::with_gil(|py| {
371369
let d = [
372-
("asyncio", py.import_bound("asyncio")?.into()),
373-
("contextvars", py.import_bound("contextvars")?.into()),
370+
("asyncio", py.import("asyncio")?.into()),
371+
("contextvars", py.import("contextvars")?.into()),
374372
("cvars_mod", wrap_pymodule!(cvars_mod)(py)),
375373
]
376-
.into_py_dict_bound(py);
374+
.into_py_dict(py)?;
377375

378-
py.run_bound(CONTEXTVARS_CODE, Some(&d), None)?;
379-
py.run_bound(CONTEXTVARS_CODE, Some(&d), None)?;
376+
py.run(&CString::new(CONTEXTVARS_CODE).unwrap(), Some(&d), None)?;
377+
py.run(&CString::new(CONTEXTVARS_CODE).unwrap(), Some(&d), None)?;
380378
Ok(())
381379
})
382380
}

pytests/test_async_std_run_forever.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fn main() {
1212
pyo3::prepare_freethreaded_python();
1313

1414
Python::with_gil(|py| {
15-
let asyncio = py.import_bound("asyncio")?;
15+
let asyncio = py.import("asyncio")?;
1616

1717
let event_loop = asyncio.call_method0("new_event_loop")?;
1818
asyncio.call_method1("set_event_loop", (&event_loop,))?;

pytests/test_async_std_uvloop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ fn main() -> pyo3::PyResult<()> {
44
pyo3::prepare_freethreaded_python();
55

66
Python::with_gil(|py| {
7-
let uvloop = py.import_bound("uvloop")?;
7+
let uvloop = py.import("uvloop")?;
88
uvloop.call_method0("install")?;
99

1010
// store a reference for the assertion

pytests/test_race_condition_regression.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::ffi::CString;
2+
13
use pyo3::{prelude::*, wrap_pyfunction};
24

35
#[pyfunction]
@@ -49,15 +51,15 @@ fn main() -> pyo3::PyResult<()> {
4951
pyo3::prepare_freethreaded_python();
5052

5153
Python::with_gil(|py| -> PyResult<()> {
52-
let sleeper_mod = PyModule::new_bound(py, "rust_sleeper")?;
54+
let sleeper_mod = PyModule::new(py, "rust_sleeper")?;
5355

5456
sleeper_mod.add_wrapped(wrap_pyfunction!(sleep))?;
5557

56-
let test_mod = PyModule::from_code_bound(
58+
let test_mod = PyModule::from_code(
5759
py,
58-
RACE_CONDITION_REGRESSION_TEST,
59-
"race_condition_regression_test.py",
60-
"race_condition_regression_test",
60+
&CString::new(RACE_CONDITION_REGRESSION_TEST).unwrap(),
61+
&CString::new("race_condition_regression_test.py").unwrap(),
62+
&CString::new("race_condition_regression_test").unwrap(),
6163
)?;
6264

6365
test_mod.call_method1("main", (sleeper_mod.getattr("sleep")?,))?;

0 commit comments

Comments
 (0)