Skip to content

Commit ab8ad9a

Browse files
committed
Upgrade to pyo3 0.21
Fix compilation errors Migrate off of deprecated methods Ignore must use false-positive
1 parent 398ed30 commit ab8ad9a

19 files changed

+292
-264
lines changed

Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "pyo3-asyncio"
33
description = "PyO3 utilities for Python's Asyncio library"
4-
version = "0.20.0"
4+
version = "0.21.0"
55
authors = ["Andrew J Westlake <[email protected]>"]
66
readme = "README.md"
77
keywords = ["pyo3", "python", "ffi", "async", "asyncio"]
@@ -116,11 +116,11 @@ futures = "0.3"
116116
inventory = { version = "0.3", optional = true }
117117
once_cell = "1.14"
118118
pin-project-lite = "0.2"
119-
pyo3 = "0.20"
119+
pyo3 = "0.21"
120120
pyo3-asyncio-macros = { path = "pyo3-asyncio-macros", version = "=0.20.0", optional = true }
121121

122122
[dev-dependencies]
123-
pyo3 = { version = "0.20", features = ["macros"] }
123+
pyo3 = { version = "0.21", features = ["macros"] }
124124

125125
[dependencies.async-std]
126126
version = "1.12"

README.md

+16-16
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ use pyo3::prelude::*;
6565
#[pyo3_asyncio::async_std::main]
6666
async fn main() -> PyResult<()> {
6767
let fut = Python::with_gil(|py| {
68-
let asyncio = py.import("asyncio")?;
68+
let asyncio = py.import_bound("asyncio")?;
6969
// convert asyncio.sleep into a Rust Future
7070
pyo3_asyncio::async_std::into_future(asyncio.call_method1("sleep", (1.into_py(py),))?)
7171
})?;
@@ -95,7 +95,7 @@ use pyo3::prelude::*;
9595
#[pyo3_asyncio::tokio::main]
9696
async fn main() -> PyResult<()> {
9797
let fut = Python::with_gil(|py| {
98-
let asyncio = py.import("asyncio")?;
98+
let asyncio = py.import_bound("asyncio")?;
9999
// convert asyncio.sleep into a Rust Future
100100
pyo3_asyncio::tokio::into_future(asyncio.call_method1("sleep", (1.into_py(py),))?)
101101
})?;
@@ -149,7 +149,7 @@ Export an async function that makes use of `async-std`:
149149
use pyo3::{prelude::*, wrap_pyfunction};
150150

151151
#[pyfunction]
152-
fn rust_sleep(py: Python) -> PyResult<&PyAny> {
152+
fn rust_sleep(py: Python) -> PyResult<Bound<PyAny>> {
153153
pyo3_asyncio::async_std::future_into_py(py, async {
154154
async_std::task::sleep(std::time::Duration::from_secs(1)).await;
155155
Ok(())
@@ -173,7 +173,7 @@ If you want to use `tokio` instead, here's what your module should look like:
173173
use pyo3::{prelude::*, wrap_pyfunction};
174174

175175
#[pyfunction]
176-
fn rust_sleep(py: Python) -> PyResult<&PyAny> {
176+
fn rust_sleep(py: Python) -> PyResult<Bound<PyAny>> {
177177
pyo3_asyncio::tokio::future_into_py(py, async {
178178
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
179179
Ok(())
@@ -237,7 +237,7 @@ use pyo3::prelude::*;
237237
async fn main() -> PyResult<()> {
238238
let future = Python::with_gil(|py| -> PyResult<_> {
239239
// import the module containing the py_sleep function
240-
let example = py.import("example")?;
240+
let example = py.import_bound("example")?;
241241

242242
// calling the py_sleep method like a normal function
243243
// returns a coroutine
@@ -289,7 +289,7 @@ async fn rust_sleep() {
289289
}
290290

291291
#[pyfunction]
292-
fn call_rust_sleep(py: Python) -> PyResult<&PyAny> {
292+
fn call_rust_sleep(py: Python) -> PyResult<Bound<PyAny>> {
293293
pyo3_asyncio::async_std::future_into_py(py, async move {
294294
rust_sleep().await;
295295
Ok(())
@@ -356,7 +356,7 @@ async fn main() -> PyResult<()> {
356356
// PyO3 is initialized - Ready to go
357357
358358
let fut = Python::with_gil(|py| -> PyResult<_> {
359-
let asyncio = py.import("asyncio")?;
359+
let asyncio = py.import_bound("asyncio")?;
360360
361361
// convert asyncio.sleep into a Rust Future
362362
pyo3_asyncio::async_std::into_future(
@@ -443,7 +443,7 @@ tokio = "1.9"
443443
use pyo3::{prelude::*, wrap_pyfunction};
444444

445445
#[pyfunction]
446-
fn rust_sleep(py: Python) -> PyResult<&PyAny> {
446+
fn rust_sleep(py: Python) -> PyResult<Bound<PyAny>> {
447447
pyo3_asyncio::tokio::future_into_py(py, async {
448448
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
449449
Ok(())
@@ -504,7 +504,7 @@ fn main() -> PyResult<()> {
504504
pyo3::prepare_freethreaded_python();
505505

506506
Python::with_gil(|py| {
507-
let uvloop = py.import("uvloop")?;
507+
let uvloop = py.import_bound("uvloop")?;
508508
uvloop.call_method0("install")?;
509509

510510
// store a reference for the assertion
@@ -514,11 +514,11 @@ fn main() -> PyResult<()> {
514514
// verify that we are on a uvloop.Loop
515515
Python::with_gil(|py| -> PyResult<()> {
516516
assert!(uvloop
517-
.as_ref(py)
517+
.bind(py)
518518
.getattr("Loop")?
519519
.downcast::<PyType>()
520520
.unwrap()
521-
.is_instance(pyo3_asyncio::async_std::get_current_loop(py)?)?);
521+
.is_instance(&pyo3_asyncio::async_std::get_current_loop(py)?)?);
522522
Ok(())
523523
})?;
524524

@@ -601,24 +601,24 @@ To make things a bit easier, I decided to keep most of the old API alongside the
601601
pyo3::prepare_freethreaded_python();
602602

603603
Python::with_gil(|py| {
604-
let asyncio = py.import("asyncio")?;
604+
let asyncio = py.import_bound("asyncio")?;
605605

606606
let event_loop = asyncio.call_method0("new_event_loop")?;
607-
asyncio.call_method1("set_event_loop", (event_loop,))?;
607+
asyncio.call_method1("set_event_loop", (&event_loop,))?;
608608

609-
let event_loop_hdl = PyObject::from(event_loop);
609+
let event_loop_hdl = PyObject::from(event_loop.clone());
610610

611611
pyo3_asyncio::tokio::get_runtime().spawn(async move {
612612
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
613613

614614
// Stop the event loop manually
615615
Python::with_gil(|py| {
616616
event_loop_hdl
617-
.as_ref(py)
617+
.bind(py)
618618
.call_method1(
619619
"call_soon_threadsafe",
620620
(event_loop_hdl
621-
.as_ref(py)
621+
.bind(py)
622622
.getattr("stop")
623623
.unwrap(),),
624624
)

examples/async_std.rs

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

88
// convert asyncio.sleep into a Rust Future
99
pyo3_asyncio::async_std::into_future(asyncio.call_method1("sleep", (1.into_py(py),))?)

examples/tokio.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use pyo3::prelude::*;
33
#[pyo3_asyncio::tokio::main]
44
async fn main() -> PyResult<()> {
55
let fut = Python::with_gil(|py| {
6-
let asyncio = py.import("asyncio")?;
6+
let asyncio = py.import_bound("asyncio")?;
77

88
// convert asyncio.sleep into a Rust Future
99
pyo3_asyncio::tokio::into_future(asyncio.call_method1("sleep", (1.into_py(py),))?)

examples/tokio_current_thread.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use pyo3::prelude::*;
33
#[pyo3_asyncio::tokio::main(flavor = "current_thread")]
44
async fn main() -> PyResult<()> {
55
let fut = Python::with_gil(|py| {
6-
let asyncio = py.import("asyncio")?;
6+
let asyncio = py.import_bound("asyncio")?;
77

88
// convert asyncio.sleep into a Rust Future
99
pyo3_asyncio::tokio::into_future(asyncio.call_method1("sleep", (1.into_py(py),))?)

examples/tokio_multi_thread.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use pyo3::prelude::*;
33
#[pyo3_asyncio::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("asyncio")?;
6+
let asyncio = py.import_bound("asyncio")?;
77

88
// convert asyncio.sleep into a Rust Future
99
pyo3_asyncio::tokio::into_future(asyncio.call_method1("sleep", (1.into_py(py),))?)

pytests/common/mod.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ async def sleep_for_1s(sleep_for):
1616
pub(super) async fn test_into_future(event_loop: PyObject) -> PyResult<()> {
1717
let fut = Python::with_gil(|py| {
1818
let test_mod =
19-
PyModule::from_code(py, TEST_MOD, "test_rust_coroutine/test_mod.py", "test_mod")?;
19+
PyModule::from_code_bound(py, TEST_MOD, "test_rust_coroutine/test_mod.py", "test_mod")?;
2020

2121
pyo3_asyncio::into_future_with_locals(
22-
&TaskLocals::new(event_loop.as_ref(py)),
22+
&TaskLocals::new(event_loop.into_bound(py)),
2323
test_mod.call_method1("py_sleep", (1.into_py(py),))?,
2424
)
2525
})?;
@@ -36,19 +36,19 @@ pub(super) fn test_blocking_sleep() -> PyResult<()> {
3636

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

4242
// spawn a blocking sleep in the threadpool executor - returns a task, not a coroutine
43-
let task = event_loop.as_ref(py).call_method1(
43+
let task = event_loop.bind(py).call_method1(
4444
"run_in_executor",
4545
(
4646
py.None(),
4747
functools.call_method1("partial", (time.getattr("sleep")?, 1))?,
4848
),
4949
)?;
5050

51-
pyo3_asyncio::into_future_with_locals(&TaskLocals::new(event_loop.as_ref(py)), task)
51+
pyo3_asyncio::into_future_with_locals(&TaskLocals::new(event_loop.into_bound(py)), task)
5252
})?;
5353

5454
fut.await?;

0 commit comments

Comments
 (0)