Skip to content

Commit 5ce66d1

Browse files
Prepare for pyo3 0.26 (#54)
* Bump to 0.26 * Prepare for pyo3 0.26 * fix macros * Remove unneeded into_pyobject * lint * Replace some `OnceCell` with `PyOnceLock` * Use released v0.26 * Bump msrv to 1.74 * Remove setting MSRV deps in CI and use stdlib instead of once_cell where possible * move import into function * bump msrv in CI test * simplify `GenericSender::send --------- Co-authored-by: David Hewitt <[email protected]>
1 parent 5e6c5b7 commit 5ce66d1

27 files changed

+288
-317
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ jobs:
7979
]
8080
include:
8181
# Test minimal supported Rust version
82-
- rust: 1.63.0
82+
- rust: 1.74.0
8383
python-version: "3.13"
8484
platform:
8585
{
@@ -126,12 +126,6 @@ jobs:
126126
toolchain: ${{ matrix.rust }}
127127
target: ${{ matrix.platform.rust-target }}
128128

129-
- if: ${{ matrix.msrv == 'MSRV' }}
130-
name: Set MSRV dependencies
131-
run: |
132-
cargo add tokio@=1.38.1
133-
cargo update -p once_cell --precise 1.20.3
134-
135129
- name: Build (no features)
136130
run: cargo build --no-default-features --verbose --target ${{ matrix.platform.rust-target }}
137131

Cargo.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "pyo3-async-runtimes"
33
description = "PyO3 bridges from Rust runtimes to Python's Asyncio library"
4-
version = "0.25.0"
4+
version = "0.26.0"
55
authors = [
66
"Andrew J Westlake <[email protected]>",
77
"David Hewitt <[email protected]>",
@@ -15,7 +15,7 @@ categories = ["api-bindings", "development-tools::ffi"]
1515
license = "Apache-2.0"
1616
exclude = ["/.gitignore", "/codecov.yml", "/Makefile"]
1717
edition = "2021"
18-
rust-version = "1.63"
18+
rust-version = "1.74"
1919

2020
[workspace]
2121
members = ["pyo3-async-runtimes-macros"]
@@ -120,11 +120,11 @@ futures = "0.3"
120120
inventory = { version = "0.3", optional = true }
121121
once_cell = "1.14"
122122
pin-project-lite = "0.2"
123-
pyo3 = "0.25"
124-
pyo3-async-runtimes-macros = { path = "pyo3-async-runtimes-macros", version = "=0.25.0", optional = true }
123+
pyo3 = "0.26"
124+
pyo3-async-runtimes-macros = { path = "pyo3-async-runtimes-macros", version = "=0.26.0", optional = true }
125125

126126
[dev-dependencies]
127-
pyo3 = { version = "0.25", features = ["macros"] }
127+
pyo3 = { version = "0.26", features = ["macros"] }
128128

129129
[dependencies.async-std]
130130
version = "1.12"

README.md

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ Here we initialize the runtime, import Python's `asyncio` library and run the gi
5050
```toml
5151
# Cargo.toml dependencies
5252
[dependencies]
53-
pyo3 = { version = "0.25" }
54-
pyo3-async-runtimes = { version = "0.25", features = ["attributes", "async-std-runtime"] }
53+
pyo3 = { version = "0.26" }
54+
pyo3-async-runtimes = { version = "0.26", features = ["attributes", "async-std-runtime"] }
5555
async-std = "1.13"
5656
```
5757

@@ -62,10 +62,10 @@ use pyo3::prelude::*;
6262

6363
#[pyo3_async_runtimes::async_std::main]
6464
async fn main() -> PyResult<()> {
65-
let fut = Python::with_gil(|py| {
65+
let fut = Python::attach(|py| {
6666
let asyncio = py.import("asyncio")?;
6767
// convert asyncio.sleep into a Rust Future
68-
pyo3_async_runtimes::async_std::into_future(asyncio.call_method1("sleep", (1.into_pyobject(py).unwrap(),))?)
68+
pyo3_async_runtimes::async_std::into_future(asyncio.call_method1("sleep", (1,))?)
6969
})?;
7070

7171
fut.await?;
@@ -80,8 +80,8 @@ attribute.
8080
```toml
8181
# Cargo.toml dependencies
8282
[dependencies]
83-
pyo3 = { version = "0.25" }
84-
pyo3-async-runtimes = { version = "0.25", features = ["attributes", "tokio-runtime"] }
83+
pyo3 = { version = "0.26" }
84+
pyo3-async-runtimes = { version = "0.26", features = ["attributes", "tokio-runtime"] }
8585
tokio = "1.40"
8686
```
8787

@@ -92,10 +92,10 @@ use pyo3::prelude::*;
9292

9393
#[pyo3_async_runtimes::tokio::main]
9494
async fn main() -> PyResult<()> {
95-
let fut = Python::with_gil(|py| {
95+
let fut = Python::attach(|py| {
9696
let asyncio = py.import("asyncio")?;
9797
// convert asyncio.sleep into a Rust Future
98-
pyo3_async_runtimes::tokio::into_future(asyncio.call_method1("sleep", (1.into_pyobject(py).unwrap(),))?)
98+
pyo3_async_runtimes::tokio::into_future(asyncio.call_method1("sleep", (1,))?)
9999
})?;
100100

101101
fut.await?;
@@ -126,17 +126,17 @@ For `async-std`:
126126

127127
```toml
128128
[dependencies]
129-
pyo3 = { version = "0.25", features = ["extension-module"] }
130-
pyo3-async-runtimes = { version = "0.25", features = ["async-std-runtime"] }
129+
pyo3 = { version = "0.26", features = ["extension-module"] }
130+
pyo3-async-runtimes = { version = "0.26", features = ["async-std-runtime"] }
131131
async-std = "1.13"
132132
```
133133

134134
For `tokio`:
135135

136136
```toml
137137
[dependencies]
138-
pyo3 = { version = "0.25", features = ["extension-module"] }
139-
pyo3-async-runtimes = { version = "0.25", features = ["tokio-runtime"] }
138+
pyo3 = { version = "0.26", features = ["extension-module"] }
139+
pyo3-async-runtimes = { version = "0.26", features = ["tokio-runtime"] }
140140
tokio = "1.40"
141141
```
142142

@@ -234,7 +234,7 @@ use pyo3::prelude::*;
234234

235235
#[pyo3_async_runtimes::tokio::main]
236236
async fn main() -> PyResult<()> {
237-
let future = Python::with_gil(|py| -> PyResult<_> {
237+
let future = Python::attach(|py| -> PyResult<_> {
238238
// import the module containing the py_sleep function
239239
let example = py.import("example")?;
240240

@@ -354,12 +354,12 @@ use pyo3::prelude::*;
354354
async fn main() -> PyResult<()> {
355355
// PyO3 is initialized - Ready to go
356356
357-
let fut = Python::with_gil(|py| -> PyResult<_> {
357+
let fut = Python::attach(|py| -> PyResult<_> {
358358
let asyncio = py.import("asyncio")?;
359359
360360
// convert asyncio.sleep into a Rust Future
361361
pyo3_async_runtimes::async_std::into_future(
362-
asyncio.call_method1("sleep", (1.into_pyobject(py).unwrap(),))?
362+
asyncio.call_method1("sleep", (1,))?
363363
)
364364
})?;
365365
@@ -430,8 +430,8 @@ name = "my_async_module"
430430
crate-type = ["cdylib"]
431431

432432
[dependencies]
433-
pyo3 = { version = "0.25", features = ["extension-module"] }
434-
pyo3-async-runtimes = { version = "0.25", features = ["tokio-runtime"] }
433+
pyo3 = { version = "0.26", features = ["extension-module"] }
434+
pyo3-async-runtimes = { version = "0.26", features = ["tokio-runtime"] }
435435
async-std = "1.13"
436436
tokio = "1.40"
437437
```
@@ -490,8 +490,8 @@ event loop before we can install the `uvloop` policy.
490490
```toml
491491
[dependencies]
492492
async-std = "1.13"
493-
pyo3 = "0.25"
494-
pyo3-async-runtimes = { version = "0.25", features = ["async-std-runtime"] }
493+
pyo3 = "0.26"
494+
pyo3-async-runtimes = { version = "0.26", features = ["async-std-runtime"] }
495495
```
496496

497497
```rust no_run
@@ -500,18 +500,18 @@ pyo3-async-runtimes = { version = "0.25", features = ["async-std-runtime"] }
500500
use pyo3::{prelude::*, types::PyType};
501501

502502
fn main() -> PyResult<()> {
503-
pyo3::prepare_freethreaded_python();
503+
Python::initialize();
504504

505-
Python::with_gil(|py| {
505+
Python::attach(|py| {
506506
let uvloop = py.import("uvloop")?;
507507
uvloop.call_method0("install")?;
508508

509509
// store a reference for the assertion
510-
let uvloop = PyObject::from(uvloop);
510+
let uvloop: Py<PyAny> = uvloop.into();
511511

512512
pyo3_async_runtimes::async_std::run(py, async move {
513513
// verify that we are on a uvloop.Loop
514-
Python::with_gil(|py| -> PyResult<()> {
514+
Python::attach(|py| -> PyResult<()> {
515515
assert!(uvloop
516516
.bind(py)
517517
.getattr("Loop")?

examples/async_std.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@ use pyo3::prelude::*;
22

33
#[pyo3_async_runtimes::async_std::main]
44
async fn main() -> PyResult<()> {
5-
let fut = Python::with_gil(|py| {
5+
let fut = Python::attach(|py| {
66
let asyncio = py.import("asyncio")?;
77

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

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

examples/tokio.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@ use pyo3::prelude::*;
22

33
#[pyo3_async_runtimes::tokio::main]
44
async fn main() -> PyResult<()> {
5-
let fut = Python::with_gil(|py| {
5+
let fut = Python::attach(|py| {
66
let asyncio = py.import("asyncio")?;
77

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

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

examples/tokio_current_thread.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@ use pyo3::prelude::*;
22

33
#[pyo3_async_runtimes::tokio::main(flavor = "current_thread")]
44
async fn main() -> PyResult<()> {
5-
let fut = Python::with_gil(|py| {
5+
let fut = Python::attach(|py| {
66
let asyncio = py.import("asyncio")?;
77

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

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

examples/tokio_multi_thread.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@ use pyo3::prelude::*;
22

33
#[pyo3_async_runtimes::tokio::main(flavor = "multi_thread", worker_threads = 10)]
44
async fn main() -> PyResult<()> {
5-
let fut = Python::with_gil(|py| {
5+
let fut = Python::attach(|py| {
66
let asyncio = py.import("asyncio")?;
77

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

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

pyo3-async-runtimes-macros/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "pyo3-async-runtimes-macros"
33
description = "Proc Macro Attributes for `pyo3-async-runtimes`"
4-
version = "0.25.0"
4+
version = "0.26.0"
55
authors = [
66
"Andrew J Westlake <[email protected]>",
77
"David Hewitt <[email protected]>",

pyo3-async-runtimes-macros/src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ pub fn async_std_main(_attr: TokenStream, item: TokenStream) -> TokenStream {
4949
#body
5050
}
5151

52-
pyo3::prepare_freethreaded_python();
52+
pyo3::Python::initialize();
5353

54-
pyo3::Python::with_gil(|py| {
54+
pyo3::Python::attach(|py| {
5555
pyo3_async_runtimes::async_std::run(py, main())
5656
.map_err(|e| {
5757
e.print_and_set_sys_last_vars(py);
@@ -129,7 +129,7 @@ pub fn tokio_main(args: TokenStream, item: TokenStream) -> TokenStream {
129129
///
130130
/// // blocking test functions can optionally accept an event_loop parameter
131131
/// #[pyo3_async_runtimes::async_std::test]
132-
/// fn test_blocking_sleep_with_event_loop(event_loop: PyObject) -> PyResult<()> {
132+
/// fn test_blocking_sleep_with_event_loop(event_loop: Py<PyAny>) -> PyResult<()> {
133133
/// thread::sleep(Duration::from_secs(1));
134134
/// Ok(())
135135
/// }
@@ -154,7 +154,7 @@ pub fn async_std_test(_attr: TokenStream, item: TokenStream) -> TokenStream {
154154
}
155155
} else {
156156
quote! {
157-
let event_loop = Python::with_gil(|py| {
157+
let event_loop = pyo3::Python::attach(|py| {
158158
pyo3_async_runtimes::async_std::get_current_loop(py).unwrap().into()
159159
});
160160
Box::pin(pyo3_async_runtimes::async_std::re_exports::spawn_blocking(move || {
@@ -226,7 +226,7 @@ pub fn async_std_test(_attr: TokenStream, item: TokenStream) -> TokenStream {
226226
///
227227
/// // blocking test functions can optionally accept an event_loop parameter
228228
/// #[pyo3_async_runtimes::tokio::test]
229-
/// fn test_blocking_sleep_with_event_loop(event_loop: PyObject) -> PyResult<()> {
229+
/// fn test_blocking_sleep_with_event_loop(event_loop: Py<PyAny>) -> PyResult<()> {
230230
/// thread::sleep(Duration::from_secs(1));
231231
/// Ok(())
232232
/// }
@@ -265,7 +265,7 @@ pub fn tokio_test(_attr: TokenStream, item: TokenStream) -> TokenStream {
265265
}
266266
} else {
267267
quote! {
268-
let event_loop = Python::with_gil(|py| {
268+
let event_loop = pyo3::Python::attach(|py| {
269269
pyo3_async_runtimes::tokio::get_current_loop(py).unwrap().into()
270270
});
271271
Box::pin(async move {

pyo3-async-runtimes-macros/src/tokio.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ fn parse_knobs(
268268
#body
269269
}
270270

271-
pyo3::prepare_freethreaded_python();
271+
pyo3::Python::initialize();
272272

273273
let mut builder = #builder;
274274
#builder_init;
@@ -277,7 +277,7 @@ fn parse_knobs(
277277

278278
#rt_init
279279

280-
pyo3::Python::with_gil(|py| {
280+
pyo3::Python::attach(|py| {
281281
pyo3_async_runtimes::tokio::run(py, main())
282282
.map_err(|e| {
283283
e.print_and_set_sys_last_vars(py);

0 commit comments

Comments
 (0)