diff --git a/Cargo.toml b/Cargo.toml index 00fe4e0..c5bb6b8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "pyo3-async-runtimes" description = "PyO3 bridges from Rust runtimes to Python's Asyncio library" -version = "0.22.0" +version = "0.23.0" authors = [ "Andrew J Westlake ", "David Hewitt ", @@ -120,11 +120,11 @@ futures = "0.3" inventory = { version = "0.3", optional = true } once_cell = "1.14" pin-project-lite = "0.2" -pyo3 = "0.22" -pyo3-async-runtimes-macros = { path = "pyo3-async-runtimes-macros", version = "=0.22.0", optional = true } +pyo3 = "0.23" +pyo3-async-runtimes-macros = { path = "pyo3-async-runtimes-macros", version = "=0.23.0", optional = true } [dev-dependencies] -pyo3 = { version = "0.22", features = ["macros"] } +pyo3 = { version = "0.23", features = ["macros"] } [dependencies.async-std] version = "1.12" diff --git a/README.md b/README.md index 1eb3376..b98403a 100644 --- a/README.md +++ b/README.md @@ -54,8 +54,8 @@ Here we initialize the runtime, import Python's `asyncio` library and run the gi ```toml # Cargo.toml dependencies [dependencies] -pyo3 = { version = "0.22" } -pyo3-async-runtimes = { version = "0.22", features = ["attributes", "async-std-runtime"] } +pyo3 = { version = "0.23" } +pyo3-async-runtimes = { version = "0.23", features = ["attributes", "async-std-runtime"] } async-std = "1.13" ``` @@ -67,9 +67,9 @@ use pyo3::prelude::*; #[pyo3_async_runtimes::async_std::main] async fn main() -> PyResult<()> { let fut = Python::with_gil(|py| { - let asyncio = py.import_bound("asyncio")?; + let asyncio = py.import("asyncio")?; // convert asyncio.sleep into a Rust Future - pyo3_async_runtimes::async_std::into_future(asyncio.call_method1("sleep", (1.into_py(py),))?) + pyo3_async_runtimes::async_std::into_future(asyncio.call_method1("sleep", (1.into_pyobject(py).unwrap(),))?) })?; fut.await?; @@ -84,8 +84,8 @@ attribute. ```toml # Cargo.toml dependencies [dependencies] -pyo3 = { version = "0.22" } -pyo3-async-runtimes = { version = "0.22", features = ["attributes", "tokio-runtime"] } +pyo3 = { version = "0.23" } +pyo3-async-runtimes = { version = "0.23", features = ["attributes", "tokio-runtime"] } tokio = "1.40" ``` @@ -97,9 +97,9 @@ use pyo3::prelude::*; #[pyo3_async_runtimes::tokio::main] async fn main() -> PyResult<()> { let fut = Python::with_gil(|py| { - let asyncio = py.import_bound("asyncio")?; + let asyncio = py.import("asyncio")?; // convert asyncio.sleep into a Rust Future - pyo3_async_runtimes::tokio::into_future(asyncio.call_method1("sleep", (1.into_py(py),))?) + pyo3_async_runtimes::tokio::into_future(asyncio.call_method1("sleep", (1.into_pyobject(py).unwrap(),))?) })?; fut.await?; @@ -130,8 +130,8 @@ For `async-std`: ```toml [dependencies] -pyo3 = { version = "0.22", features = ["extension-module"] } -pyo3-async-runtimes = { version = "0.22", features = ["async-std-runtime"] } +pyo3 = { version = "0.23", features = ["extension-module"] } +pyo3-async-runtimes = { version = "0.23", features = ["async-std-runtime"] } async-std = "1.13" ``` @@ -140,7 +140,7 @@ For `tokio`: ```toml [dependencies] pyo3 = { version = "0.20", features = ["extension-module"] } -pyo3-async-runtimes = { version = "0.22", features = ["tokio-runtime"] } +pyo3-async-runtimes = { version = "0.23", features = ["tokio-runtime"] } tokio = "1.40" ``` @@ -240,7 +240,7 @@ use pyo3::prelude::*; async fn main() -> PyResult<()> { let future = Python::with_gil(|py| -> PyResult<_> { // import the module containing the py_sleep function - let example = py.import_bound("example")?; + let example = py.import("example")?; // calling the py_sleep method like a normal function // returns a coroutine @@ -359,11 +359,11 @@ async fn main() -> PyResult<()> { // PyO3 is initialized - Ready to go let fut = Python::with_gil(|py| -> PyResult<_> { - let asyncio = py.import_bound("asyncio")?; + let asyncio = py.import("asyncio")?; // convert asyncio.sleep into a Rust Future pyo3_async_runtimes::async_std::into_future( - asyncio.call_method1("sleep", (1.into_py(py),))? + asyncio.call_method1("sleep", (1.into_pyobject(py).unwrap(),))? ) })?; @@ -434,8 +434,8 @@ name = "my_async_module" crate-type = ["cdylib"] [dependencies] -pyo3 = { version = "0.22", features = ["extension-module"] } -pyo3-async-runtimes = { version = "0.22", features = ["tokio-runtime"] } +pyo3 = { version = "0.23", features = ["extension-module"] } +pyo3-async-runtimes = { version = "0.23", features = ["tokio-runtime"] } async-std = "1.13" tokio = "1.40" ``` @@ -494,8 +494,8 @@ event loop before we can install the `uvloop` policy. ```toml [dependencies] async-std = "1.13" -pyo3 = "0.22" -pyo3-async-runtimes = { version = "0.22", features = ["async-std-runtime"] } +pyo3 = "0.23" +pyo3-async-runtimes = { version = "0.23", features = ["async-std-runtime"] } ``` ```rust no_run @@ -507,7 +507,7 @@ fn main() -> PyResult<()> { pyo3::prepare_freethreaded_python(); Python::with_gil(|py| { - let uvloop = py.import_bound("uvloop")?; + let uvloop = py.import("uvloop")?; uvloop.call_method0("install")?; // 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 pyo3::prepare_freethreaded_python(); Python::with_gil(|py| { - let asyncio = py.import_bound("asyncio")?; + let asyncio = py.import("asyncio")?; let event_loop = asyncio.call_method0("new_event_loop")?; asyncio.call_method1("set_event_loop", (&event_loop,))?; diff --git a/examples/async_std.rs b/examples/async_std.rs index fc3af87..254d057 100644 --- a/examples/async_std.rs +++ b/examples/async_std.rs @@ -3,11 +3,11 @@ use pyo3::prelude::*; #[pyo3_async_runtimes::async_std::main] async fn main() -> PyResult<()> { let fut = Python::with_gil(|py| { - let asyncio = py.import_bound("asyncio")?; + let asyncio = py.import("asyncio")?; // convert asyncio.sleep into a Rust Future pyo3_async_runtimes::async_std::into_future( - asyncio.call_method1("sleep", (1.into_py(py),))?, + asyncio.call_method1("sleep", (1.into_pyobject(py).unwrap(),))?, ) })?; diff --git a/examples/tokio.rs b/examples/tokio.rs index c005136..31581d6 100644 --- a/examples/tokio.rs +++ b/examples/tokio.rs @@ -3,10 +3,12 @@ use pyo3::prelude::*; #[pyo3_async_runtimes::tokio::main] async fn main() -> PyResult<()> { let fut = Python::with_gil(|py| { - let asyncio = py.import_bound("asyncio")?; + let asyncio = py.import("asyncio")?; // convert asyncio.sleep into a Rust Future - pyo3_async_runtimes::tokio::into_future(asyncio.call_method1("sleep", (1.into_py(py),))?) + pyo3_async_runtimes::tokio::into_future( + asyncio.call_method1("sleep", (1.into_pyobject(py).unwrap(),))?, + ) })?; println!("sleeping for 1s"); diff --git a/examples/tokio_current_thread.rs b/examples/tokio_current_thread.rs index 776bc86..d076bd0 100644 --- a/examples/tokio_current_thread.rs +++ b/examples/tokio_current_thread.rs @@ -3,10 +3,12 @@ use pyo3::prelude::*; #[pyo3_async_runtimes::tokio::main(flavor = "current_thread")] async fn main() -> PyResult<()> { let fut = Python::with_gil(|py| { - let asyncio = py.import_bound("asyncio")?; + let asyncio = py.import("asyncio")?; // convert asyncio.sleep into a Rust Future - pyo3_async_runtimes::tokio::into_future(asyncio.call_method1("sleep", (1.into_py(py),))?) + pyo3_async_runtimes::tokio::into_future( + asyncio.call_method1("sleep", (1.into_pyobject(py).unwrap(),))?, + ) })?; println!("sleeping for 1s"); diff --git a/examples/tokio_multi_thread.rs b/examples/tokio_multi_thread.rs index 733dd8c..c828977 100644 --- a/examples/tokio_multi_thread.rs +++ b/examples/tokio_multi_thread.rs @@ -3,10 +3,12 @@ use pyo3::prelude::*; #[pyo3_async_runtimes::tokio::main(flavor = "multi_thread", worker_threads = 10)] async fn main() -> PyResult<()> { let fut = Python::with_gil(|py| { - let asyncio = py.import_bound("asyncio")?; + let asyncio = py.import("asyncio")?; // convert asyncio.sleep into a Rust Future - pyo3_async_runtimes::tokio::into_future(asyncio.call_method1("sleep", (1.into_py(py),))?) + pyo3_async_runtimes::tokio::into_future( + asyncio.call_method1("sleep", (1.into_pyobject(py).unwrap(),))?, + ) })?; println!("sleeping for 1s"); diff --git a/pyo3-async-runtimes-macros/Cargo.toml b/pyo3-async-runtimes-macros/Cargo.toml index dee21df..91f1b15 100644 --- a/pyo3-async-runtimes-macros/Cargo.toml +++ b/pyo3-async-runtimes-macros/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "pyo3-async-runtimes-macros" description = "Proc Macro Attributes for `pyo3-async-runtimes`" -version = "0.22.0" +version = "0.23.0" authors = [ "Andrew J Westlake ", "David Hewitt ", diff --git a/pytests/common/mod.rs b/pytests/common/mod.rs index 91008e9..67b7213 100644 --- a/pytests/common/mod.rs +++ b/pytests/common/mod.rs @@ -1,9 +1,10 @@ +use std::ffi::CString; use std::{thread, time::Duration}; use pyo3::prelude::*; use pyo3_async_runtimes::TaskLocals; -pub(super) const TEST_MOD: &'static str = r#" +pub(super) const TEST_MOD: &str = r#" import asyncio async def py_sleep(duration): @@ -15,12 +16,16 @@ async def sleep_for_1s(sleep_for): pub(super) async fn test_into_future(event_loop: PyObject) -> PyResult<()> { let fut = Python::with_gil(|py| { - let test_mod = - PyModule::from_code_bound(py, TEST_MOD, "test_rust_coroutine/test_mod.py", "test_mod")?; + let test_mod = PyModule::from_code( + py, + &CString::new(TEST_MOD).unwrap(), + &CString::new("test_rust_coroutine/test_mod.py").unwrap(), + &CString::new("test_mod").unwrap(), + )?; pyo3_async_runtimes::into_future_with_locals( &TaskLocals::new(event_loop.into_bound(py)), - test_mod.call_method1("py_sleep", (1.into_py(py),))?, + test_mod.call_method1("py_sleep", (1.into_pyobject(py).unwrap(),))?, ) })?; @@ -36,8 +41,8 @@ pub(super) fn test_blocking_sleep() -> PyResult<()> { pub(super) async fn test_other_awaitables(event_loop: PyObject) -> PyResult<()> { let fut = Python::with_gil(|py| { - let functools = py.import_bound("functools")?; - let time = py.import_bound("time")?; + let functools = py.import("functools")?; + let time = py.import("time")?; // spawn a blocking sleep in the threadpool executor - returns a task, not a coroutine let task = event_loop.bind(py).call_method1( diff --git a/pytests/test_async_std_asyncio.rs b/pytests/test_async_std_asyncio.rs index 0f4afed..cdd36a7 100644 --- a/pytests/test_async_std_asyncio.rs +++ b/pytests/test_async_std_asyncio.rs @@ -1,5 +1,6 @@ mod common; +use std::ffi::CString; use std::{ rc::Rc, sync::{Arc, Mutex}, @@ -30,15 +31,15 @@ fn sleep<'p>(py: Python<'p>, secs: Bound<'p, PyAny>) -> PyResult PyResult<()> { let fut = Python::with_gil(|py| { - let sleeper_mod = PyModule::new_bound(py, "rust_sleeper")?; + let sleeper_mod = PyModule::new(py, "rust_sleeper")?; sleeper_mod.add_wrapped(wrap_pyfunction!(sleep))?; - let test_mod = PyModule::from_code_bound( + let test_mod = PyModule::from_code( py, - common::TEST_MOD, - "test_future_into_py_mod.py", - "test_future_into_py_mod", + &CString::new(common::TEST_MOD).unwrap(), + &CString::new("test_future_into_py_mod.py").unwrap(), + &CString::new("test_future_into_py_mod").unwrap(), )?; pyo3_async_runtimes::async_std::into_future( @@ -53,10 +54,7 @@ async fn test_future_into_py() -> PyResult<()> { #[pyo3_async_runtimes::async_std::test] async fn test_async_sleep() -> PyResult<()> { - let asyncio = Python::with_gil(|py| { - py.import_bound("asyncio") - .map(|asyncio| PyObject::from(asyncio)) - })?; + let asyncio = Python::with_gil(|py| py.import("asyncio").map(PyObject::from))?; task::sleep(Duration::from_secs(1)).await; @@ -157,8 +155,8 @@ async fn test_cancel() -> PyResult<()> { .await { Python::with_gil(|py| -> PyResult<()> { - assert!(e.value_bound(py).is_instance( - py.import_bound("asyncio")? + assert!(e.value(py).is_instance( + py.import("asyncio")? .getattr("CancelledError")? .downcast::() .unwrap() @@ -191,18 +189,18 @@ async def gen(): #[pyo3_async_runtimes::async_std::test] async fn test_async_gen_v1() -> PyResult<()> { let stream = Python::with_gil(|py| { - let test_mod = PyModule::from_code_bound( + let test_mod = PyModule::from_code( py, - ASYNC_STD_TEST_MOD, - "test_rust_coroutine/async_std_test_mod.py", - "async_std_test_mod", + &CString::new(ASYNC_STD_TEST_MOD).unwrap(), + &CString::new("test_rust_coroutine/async_std_test_mod.py").unwrap(), + &CString::new("async_std_test_mod").unwrap(), )?; pyo3_async_runtimes::async_std::into_stream_v1(test_mod.call_method0("gen")?) })?; let vals = stream - .map(|item| Python::with_gil(|py| -> PyResult { Ok(item?.bind(py).extract()?) })) + .map(|item| Python::with_gil(|py| -> PyResult { item?.bind(py).extract() })) .try_collect::>() .await?; @@ -214,7 +212,7 @@ async fn test_async_gen_v1() -> PyResult<()> { #[pyo3_async_runtimes::async_std::test] fn test_local_cancel(event_loop: PyObject) -> PyResult<()> { let locals = Python::with_gil(|py| -> PyResult { - Ok(TaskLocals::new(event_loop.into_bound(py)).copy_context(py)?) + TaskLocals::new(event_loop.into_bound(py)).copy_context(py) })?; async_std::task::block_on(pyo3_async_runtimes::async_std::scope_local(locals, async { let completed = Arc::new(Mutex::new(false)); @@ -239,8 +237,8 @@ fn test_local_cancel(event_loop: PyObject) -> PyResult<()> { .await { Python::with_gil(|py| -> PyResult<()> { - assert!(e.value_bound(py).is_instance( - py.import_bound("asyncio")? + assert!(e.value(py).is_instance( + py.import("asyncio")? .getattr("CancelledError")? .downcast::() .unwrap() @@ -297,13 +295,13 @@ fn test_multiple_asyncio_run() -> PyResult<()> { })?; let d = [ - ("asyncio", py.import_bound("asyncio")?.into()), + ("asyncio", py.import("asyncio")?.into()), ("test_mod", wrap_pymodule!(test_mod)(py)), ] - .into_py_dict_bound(py); + .into_py_dict(py)?; - py.run_bound(MULTI_ASYNCIO_CODE, Some(&d), None)?; - py.run_bound(MULTI_ASYNCIO_CODE, Some(&d), None)?; + py.run(&CString::new(MULTI_ASYNCIO_CODE).unwrap(), Some(&d), None)?; + py.run(&CString::new(MULTI_ASYNCIO_CODE).unwrap(), Some(&d), None)?; Ok(()) }) } @@ -332,18 +330,18 @@ fn cvars_mod(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> { #[pyo3_async_runtimes::async_std::test] async fn test_async_gen_v2() -> PyResult<()> { let stream = Python::with_gil(|py| { - let test_mod = PyModule::from_code_bound( + let test_mod = PyModule::from_code( py, - ASYNC_STD_TEST_MOD, - "test_rust_coroutine/async_std_test_mod.py", - "async_std_test_mod", + &CString::new(ASYNC_STD_TEST_MOD).unwrap(), + &CString::new("test_rust_coroutine/async_std_test_mod.py").unwrap(), + &CString::new("async_std_test_mod").unwrap(), )?; pyo3_async_runtimes::async_std::into_stream_v2(test_mod.call_method0("gen")?) })?; let vals = stream - .map(|item| Python::with_gil(|py| -> PyResult { Ok(item.bind(py).extract()?) })) + .map(|item| Python::with_gil(|py| -> PyResult { item.bind(py).extract() })) .try_collect::>() .await?; @@ -369,14 +367,14 @@ asyncio.run(main()) fn test_contextvars() -> PyResult<()> { Python::with_gil(|py| { let d = [ - ("asyncio", py.import_bound("asyncio")?.into()), - ("contextvars", py.import_bound("contextvars")?.into()), + ("asyncio", py.import("asyncio")?.into()), + ("contextvars", py.import("contextvars")?.into()), ("cvars_mod", wrap_pymodule!(cvars_mod)(py)), ] - .into_py_dict_bound(py); + .into_py_dict(py)?; - py.run_bound(CONTEXTVARS_CODE, Some(&d), None)?; - py.run_bound(CONTEXTVARS_CODE, Some(&d), None)?; + py.run(&CString::new(CONTEXTVARS_CODE).unwrap(), Some(&d), None)?; + py.run(&CString::new(CONTEXTVARS_CODE).unwrap(), Some(&d), None)?; Ok(()) }) } diff --git a/pytests/test_async_std_run_forever.rs b/pytests/test_async_std_run_forever.rs index ecc76f9..70928bc 100644 --- a/pytests/test_async_std_run_forever.rs +++ b/pytests/test_async_std_run_forever.rs @@ -12,7 +12,7 @@ fn main() { pyo3::prepare_freethreaded_python(); Python::with_gil(|py| { - let asyncio = py.import_bound("asyncio")?; + let asyncio = py.import("asyncio")?; let event_loop = asyncio.call_method0("new_event_loop")?; asyncio.call_method1("set_event_loop", (&event_loop,))?; diff --git a/pytests/test_async_std_uvloop.rs b/pytests/test_async_std_uvloop.rs index 57e9a73..972cebd 100644 --- a/pytests/test_async_std_uvloop.rs +++ b/pytests/test_async_std_uvloop.rs @@ -4,7 +4,7 @@ fn main() -> pyo3::PyResult<()> { pyo3::prepare_freethreaded_python(); Python::with_gil(|py| { - let uvloop = py.import_bound("uvloop")?; + let uvloop = py.import("uvloop")?; uvloop.call_method0("install")?; // store a reference for the assertion diff --git a/pytests/test_race_condition_regression.rs b/pytests/test_race_condition_regression.rs index 5a382aa..cfd2c71 100644 --- a/pytests/test_race_condition_regression.rs +++ b/pytests/test_race_condition_regression.rs @@ -1,3 +1,5 @@ +use std::ffi::CString; + use pyo3::{prelude::*, wrap_pyfunction}; #[pyfunction] @@ -49,15 +51,15 @@ fn main() -> pyo3::PyResult<()> { pyo3::prepare_freethreaded_python(); Python::with_gil(|py| -> PyResult<()> { - let sleeper_mod = PyModule::new_bound(py, "rust_sleeper")?; + let sleeper_mod = PyModule::new(py, "rust_sleeper")?; sleeper_mod.add_wrapped(wrap_pyfunction!(sleep))?; - let test_mod = PyModule::from_code_bound( + let test_mod = PyModule::from_code( py, - RACE_CONDITION_REGRESSION_TEST, - "race_condition_regression_test.py", - "race_condition_regression_test", + &CString::new(RACE_CONDITION_REGRESSION_TEST).unwrap(), + &CString::new("race_condition_regression_test.py").unwrap(), + &CString::new("race_condition_regression_test").unwrap(), )?; test_mod.call_method1("main", (sleeper_mod.getattr("sleep")?,))?; diff --git a/pytests/test_tokio_current_thread_uvloop.rs b/pytests/test_tokio_current_thread_uvloop.rs index 04f3cbc..2d1b47d 100644 --- a/pytests/test_tokio_current_thread_uvloop.rs +++ b/pytests/test_tokio_current_thread_uvloop.rs @@ -13,7 +13,7 @@ fn main() -> pyo3::PyResult<()> { }); Python::with_gil(|py| { - let uvloop = py.import_bound("uvloop")?; + let uvloop = py.import("uvloop")?; uvloop.call_method0("install")?; // store a reference for the assertion diff --git a/pytests/test_tokio_multi_thread_uvloop.rs b/pytests/test_tokio_multi_thread_uvloop.rs index 29b8263..f664919 100644 --- a/pytests/test_tokio_multi_thread_uvloop.rs +++ b/pytests/test_tokio_multi_thread_uvloop.rs @@ -5,7 +5,7 @@ fn main() -> pyo3::PyResult<()> { pyo3::prepare_freethreaded_python(); Python::with_gil(|py| { - let uvloop = py.import_bound("uvloop")?; + let uvloop = py.import("uvloop")?; uvloop.call_method0("install")?; // store a reference for the assertion diff --git a/pytests/tokio_asyncio/mod.rs b/pytests/tokio_asyncio/mod.rs index 740619e..afb31f7 100644 --- a/pytests/tokio_asyncio/mod.rs +++ b/pytests/tokio_asyncio/mod.rs @@ -1,3 +1,4 @@ +use std::ffi::CString; use std::{ rc::Rc, sync::{Arc, Mutex}, @@ -29,15 +30,15 @@ fn sleep<'p>(py: Python<'p>, secs: Bound<'p, PyAny>) -> PyResult PyResult<()> { let fut = Python::with_gil(|py| { - let sleeper_mod = PyModule::new_bound(py, "rust_sleeper")?; + let sleeper_mod = PyModule::new(py, "rust_sleeper")?; sleeper_mod.add_wrapped(wrap_pyfunction!(sleep))?; - let test_mod = PyModule::from_code_bound( + let test_mod = PyModule::from_code( py, - common::TEST_MOD, - "test_future_into_py_mod.py", - "test_future_into_py_mod", + &CString::new(common::TEST_MOD).unwrap(), + &CString::new("test_future_into_py_mod.py").unwrap(), + &CString::new("test_future_into_py_mod").unwrap(), )?; pyo3_async_runtimes::tokio::into_future( @@ -52,10 +53,7 @@ async fn test_future_into_py() -> PyResult<()> { #[pyo3_async_runtimes::tokio::test] async fn test_async_sleep() -> PyResult<()> { - let asyncio = Python::with_gil(|py| { - py.import_bound("asyncio") - .map(|asyncio| PyObject::from(asyncio)) - })?; + let asyncio = Python::with_gil(|py| py.import("asyncio").map(PyObject::from))?; tokio::time::sleep(Duration::from_secs(1)).await; @@ -163,8 +161,8 @@ async fn test_cancel() -> PyResult<()> { .await { Python::with_gil(|py| -> PyResult<()> { - assert!(e.value_bound(py).is_instance( - py.import_bound("asyncio")? + assert!(e.value(py).is_instance( + py.import("asyncio")? .getattr("CancelledError")? .downcast::() .unwrap() @@ -187,7 +185,7 @@ async fn test_cancel() -> PyResult<()> { #[allow(deprecated)] fn test_local_cancel(event_loop: PyObject) -> PyResult<()> { let locals = Python::with_gil(|py| -> PyResult { - Ok(TaskLocals::new(event_loop.into_bound(py)).copy_context(py)?) + TaskLocals::new(event_loop.into_bound(py)).copy_context(py) })?; tokio::task::LocalSet::new().block_on( @@ -215,8 +213,8 @@ fn test_local_cancel(event_loop: PyObject) -> PyResult<()> { .await { Python::with_gil(|py| -> PyResult<()> { - assert!(e.value_bound(py).is_instance( - py.import_bound("asyncio")? + assert!(e.value(py).is_instance( + py.import("asyncio")? .getattr("CancelledError")? .downcast::() .unwrap() @@ -275,13 +273,13 @@ fn test_multiple_asyncio_run() -> PyResult<()> { })?; let d = [ - ("asyncio", py.import_bound("asyncio")?.into()), + ("asyncio", py.import("asyncio")?.into()), ("test_mod", wrap_pymodule!(test_mod)(py)), ] - .into_py_dict_bound(py); + .into_py_dict(py)?; - py.run_bound(TEST_CODE, Some(&d), None)?; - py.run_bound(TEST_CODE, Some(&d), None)?; + py.run(&CString::new(TEST_CODE).unwrap(), Some(&d), None)?; + py.run(&CString::new(TEST_CODE).unwrap(), Some(&d), None)?; Ok(()) }) } @@ -320,18 +318,18 @@ async def gen(): #[pyo3_async_runtimes::tokio::test] async fn test_async_gen_v1() -> PyResult<()> { let stream = Python::with_gil(|py| { - let test_mod = PyModule::from_code_bound( + let test_mod = PyModule::from_code( py, - TOKIO_TEST_MOD, - "test_rust_coroutine/tokio_test_mod.py", - "tokio_test_mod", + &CString::new(TOKIO_TEST_MOD).unwrap(), + &CString::new("test_rust_coroutine/tokio_test_mod.py").unwrap(), + &CString::new("tokio_test_mod").unwrap(), )?; pyo3_async_runtimes::tokio::into_stream_v1(test_mod.call_method0("gen")?) })?; let vals = stream - .map(|item| Python::with_gil(|py| -> PyResult { Ok(item?.bind(py).extract()?) })) + .map(|item| Python::with_gil(|py| -> PyResult { item?.bind(py).extract() })) .try_collect::>() .await?; @@ -344,18 +342,18 @@ async fn test_async_gen_v1() -> PyResult<()> { #[pyo3_async_runtimes::tokio::test] async fn test_async_gen_v2() -> PyResult<()> { let stream = Python::with_gil(|py| { - let test_mod = PyModule::from_code_bound( + let test_mod = PyModule::from_code( py, - TOKIO_TEST_MOD, - "test_rust_coroutine/tokio_test_mod.py", - "tokio_test_mod", + &CString::new(TOKIO_TEST_MOD).unwrap(), + &CString::new("test_rust_coroutine/tokio_test_mod.py").unwrap(), + &CString::new("tokio_test_mod").unwrap(), )?; pyo3_async_runtimes::tokio::into_stream_v2(test_mod.call_method0("gen")?) })?; let vals = stream - .map(|item| Python::with_gil(|py| -> PyResult { Ok(item.bind(py).extract()?) })) + .map(|item| Python::with_gil(|py| -> PyResult { item.bind(py).extract() })) .try_collect::>() .await?; @@ -381,14 +379,14 @@ asyncio.run(main()) fn test_contextvars() -> PyResult<()> { Python::with_gil(|py| { let d = [ - ("asyncio", py.import_bound("asyncio")?.into()), - ("contextvars", py.import_bound("contextvars")?.into()), + ("asyncio", py.import("asyncio")?.into()), + ("contextvars", py.import("contextvars")?.into()), ("cvars_mod", wrap_pymodule!(cvars_mod)(py)), ] - .into_py_dict_bound(py); + .into_py_dict(py)?; - py.run_bound(CONTEXTVARS_CODE, Some(&d), None)?; - py.run_bound(CONTEXTVARS_CODE, Some(&d), None)?; + py.run(&CString::new(CONTEXTVARS_CODE).unwrap(), Some(&d), None)?; + py.run(&CString::new(CONTEXTVARS_CODE).unwrap(), Some(&d), None)?; Ok(()) }) } diff --git a/pytests/tokio_run_forever/mod.rs b/pytests/tokio_run_forever/mod.rs index ca4742a..c067d1f 100644 --- a/pytests/tokio_run_forever/mod.rs +++ b/pytests/tokio_run_forever/mod.rs @@ -10,7 +10,7 @@ fn dump_err(py: Python<'_>, e: PyErr) { pub(super) fn test_main() { Python::with_gil(|py| { - let asyncio = py.import_bound("asyncio")?; + let asyncio = py.import("asyncio")?; let event_loop = asyncio.call_method0("new_event_loop")?; asyncio.call_method1("set_event_loop", (&event_loop,))?; diff --git a/src/async_std.rs b/src/async_std.rs index 80b0053..34c9401 100644 --- a/src/async_std.rs +++ b/src/async_std.rs @@ -9,7 +9,7 @@ //! //! ```toml //! [dependencies.pyo3-async-runtimes] -//! version = "0.22" +//! version = "0.23" //! features = ["unstable-streams"] //! ``` @@ -176,7 +176,7 @@ pub fn get_current_locals(py: Python) -> PyResult { /// # pyo3::prepare_freethreaded_python(); /// # /// # Python::with_gil(|py| -> PyResult<()> { -/// # let event_loop = py.import_bound("asyncio")?.call_method0("new_event_loop")?; +/// # let event_loop = py.import("asyncio")?.call_method0("new_event_loop")?; /// pyo3_async_runtimes::async_std::run_until_complete(event_loop, async move { /// async_std::task::sleep(Duration::from_secs(1)).await; /// Ok(()) @@ -279,7 +279,7 @@ pub fn future_into_py_with_locals( ) -> PyResult> where F: Future> + Send + 'static, - T: IntoPy, + T: for<'py> IntoPyObject<'py>, { generic::future_into_py_with_locals::(py, locals, fut) } @@ -325,7 +325,7 @@ where pub fn future_into_py(py: Python, fut: F) -> PyResult> where F: Future> + Send + 'static, - T: IntoPy, + T: for<'py> IntoPyObject<'py>, { generic::future_into_py::(py, fut) } @@ -400,7 +400,7 @@ pub fn local_future_into_py_with_locals( ) -> PyResult> where F: Future> + 'static, - T: IntoPy, + T: for<'py> IntoPyObject<'py>, { generic::local_future_into_py_with_locals::(py, locals, fut) } @@ -466,7 +466,7 @@ where pub fn local_future_into_py(py: Python, fut: F) -> PyResult> where F: Future> + 'static, - T: IntoPy, + T: for<'py> IntoPyObject<'py>, { generic::local_future_into_py::(py, fut) } @@ -485,6 +485,7 @@ where /// /// ``` /// use std::time::Duration; +/// use std::ffi::CString; /// /// use pyo3::prelude::*; /// @@ -498,11 +499,11 @@ where /// async fn py_sleep(seconds: f32) -> PyResult<()> { /// let test_mod = Python::with_gil(|py| -> PyResult { /// Ok( -/// PyModule::from_code_bound( +/// PyModule::from_code( /// py, -/// PYTHON_CODE, -/// "test_into_future/test_mod.py", -/// "test_mod" +/// &CString::new(PYTHON_CODE).unwrap(), +/// &CString::new("test_into_future/test_mod.py").unwrap(), +/// &CString::new("test_mod").unwrap() /// )? /// .into() /// ) @@ -511,7 +512,7 @@ where /// Python::with_gil(|py| { /// pyo3_async_runtimes::async_std::into_future( /// test_mod -/// .call_method1(py, "py_sleep", (seconds.into_py(py),))? +/// .call_method1(py, "py_sleep", (seconds.into_pyobject(py).unwrap(),))? /// .into_bound(py), /// ) /// })? @@ -538,6 +539,7 @@ pub fn into_future( /// ``` /// use pyo3::prelude::*; /// use futures::{StreamExt, TryStreamExt}; +/// use std::ffi::CString; /// /// const TEST_MOD: &str = r#" /// import asyncio @@ -552,11 +554,11 @@ pub fn into_future( /// # #[pyo3_async_runtimes::async_std::main] /// # async fn main() -> PyResult<()> { /// let stream = Python::with_gil(|py| { -/// let test_mod = PyModule::from_code_bound( +/// let test_mod = PyModule::from_code( /// py, -/// TEST_MOD, -/// "test_rust_coroutine/test_mod.py", -/// "test_mod", +/// &CString::new(TEST_MOD).unwrap(), +/// &CString::new("test_rust_coroutine/test_mod.py").unwrap(), +/// &CString::new("test_mod").unwrap(), /// )?; /// /// pyo3_async_runtimes::async_std::into_stream_v1(test_mod.call_method0("gen")?) @@ -595,6 +597,7 @@ pub fn into_stream_v1( /// ``` /// use pyo3::prelude::*; /// use futures::{StreamExt, TryStreamExt}; +/// use std::ffi::CString; /// /// const TEST_MOD: &str = r#" /// import asyncio @@ -609,11 +612,11 @@ pub fn into_stream_v1( /// # #[pyo3_async_runtimes::async_std::main] /// # async fn main() -> PyResult<()> { /// let stream = Python::with_gil(|py| { -/// let test_mod = PyModule::from_code_bound( +/// let test_mod = PyModule::from_code( /// py, -/// TEST_MOD, -/// "test_rust_coroutine/test_mod.py", -/// "test_mod", +/// &CString::new(TEST_MOD).unwrap(), +/// &CString::new("test_rust_coroutine/test_mod.py").unwrap(), +/// &CString::new("test_mod").unwrap(), /// )?; /// /// pyo3_async_runtimes::async_std::into_stream_with_locals_v1( @@ -656,6 +659,7 @@ pub fn into_stream_with_locals_v1( /// ``` /// use pyo3::prelude::*; /// use futures::{StreamExt, TryStreamExt}; +/// use std::ffi::CString; /// /// const TEST_MOD: &str = r#" /// import asyncio @@ -670,11 +674,11 @@ pub fn into_stream_with_locals_v1( /// # #[pyo3_async_runtimes::async_std::main] /// # async fn main() -> PyResult<()> { /// let stream = Python::with_gil(|py| { -/// let test_mod = PyModule::from_code_bound( +/// let test_mod = PyModule::from_code( /// py, -/// TEST_MOD, -/// "test_rust_coroutine/test_mod.py", -/// "test_mod", +/// &CString::new(TEST_MOD).unwrap(), +/// &CString::new("test_rust_coroutine/test_mod.py").unwrap(), +/// &CString::new("test_mod").unwrap(), /// )?; /// /// pyo3_async_runtimes::async_std::into_stream_with_locals_v2( @@ -716,6 +720,7 @@ pub fn into_stream_with_locals_v2( /// ``` /// use pyo3::prelude::*; /// use futures::{StreamExt, TryStreamExt}; +/// use std::ffi::CString; /// /// const TEST_MOD: &str = r#" /// import asyncio @@ -730,11 +735,11 @@ pub fn into_stream_with_locals_v2( /// # #[pyo3_async_runtimes::async_std::main] /// # async fn main() -> PyResult<()> { /// let stream = Python::with_gil(|py| { -/// let test_mod = PyModule::from_code_bound( +/// let test_mod = PyModule::from_code( /// py, -/// TEST_MOD, -/// "test_rust_coroutine/test_mod.py", -/// "test_mod", +/// &CString::new(TEST_MOD).unwrap(), +/// &CString::new("test_rust_coroutine/test_mod.py").unwrap(), +/// &CString::new("test_mod").unwrap(), /// )?; /// /// pyo3_async_runtimes::async_std::into_stream_v2(test_mod.call_method0("gen")?) diff --git a/src/generic.rs b/src/generic.rs index 759e3f5..3f96f08 100644 --- a/src/generic.rs +++ b/src/generic.rs @@ -9,7 +9,7 @@ //! //! ```toml //! [dependencies.pyo3-async-runtimes] -//! version = "0.22" +//! version = "0.23" //! features = ["unstable-streams"] //! ``` @@ -31,6 +31,7 @@ use futures::{channel::mpsc, SinkExt}; use once_cell::sync::OnceCell; use pin_project_lite::pin_project; use pyo3::prelude::*; +use pyo3::BoundObject; #[cfg(feature = "unstable-streams")] use std::marker::PhantomData; @@ -181,7 +182,7 @@ where /// # use pyo3::prelude::*; /// # /// # Python::with_gil(|py| -> PyResult<()> { -/// # let event_loop = py.import_bound("asyncio")?.call_method0("new_event_loop")?; +/// # let event_loop = py.import("asyncio")?.call_method0("new_event_loop")?; /// # #[cfg(feature = "tokio-runtime")] /// pyo3_async_runtimes::generic::run_until_complete::(&event_loop, async move { /// tokio::time::sleep(Duration::from_secs(1)).await; @@ -347,8 +348,11 @@ fn set_result( let none = py.None().into_bound(py); let (complete, val) = match result { - Ok(val) => (future.getattr("set_result")?, val.into_py(py)), - Err(err) => (future.getattr("set_exception")?, err.into_py(py)), + Ok(val) => (future.getattr("set_result")?, val.into_pyobject(py)?), + Err(err) => ( + future.getattr("set_exception")?, + err.into_pyobject(py)?.into_any(), + ), }; call_soon_threadsafe(event_loop, &none, (CheckedCompletor, future, complete, val))?; @@ -368,6 +372,7 @@ fn set_result( /// /// ```no_run /// # use std::{any::Any, pin::Pin, future::Future, task::{Context, Poll}, time::Duration}; +/// # use std::ffi::CString; /// # /// # use pyo3::prelude::*; /// # @@ -439,11 +444,11 @@ fn set_result( /// async fn py_sleep(seconds: f32) -> PyResult<()> { /// let test_mod = Python::with_gil(|py| -> PyResult { /// Ok( -/// PyModule::from_code_bound( +/// PyModule::from_code( /// py, -/// PYTHON_CODE, -/// "test_into_future/test_mod.py", -/// "test_mod" +/// &CString::new(PYTHON_CODE).unwrap(), +/// &CString::new("test_into_future/test_mod.py").unwrap(), +/// &CString::new("test_mod").unwrap(), /// )? /// .into() /// ) @@ -452,7 +457,7 @@ fn set_result( /// Python::with_gil(|py| { /// pyo3_async_runtimes::generic::into_future::( /// test_mod -/// .call_method1(py, "py_sleep", (seconds.into_py(py),))? +/// .call_method1(py, "py_sleep", (seconds.into_pyobject(py).unwrap(),))? /// .into_bound(py), /// ) /// })? @@ -581,7 +586,7 @@ pub fn future_into_py_with_locals( where R: Runtime + ContextExt, F: Future> + Send + 'static, - T: IntoPy, + T: for<'py> IntoPyObject<'py>, { let (cancel_tx, cancel_rx) = oneshot::channel(); @@ -617,7 +622,10 @@ where let _ = set_result( &locals2.event_loop(py), future_tx1.bind(py), - result.map(|val| val.into_py(py)), + result.and_then(|val| match val.into_pyobject(py) { + Ok(obj) => Ok(obj.into_any().unbind()), + Err(err) => Err(err.into()), + }), ) .map_err(dump_err(py)); }); @@ -686,10 +694,10 @@ impl Cancellable { } } -impl Future for Cancellable +impl<'py, F, T> Future for Cancellable where F: Future>, - T: IntoPy, + T: IntoPyObject<'py>, { type Output = F::Output; @@ -844,7 +852,7 @@ pub fn future_into_py(py: Python, fut: F) -> PyResult> where R: Runtime + ContextExt, F: Future> + Send + 'static, - T: IntoPy, + T: for<'py> IntoPyObject<'py>, { future_into_py_with_locals::(py, get_current_locals::(py)?, fut) } @@ -986,7 +994,7 @@ pub fn local_future_into_py_with_locals( where R: Runtime + SpawnLocalExt + LocalContextExt, F: Future> + 'static, - T: IntoPy, + T: for<'py> IntoPyObject<'py>, { let (cancel_tx, cancel_rx) = oneshot::channel(); @@ -1022,7 +1030,10 @@ where let _ = set_result( locals2.event_loop.bind(py), future_tx1.bind(py), - result.map(|val| val.into_py(py)), + result.and_then(|val| match val.into_pyobject(py) { + Ok(obj) => Ok(obj.into_any().unbind()), + Err(err) => Err(err.into()), + }), ) .map_err(dump_err(py)); }); @@ -1183,7 +1194,7 @@ pub fn local_future_into_py(py: Python, fut: F) -> PyResult> + 'static, - T: IntoPy, + T: for<'py> IntoPyObject<'py>, { local_future_into_py_with_locals::(py, get_current_locals::(py)?, fut) } @@ -1256,6 +1267,7 @@ where /// /// use pyo3::prelude::*; /// use futures::{StreamExt, TryStreamExt}; +/// use std::ffi::CString; /// /// const TEST_MOD: &str = r#" /// import asyncio @@ -1268,11 +1280,11 @@ where /// /// # async fn test_async_gen() -> PyResult<()> { /// let stream = Python::with_gil(|py| { -/// let test_mod = PyModule::from_code_bound( +/// let test_mod = PyModule::from_code( /// py, -/// TEST_MOD, -/// "test_rust_coroutine/test_mod.py", -/// "test_mod", +/// &CString::new(TEST_MOD).unwrap(), +/// &CString::new("test_rust_coroutine/test_mod.py").unwrap(), +/// &CString::new("test_mod").unwrap(), /// )?; /// /// pyo3_async_runtimes::generic::into_stream_with_locals_v1::( @@ -1404,6 +1416,7 @@ where /// /// use pyo3::prelude::*; /// use futures::{StreamExt, TryStreamExt}; +/// use std::ffi::CString; /// /// const TEST_MOD: &str = r#" /// import asyncio @@ -1416,11 +1429,11 @@ where /// /// # async fn test_async_gen() -> PyResult<()> { /// let stream = Python::with_gil(|py| { -/// let test_mod = PyModule::from_code_bound( +/// let test_mod = PyModule::from_code( /// py, -/// TEST_MOD, -/// "test_rust_coroutine/test_mod.py", -/// "test_mod", +/// &CString::new(TEST_MOD).unwrap(), +/// &CString::new("test_rust_coroutine/test_mod.py").unwrap(), +/// &CString::new("test_mod").unwrap(), /// )?; /// /// pyo3_async_runtimes::generic::into_stream_v1::(test_mod.call_method0("gen")?) @@ -1467,7 +1480,7 @@ where { fn send(&mut self, py: Python, locals: TaskLocals, item: PyObject) -> PyResult { match self.tx.try_send(item.clone_ref(py)) { - Ok(_) => Ok(true.into_py(py)), + Ok(_) => Ok(true.into_pyobject(py)?.into_any().unbind()), Err(e) => { if e.is_full() { let mut tx = self.tx.clone(); @@ -1476,19 +1489,25 @@ where future_into_py_with_locals::(py, locals, async move { if tx.flush().await.is_err() { // receiving side disconnected - return Python::with_gil(|py| Ok(false.into_py(py))); + return Python::with_gil(|py| { + Ok(false.into_pyobject(py)?.into_any().unbind()) + }); } if tx.send(item).await.is_err() { // receiving side disconnected - return Python::with_gil(|py| Ok(false.into_py(py))); + return Python::with_gil(|py| { + Ok(false.into_pyobject(py)?.into_any().unbind()) + }); } - Python::with_gil(|py| Ok(true.into_py(py))) + Python::with_gil(|py| { + Ok(true.into_pyobject(py)?.into_any().unbind()) + }) })? .into(), ) }) } else { - Ok(false.into_py(py)) + Ok(false.into_pyobject(py)?.into_any().unbind()) } } } @@ -1502,15 +1521,20 @@ where #[pyclass] struct SenderGlue { locals: TaskLocals, - tx: Box, + tx: Arc>, } #[pymethods] impl SenderGlue { pub fn send(&mut self, item: PyObject) -> PyResult { - Python::with_gil(|py| self.tx.send(py, self.locals.clone_ref(py), item)) + Python::with_gil(|py| { + self.tx + .lock() + .unwrap() + .send(py, self.locals.clone_ref(py), item) + }) } pub fn close(&mut self) -> PyResult<()> { - self.tx.close() + self.tx.lock().unwrap().close() } } @@ -1601,6 +1625,7 @@ async def forward(gen, sender): /// /// use pyo3::prelude::*; /// use futures::{StreamExt, TryStreamExt}; +/// use std::ffi::CString; /// /// const TEST_MOD: &str = r#" /// import asyncio @@ -1613,11 +1638,11 @@ async def forward(gen, sender): /// /// # async fn test_async_gen() -> PyResult<()> { /// let stream = Python::with_gil(|py| { -/// let test_mod = PyModule::from_code_bound( +/// let test_mod = PyModule::from_code( /// py, -/// TEST_MOD, -/// "test_rust_coroutine/test_mod.py", -/// "test_mod", +/// &CString::new(TEST_MOD).unwrap(), +/// &CString::new("test_rust_coroutine/test_mod.py").unwrap(), +/// &CString::new("test_mod").unwrap(), /// )?; /// /// pyo3_async_runtimes::generic::into_stream_with_locals_v2::( @@ -1644,15 +1669,17 @@ pub fn into_stream_with_locals_v2( where R: Runtime + ContextExt, { + use std::ffi::CString; + static GLUE_MOD: OnceCell = OnceCell::new(); let py = gen.py(); let glue = GLUE_MOD .get_or_try_init(|| -> PyResult { - Ok(PyModule::from_code_bound( + Ok(PyModule::from_code( py, - STREAM_GLUE, - "pyo3_async_runtimes/pyo3_async_runtimes_glue.py", - "pyo3_async_runtimes_glue", + &CString::new(STREAM_GLUE).unwrap(), + &CString::new("pyo3_async_runtimes/pyo3_async_runtimes_glue.py").unwrap(), + &CString::new("pyo3_async_runtimes_glue").unwrap(), )? .into()) })? @@ -1670,10 +1697,10 @@ where gen, SenderGlue { locals, - tx: Box::new(GenericSender { + tx: Arc::new(Mutex::new(GenericSender { runtime: PhantomData::, tx, - }), + })), }, ), )?, @@ -1749,6 +1776,7 @@ where /// /// use pyo3::prelude::*; /// use futures::{StreamExt, TryStreamExt}; +/// use std::ffi::CString; /// /// const TEST_MOD: &str = r#" /// import asyncio @@ -1761,11 +1789,11 @@ where /// /// # async fn test_async_gen() -> PyResult<()> { /// let stream = Python::with_gil(|py| { -/// let test_mod = PyModule::from_code_bound( +/// let test_mod = PyModule::from_code( /// py, -/// TEST_MOD, -/// "test_rust_coroutine/test_mod.py", -/// "test_mod", +/// &CString::new(TEST_MOD).unwrap(), +/// &CString::new("test_rust_coroutine/test_mod.py").unwrap(), +/// &CString::new("test_mod").unwrap(), /// )?; /// /// pyo3_async_runtimes::generic::into_stream_v2::(test_mod.call_method0("gen")?) diff --git a/src/lib.rs b/src/lib.rs index d399297..480e8a4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -100,7 +100,7 @@ //! // event loop from earlier. //! pyo3_async_runtimes::into_future_with_locals( //! &locals, -//! py.import_bound("asyncio")?.call_method1("sleep", (1,))? +//! py.import("asyncio")?.call_method1("sleep", (1,))? //! ) //! })?; //! @@ -169,7 +169,7 @@ //! pyo3_async_runtimes::into_future_with_locals( //! // Now we can get the current locals through task-local data //! &pyo3_async_runtimes::tokio::get_current_locals(py)?, -//! py.import_bound("asyncio")?.call_method1("sleep", (1,))? +//! py.import("asyncio")?.call_method1("sleep", (1,))? //! ) //! })?; //! @@ -244,7 +244,7 @@ //! pyo3_async_runtimes::tokio::future_into_py(py, async move { //! let py_sleep = Python::with_gil(|py| { //! pyo3_async_runtimes::tokio::into_future( -//! py.import_bound("asyncio")?.call_method1("sleep", (1,))? +//! py.import("asyncio")?.call_method1("sleep", (1,))? //! ) //! })?; //! @@ -300,7 +300,7 @@ //! //! ```toml //! [dependencies.pyo3-async-runtimes] -//! version = "0.22" +//! version = "0.23" //! features = ["attributes"] //! ``` //! @@ -313,7 +313,7 @@ //! //! ```toml //! [dependencies.pyo3-async-runtimes] -//! version = "0.22" +//! version = "0.23" //! features = ["async-std-runtime"] //! ``` //! @@ -326,7 +326,7 @@ //! //! ```toml //! [dependencies.pyo3-async-runtimes] -//! version = "0.22" +//! version = "0.23" //! features = ["tokio-runtime"] //! ``` //! @@ -339,7 +339,7 @@ //! //! ```toml //! [dependencies.pyo3-async-runtimes] -//! version = "0.22" +//! version = "0.23" //! features = ["testing"] //! ``` @@ -364,7 +364,7 @@ pub mod generic; #[pymodule] fn pyo3_async_runtimes(py: Python, m: &Bound) -> PyResult<()> { - m.add("RustPanic", py.get_type_bound::())?; + m.add("RustPanic", py.get_type::())?; Ok(()) } @@ -442,7 +442,7 @@ fn close(event_loop: Bound) -> PyResult<()> { fn asyncio(py: Python) -> PyResult<&Bound> { ASYNCIO - .get_or_try_init(|| Ok(py.import_bound("asyncio")?.into())) + .get_or_try_init(|| Ok(py.import("asyncio")?.into())) .map(|asyncio| asyncio.bind(py)) } @@ -464,7 +464,7 @@ pub fn get_running_loop(py: Python) -> PyResult> { fn contextvars(py: Python) -> PyResult<&Bound> { Ok(CONTEXTVARS - .get_or_try_init(|| py.import_bound("contextvars").map(|m| m.into()))? + .get_or_try_init(|| py.import("contextvars").map(|m| m.into()))? .bind(py)) } @@ -576,14 +576,14 @@ impl PyEnsureFuture { } } -fn call_soon_threadsafe( - event_loop: &Bound, +fn call_soon_threadsafe<'py>( + event_loop: &Bound<'py, PyAny>, context: &Bound, - args: impl IntoPy>, + args: impl IntoPyObject<'py, Target = PyTuple>, ) -> PyResult<()> { let py = event_loop.py(); - let kwargs = PyDict::new_bound(py); + let kwargs = PyDict::new(py); kwargs.set_item("context", context)?; event_loop.call_method("call_soon_threadsafe", args, Some(&kwargs))?; @@ -605,6 +605,7 @@ fn call_soon_threadsafe( /// /// ``` /// use std::time::Duration; +/// use std::ffi::CString; /// /// use pyo3::prelude::*; /// @@ -619,11 +620,11 @@ fn call_soon_threadsafe( /// async fn py_sleep(seconds: f32) -> PyResult<()> { /// let test_mod = Python::with_gil(|py| -> PyResult { /// Ok( -/// PyModule::from_code_bound( +/// PyModule::from_code( /// py, -/// PYTHON_CODE, -/// "test_into_future/test_mod.py", -/// "test_mod" +/// &CString::new(PYTHON_CODE).unwrap(), +/// &CString::new("test_into_future/test_mod.py").unwrap(), +/// &CString::new("test_mod").unwrap(), /// )? /// .into() /// ) @@ -633,7 +634,7 @@ fn call_soon_threadsafe( /// pyo3_async_runtimes::into_future_with_locals( /// &pyo3_async_runtimes::tokio::get_current_locals(py)?, /// test_mod -/// .call_method1(py, "py_sleep", (seconds.into_py(py),))? +/// .call_method1(py, "py_sleep", (seconds.into_pyobject(py).unwrap(),))? /// .into_bound(py), /// ) /// })? @@ -661,7 +662,7 @@ pub fn into_future_with_locals( match rx.await { Ok(item) => item, Err(_) => Python::with_gil(|py| { - Err(PyErr::from_value_bound( + Err(PyErr::from_value( asyncio(py)?.call_method0("CancelledError")?, )) }), diff --git a/src/testing.rs b/src/testing.rs index 90e90eb..8bfc55f 100644 --- a/src/testing.rs +++ b/src/testing.rs @@ -64,7 +64,7 @@ //! Also add the `testing` and `attributes` features to the `pyo3-async-runtimes` dependency and select your preferred runtime: //! //! ```toml -//! pyo3-async-runtimes = { version = "0.22", features = ["testing", "attributes", "async-std-runtime"] } +//! pyo3-async-runtimes = { version = "0.23", features = ["testing", "attributes", "async-std-runtime"] } //! ``` //! //! At this point, you should be able to run the test via `cargo test` diff --git a/src/tokio.rs b/src/tokio.rs index f435ea5..47e6da0 100644 --- a/src/tokio.rs +++ b/src/tokio.rs @@ -9,7 +9,7 @@ //! //! ```toml //! [dependencies.pyo3-async-runtimes] -//! version = "0.22" +//! version = "0.23" //! features = ["unstable-streams"] //! ``` @@ -222,7 +222,7 @@ fn multi_thread() -> Builder { /// # /// # pyo3::prepare_freethreaded_python(); /// # Python::with_gil(|py| -> PyResult<()> { -/// # let event_loop = py.import_bound("asyncio")?.call_method0("new_event_loop")?; +/// # let event_loop = py.import("asyncio")?.call_method0("new_event_loop")?; /// pyo3_async_runtimes::tokio::run_until_complete(event_loop, async move { /// tokio::time::sleep(Duration::from_secs(1)).await; /// Ok(()) @@ -322,7 +322,7 @@ pub fn future_into_py_with_locals( ) -> PyResult> where F: Future> + Send + 'static, - T: IntoPy, + T: for<'py> IntoPyObject<'py>, { generic::future_into_py_with_locals::(py, locals, fut) } @@ -368,7 +368,7 @@ where pub fn future_into_py(py: Python, fut: F) -> PyResult> where F: Future> + Send + 'static, - T: IntoPy, + T: for<'py> IntoPyObject<'py>, { generic::future_into_py::(py, fut) } @@ -459,7 +459,7 @@ pub fn local_future_into_py_with_locals( ) -> PyResult> where F: Future> + 'static, - T: IntoPy, + T: for<'py> IntoPyObject<'py>, { generic::local_future_into_py_with_locals::(py, locals, fut) } @@ -540,7 +540,7 @@ where pub fn local_future_into_py(py: Python, fut: F) -> PyResult> where F: Future> + 'static, - T: IntoPy, + T: for<'py> IntoPyObject<'py>, { generic::local_future_into_py::(py, fut) } @@ -559,6 +559,7 @@ where /// /// ``` /// use std::time::Duration; +/// use std::ffi::CString; /// /// use pyo3::prelude::*; /// @@ -572,11 +573,11 @@ where /// async fn py_sleep(seconds: f32) -> PyResult<()> { /// let test_mod = Python::with_gil(|py| -> PyResult { /// Ok( -/// PyModule::from_code_bound( +/// PyModule::from_code( /// py, -/// PYTHON_CODE, -/// "test_into_future/test_mod.py", -/// "test_mod" +/// &CString::new(PYTHON_CODE).unwrap(), +/// &CString::new("test_into_future/test_mod.py").unwrap(), +/// &CString::new("test_mod").unwrap(), /// )? /// .into() /// ) @@ -585,7 +586,7 @@ where /// Python::with_gil(|py| { /// pyo3_async_runtimes::tokio::into_future( /// test_mod -/// .call_method1(py, "py_sleep", (seconds.into_py(py),))? +/// .call_method1(py, "py_sleep", (seconds.into_pyobject(py).unwrap(),))? /// .into_bound(py), /// ) /// })? @@ -613,6 +614,7 @@ pub fn into_future( /// ``` /// use pyo3::prelude::*; /// use futures::{StreamExt, TryStreamExt}; +/// use std::ffi::CString; /// /// const TEST_MOD: &str = r#" /// import asyncio @@ -627,11 +629,11 @@ pub fn into_future( /// # #[pyo3_async_runtimes::tokio::main] /// # async fn main() -> PyResult<()> { /// let stream = Python::with_gil(|py| { -/// let test_mod = PyModule::from_code_bound( +/// let test_mod = PyModule::from_code( /// py, -/// TEST_MOD, -/// "test_rust_coroutine/test_mod.py", -/// "test_mod", +/// &CString::new(TEST_MOD).unwrap(), +/// &CString::new("test_rust_coroutine/test_mod.py").unwrap(), +/// &CString::new("test_mod").unwrap(), /// )?; /// /// pyo3_async_runtimes::tokio::into_stream_with_locals_v1( @@ -673,6 +675,7 @@ pub fn into_stream_with_locals_v1( /// ``` /// use pyo3::prelude::*; /// use futures::{StreamExt, TryStreamExt}; +/// use std::ffi::CString; /// /// const TEST_MOD: &str = r#" /// import asyncio @@ -687,11 +690,11 @@ pub fn into_stream_with_locals_v1( /// # #[pyo3_async_runtimes::tokio::main] /// # async fn main() -> PyResult<()> { /// let stream = Python::with_gil(|py| { -/// let test_mod = PyModule::from_code_bound( +/// let test_mod = PyModule::from_code( /// py, -/// TEST_MOD, -/// "test_rust_coroutine/test_mod.py", -/// "test_mod", +/// &CString::new(TEST_MOD).unwrap(), +/// &CString::new("test_rust_coroutine/test_mod.py").unwrap(), +/// &CString::new("test_mod").unwrap(), /// )?; /// /// pyo3_async_runtimes::tokio::into_stream_v1(test_mod.call_method0("gen")?) @@ -730,6 +733,7 @@ pub fn into_stream_v1( /// ``` /// use pyo3::prelude::*; /// use futures::{StreamExt, TryStreamExt}; +/// use std::ffi::CString; /// /// const TEST_MOD: &str = r#" /// import asyncio @@ -744,11 +748,11 @@ pub fn into_stream_v1( /// # #[pyo3_async_runtimes::tokio::main] /// # async fn main() -> PyResult<()> { /// let stream = Python::with_gil(|py| { -/// let test_mod = PyModule::from_code_bound( +/// let test_mod = PyModule::from_code( /// py, -/// TEST_MOD, -/// "test_rust_coroutine/test_mod.py", -/// "test_mod", +/// &CString::new(TEST_MOD).unwrap(), +/// &CString::new("test_rust_coroutine/test_mod.py").unwrap(), +/// &CString::new("test_mod").unwrap(), /// )?; /// /// pyo3_async_runtimes::tokio::into_stream_with_locals_v2( @@ -790,6 +794,7 @@ pub fn into_stream_with_locals_v2( /// ``` /// use pyo3::prelude::*; /// use futures::{StreamExt, TryStreamExt}; +/// use std::ffi::CString; /// /// const TEST_MOD: &str = r#" /// import asyncio @@ -804,11 +809,11 @@ pub fn into_stream_with_locals_v2( /// # #[pyo3_async_runtimes::tokio::main] /// # async fn main() -> PyResult<()> { /// let stream = Python::with_gil(|py| { -/// let test_mod = PyModule::from_code_bound( +/// let test_mod = PyModule::from_code( /// py, -/// TEST_MOD, -/// "test_rust_coroutine/test_mod.py", -/// "test_mod", +/// &CString::new(TEST_MOD).unwrap(), +/// &CString::new("test_rust_coroutine/test_mod.py").unwrap(), +/// &CString::new("test_mod").unwrap(), /// )?; /// /// pyo3_async_runtimes::tokio::into_stream_v2(test_mod.call_method0("gen")?)