Skip to content

Commit ed31a14

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 ed31a14

19 files changed

+288
-260
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

+13-13
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,7 +514,7 @@ 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()
@@ -601,7 +601,7 @@ 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")?;
607607
asyncio.call_method1("set_event_loop", (event_loop,))?;
@@ -614,11 +614,11 @@ To make things a bit easier, I decided to keep most of the old API alongside the
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?;

pytests/test_async_std_asyncio.rs

+33-31
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use pyo3_asyncio::TaskLocals;
1818
use futures::{StreamExt, TryStreamExt};
1919

2020
#[pyfunction]
21-
fn sleep<'p>(py: Python<'p>, secs: &'p PyAny) -> PyResult<&'p PyAny> {
21+
fn sleep<'p>(py: Python<'p>, secs: Bound<'p, PyAny>) -> PyResult<Bound<'p, PyAny>> {
2222
let secs = secs.extract()?;
2323

2424
pyo3_asyncio::async_std::future_into_py(py, async move {
@@ -30,11 +30,11 @@ fn sleep<'p>(py: Python<'p>, secs: &'p PyAny) -> PyResult<&'p PyAny> {
3030
#[pyo3_asyncio::async_std::test]
3131
async fn test_future_into_py() -> PyResult<()> {
3232
let fut = Python::with_gil(|py| {
33-
let sleeper_mod = PyModule::new(py, "rust_sleeper")?;
33+
let sleeper_mod = PyModule::new_bound(py, "rust_sleeper")?;
3434

3535
sleeper_mod.add_wrapped(wrap_pyfunction!(sleep))?;
3636

37-
let test_mod = PyModule::from_code(
37+
let test_mod = PyModule::from_code_bound(
3838
py,
3939
common::TEST_MOD,
4040
"test_future_into_py_mod.py",
@@ -53,13 +53,15 @@ async fn test_future_into_py() -> PyResult<()> {
5353

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

5961
task::sleep(Duration::from_secs(1)).await;
6062

6163
Python::with_gil(|py| {
62-
pyo3_asyncio::async_std::into_future(asyncio.as_ref(py).call_method1("sleep", (1.0,))?)
64+
pyo3_asyncio::async_std::into_future(asyncio.bind(py).call_method1("sleep", (1.0,))?)
6365
})?
6466
.await?;
6567

@@ -146,14 +148,14 @@ async fn test_cancel() -> PyResult<()> {
146148
})?;
147149

148150
if let Err(e) = Python::with_gil(|py| -> PyResult<_> {
149-
py_future.as_ref(py).call_method0("cancel")?;
150-
pyo3_asyncio::async_std::into_future(py_future.as_ref(py))
151+
py_future.bind(py).call_method0("cancel")?;
152+
pyo3_asyncio::async_std::into_future(py_future.into_bound(py))
151153
})?
152154
.await
153155
{
154156
Python::with_gil(|py| -> PyResult<()> {
155-
assert!(e.value(py).is_instance(
156-
py.import("asyncio")?
157+
assert!(e.value_bound(py).is_instance(
158+
py.import_bound("asyncio")?
157159
.getattr("CancelledError")?
158160
.downcast::<PyType>()
159161
.unwrap()
@@ -186,7 +188,7 @@ async def gen():
186188
#[pyo3_asyncio::async_std::test]
187189
async fn test_async_gen_v1() -> PyResult<()> {
188190
let stream = Python::with_gil(|py| {
189-
let test_mod = PyModule::from_code(
191+
let test_mod = PyModule::from_code_bound(
190192
py,
191193
ASYNC_STD_TEST_MOD,
192194
"test_rust_coroutine/async_std_test_mod.py",
@@ -197,7 +199,7 @@ async fn test_async_gen_v1() -> PyResult<()> {
197199
})?;
198200

199201
let vals = stream
200-
.map(|item| Python::with_gil(|py| -> PyResult<i32> { Ok(item?.as_ref(py).extract()?) }))
202+
.map(|item| Python::with_gil(|py| -> PyResult<i32> { Ok(item?.bind(py).extract()?) }))
201203
.try_collect::<Vec<i32>>()
202204
.await?;
203205

@@ -209,7 +211,7 @@ async fn test_async_gen_v1() -> PyResult<()> {
209211
#[pyo3_asyncio::async_std::test]
210212
fn test_local_cancel(event_loop: PyObject) -> PyResult<()> {
211213
let locals = Python::with_gil(|py| -> PyResult<TaskLocals> {
212-
Ok(TaskLocals::new(event_loop.as_ref(py)).copy_context(py)?)
214+
Ok(TaskLocals::new(event_loop.into_bound(py)).copy_context(py)?)
213215
})?;
214216
async_std::task::block_on(pyo3_asyncio::async_std::scope_local(locals, async {
215217
let completed = Arc::new(Mutex::new(false));
@@ -226,14 +228,14 @@ fn test_local_cancel(event_loop: PyObject) -> PyResult<()> {
226228
})?;
227229

228230
if let Err(e) = Python::with_gil(|py| -> PyResult<_> {
229-
py_future.as_ref(py).call_method0("cancel")?;
230-
pyo3_asyncio::async_std::into_future(py_future.as_ref(py))
231+
py_future.bind(py).call_method0("cancel")?;
232+
pyo3_asyncio::async_std::into_future(py_future.into_bound(py))
231233
})?
232234
.await
233235
{
234236
Python::with_gil(|py| -> PyResult<()> {
235-
assert!(e.value(py).is_instance(
236-
py.import("asyncio")?
237+
assert!(e.value_bound(py).is_instance(
238+
py.import_bound("asyncio")?
237239
.getattr("CancelledError")?
238240
.downcast::<PyType>()
239241
.unwrap()
@@ -258,7 +260,7 @@ fn test_local_cancel(event_loop: PyObject) -> PyResult<()> {
258260
fn test_mod(_py: Python, m: &PyModule) -> PyResult<()> {
259261
#![allow(deprecated)]
260262
#[pyfunction(name = "sleep")]
261-
fn sleep_(py: Python) -> PyResult<&PyAny> {
263+
fn sleep_(py: Python) -> PyResult<Bound<PyAny>> {
262264
pyo3_asyncio::async_std::future_into_py(py, async move {
263265
async_std::task::sleep(Duration::from_millis(500)).await;
264266
Ok(())
@@ -290,13 +292,13 @@ fn test_multiple_asyncio_run() -> PyResult<()> {
290292
})?;
291293

292294
let d = [
293-
("asyncio", py.import("asyncio")?.into()),
295+
("asyncio", py.import_bound("asyncio")?.into()),
294296
("test_mod", wrap_pymodule!(test_mod)(py)),
295297
]
296-
.into_py_dict(py);
298+
.into_py_dict_bound(py);
297299

298-
py.run(MULTI_ASYNCIO_CODE, Some(d), None)?;
299-
py.run(MULTI_ASYNCIO_CODE, Some(d), None)?;
300+
py.run_bound(MULTI_ASYNCIO_CODE, Some(&d), None)?;
301+
py.run_bound(MULTI_ASYNCIO_CODE, Some(&d), None)?;
300302
Ok(())
301303
})
302304
}
@@ -305,10 +307,10 @@ fn test_multiple_asyncio_run() -> PyResult<()> {
305307
fn cvars_mod(_py: Python, m: &PyModule) -> PyResult<()> {
306308
#![allow(deprecated)]
307309
#[pyfunction]
308-
pub(crate) fn async_callback(py: Python, callback: PyObject) -> PyResult<&PyAny> {
310+
pub(crate) fn async_callback(py: Python, callback: PyObject) -> PyResult<Bound<PyAny>> {
309311
pyo3_asyncio::async_std::future_into_py(py, async move {
310312
Python::with_gil(|py| {
311-
pyo3_asyncio::async_std::into_future(callback.as_ref(py).call0()?)
313+
pyo3_asyncio::async_std::into_future(callback.bind(py).call0()?)
312314
})?
313315
.await?;
314316

@@ -325,7 +327,7 @@ fn cvars_mod(_py: Python, m: &PyModule) -> PyResult<()> {
325327
#[pyo3_asyncio::async_std::test]
326328
async fn test_async_gen_v2() -> PyResult<()> {
327329
let stream = Python::with_gil(|py| {
328-
let test_mod = PyModule::from_code(
330+
let test_mod = PyModule::from_code_bound(
329331
py,
330332
ASYNC_STD_TEST_MOD,
331333
"test_rust_coroutine/async_std_test_mod.py",
@@ -336,7 +338,7 @@ async fn test_async_gen_v2() -> PyResult<()> {
336338
})?;
337339

338340
let vals = stream
339-
.map(|item| Python::with_gil(|py| -> PyResult<i32> { Ok(item.as_ref(py).extract()?) }))
341+
.map(|item| Python::with_gil(|py| -> PyResult<i32> { Ok(item.bind(py).extract()?) }))
340342
.try_collect::<Vec<i32>>()
341343
.await?;
342344

@@ -362,14 +364,14 @@ asyncio.run(main())
362364
fn test_contextvars() -> PyResult<()> {
363365
Python::with_gil(|py| {
364366
let d = [
365-
("asyncio", py.import("asyncio")?.into()),
366-
("contextvars", py.import("contextvars")?.into()),
367+
("asyncio", py.import_bound("asyncio")?.into()),
368+
("contextvars", py.import_bound("contextvars")?.into()),
367369
("cvars_mod", wrap_pymodule!(cvars_mod)(py)),
368370
]
369-
.into_py_dict(py);
371+
.into_py_dict_bound(py);
370372

371-
py.run(CONTEXTVARS_CODE, Some(d), None)?;
372-
py.run(CONTEXTVARS_CODE, Some(d), None)?;
373+
py.run_bound(CONTEXTVARS_CODE, Some(&d), None)?;
374+
py.run_bound(CONTEXTVARS_CODE, Some(&d), None)?;
373375
Ok(())
374376
})
375377
}

0 commit comments

Comments
 (0)