Skip to content

Commit b780910

Browse files
authored
Merge pull request #7 from bschoenmaeckers/pyo3_v0.22
Update to Pyo3 v0.22
2 parents 38e34c5 + 47c4147 commit b780910

File tree

9 files changed

+141
-152
lines changed

9 files changed

+141
-152
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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.21"
123+
pyo3 = "0.22"
124124
pyo3-async-runtimes-macros = { path = "pyo3-asyncio-macros", version = "=0.21.0", optional = true }
125125

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

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

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ fn rust_sleep(py: Python) -> PyResult<Bound<PyAny>> {
159159
}
160160

161161
#[pymodule]
162-
fn my_async_module(py: Python, m: &PyModule) -> PyResult<()> {
162+
fn my_async_module(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
163163
m.add_function(wrap_pyfunction!(rust_sleep, m)?)?;
164164

165165
Ok(())
@@ -183,7 +183,7 @@ fn rust_sleep(py: Python) -> PyResult<Bound<PyAny>> {
183183
}
184184

185185
#[pymodule]
186-
fn my_async_module(py: Python, m: &PyModule) -> PyResult<()> {
186+
fn my_async_module(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
187187
m.add_function(wrap_pyfunction!(rust_sleep, m)?)?;
188188
Ok(())
189189
}
@@ -453,7 +453,7 @@ fn rust_sleep(py: Python) -> PyResult<Bound<PyAny>> {
453453
}
454454

455455
#[pymodule]
456-
fn my_async_module(_py: Python, m: &PyModule) -> PyResult<()> {
456+
fn my_async_module(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
457457
m.add_function(wrap_pyfunction!(rust_sleep, m)?)?;
458458

459459
Ok(())

pytests/test_async_std_asyncio.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ fn test_local_cancel(event_loop: PyObject) -> PyResult<()> {
262262

263263
/// This module is implemented in Rust.
264264
#[pymodule]
265-
fn test_mod(_py: Python, m: &PyModule) -> PyResult<()> {
265+
fn test_mod(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
266266
#![allow(deprecated)]
267267
#[pyfunction(name = "sleep")]
268268
fn sleep_(py: Python) -> PyResult<Bound<PyAny>> {
@@ -309,7 +309,7 @@ fn test_multiple_asyncio_run() -> PyResult<()> {
309309
}
310310

311311
#[pymodule]
312-
fn cvars_mod(_py: Python, m: &PyModule) -> PyResult<()> {
312+
fn cvars_mod(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
313313
#![allow(deprecated)]
314314
#[pyfunction]
315315
pub(crate) fn async_callback(py: Python, callback: PyObject) -> PyResult<Bound<PyAny>> {

pytests/tokio_asyncio/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ fn test_local_cancel(event_loop: PyObject) -> PyResult<()> {
240240

241241
/// This module is implemented in Rust.
242242
#[pymodule]
243-
fn test_mod(_py: Python, m: &PyModule) -> PyResult<()> {
243+
fn test_mod(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
244244
#![allow(deprecated)]
245245
#[pyfunction(name = "sleep")]
246246
fn sleep_(py: Python) -> PyResult<Bound<PyAny>> {
@@ -287,7 +287,7 @@ fn test_multiple_asyncio_run() -> PyResult<()> {
287287
}
288288

289289
#[pymodule]
290-
fn cvars_mod(_py: Python, m: &PyModule) -> PyResult<()> {
290+
fn cvars_mod(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
291291
#![allow(deprecated)]
292292
#[pyfunction]
293293
fn async_callback(py: Python, callback: PyObject) -> PyResult<Bound<PyAny>> {

src/async_std.rs

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@
1313
//! features = ["unstable-streams"]
1414
//! ```
1515
16-
use std::{any::Any, cell::RefCell, future::Future, panic::AssertUnwindSafe, pin::Pin};
17-
1816
use async_std::task;
1917
use futures::FutureExt;
2018
use pyo3::prelude::*;
19+
use std::{any::Any, cell::RefCell, future::Future, panic::AssertUnwindSafe, pin::Pin};
2120

2221
use crate::{
2322
generic::{self, ContextExt, JoinError, LocalContextExt, Runtime, SpawnLocalExt},
@@ -71,7 +70,7 @@ impl Runtime for AsyncStdRuntime {
7170
AssertUnwindSafe(fut)
7271
.catch_unwind()
7372
.await
74-
.map_err(|e| AsyncStdJoinErr(e))
73+
.map_err(AsyncStdJoinErr)
7574
})
7675
}
7776
}
@@ -90,10 +89,13 @@ impl ContextExt for AsyncStdRuntime {
9089
}
9190

9291
fn get_task_locals() -> Option<TaskLocals> {
93-
match TASK_LOCALS.try_with(|c| c.borrow().clone()) {
94-
Ok(locals) => locals,
95-
Err(_) => None,
96-
}
92+
TASK_LOCALS
93+
.try_with(|c| {
94+
c.borrow()
95+
.as_ref()
96+
.map(|locals| Python::with_gil(|py| locals.clone_ref(py)))
97+
})
98+
.unwrap_or_default()
9799
}
98100
}
99101

@@ -236,13 +238,13 @@ where
236238
/// via [`into_future`] (new behaviour in `v0.15`).
237239
///
238240
/// > Although `contextvars` are preserved for async Python functions, synchronous functions will
239-
/// unfortunately fail to resolve them when called within the Rust future. This is because the
240-
/// function is being called from a Rust thread, not inside an actual Python coroutine context.
241+
/// > unfortunately fail to resolve them when called within the Rust future. This is because the
242+
/// > function is being called from a Rust thread, not inside an actual Python coroutine context.
241243
/// >
242244
/// > As a workaround, you can get the `contextvars` from the current task locals using
243-
/// [`get_current_locals`] and [`TaskLocals::context`](`crate::TaskLocals::context`), then wrap your
244-
/// synchronous function in a call to `contextvars.Context.run`. This will set the context, call the
245-
/// synchronous function, and restore the previous context when it returns or raises an exception.
245+
/// > [`get_current_locals`] and [`TaskLocals::context`](`crate::TaskLocals::context`), then wrap your
246+
/// > synchronous function in a call to `contextvars.Context.run`. This will set the context, call the
247+
/// > synchronous function, and restore the previous context when it returns or raises an exception.
246248
///
247249
/// # Arguments
248250
/// * `py` - PyO3 GIL guard
@@ -291,13 +293,13 @@ where
291293
/// via [`into_future`] (new behaviour in `v0.15`).
292294
///
293295
/// > Although `contextvars` are preserved for async Python functions, synchronous functions will
294-
/// unfortunately fail to resolve them when called within the Rust future. This is because the
295-
/// function is being called from a Rust thread, not inside an actual Python coroutine context.
296+
/// > unfortunately fail to resolve them when called within the Rust future. This is because the
297+
/// > function is being called from a Rust thread, not inside an actual Python coroutine context.
296298
/// >
297299
/// > As a workaround, you can get the `contextvars` from the current task locals using
298-
/// [`get_current_locals`] and [`TaskLocals::context`](`crate::TaskLocals::context`), then wrap your
299-
/// synchronous function in a call to `contextvars.Context.run`. This will set the context, call the
300-
/// synchronous function, and restore the previous context when it returns or raises an exception.
300+
/// > [`get_current_locals`] and [`TaskLocals::context`](`crate::TaskLocals::context`), then wrap your
301+
/// > synchronous function in a call to `contextvars.Context.run`. This will set the context, call the
302+
/// > synchronous function, and restore the previous context when it returns or raises an exception.
301303
///
302304
/// # Arguments
303305
/// * `py` - The current PyO3 GIL guard
@@ -337,13 +339,13 @@ where
337339
/// via [`into_future`] (new behaviour in `v0.15`).
338340
///
339341
/// > Although `contextvars` are preserved for async Python functions, synchronous functions will
340-
/// unfortunately fail to resolve them when called within the Rust future. This is because the
341-
/// function is being called from a Rust thread, not inside an actual Python coroutine context.
342+
/// > unfortunately fail to resolve them when called within the Rust future. This is because the
343+
/// > function is being called from a Rust thread, not inside an actual Python coroutine context.
342344
/// >
343345
/// > As a workaround, you can get the `contextvars` from the current task locals using
344-
/// [`get_current_locals`] and [`TaskLocals::context`](`crate::TaskLocals::context`), then wrap your
345-
/// synchronous function in a call to `contextvars.Context.run`. This will set the context, call the
346-
/// synchronous function, and restore the previous context when it returns or raises an exception.
346+
/// > [`get_current_locals`] and [`TaskLocals::context`](`crate::TaskLocals::context`), then wrap your
347+
/// > synchronous function in a call to `contextvars.Context.run`. This will set the context, call the
348+
/// > synchronous function, and restore the previous context when it returns or raises an exception.
347349
///
348350
/// # Arguments
349351
/// * `py` - PyO3 GIL guard
@@ -412,13 +414,13 @@ where
412414
/// via [`into_future`] (new behaviour in `v0.15`).
413415
///
414416
/// > Although `contextvars` are preserved for async Python functions, synchronous functions will
415-
/// unfortunately fail to resolve them when called within the Rust future. This is because the
416-
/// function is being called from a Rust thread, not inside an actual Python coroutine context.
417+
/// > unfortunately fail to resolve them when called within the Rust future. This is because the
418+
/// > function is being called from a Rust thread, not inside an actual Python coroutine context.
417419
/// >
418420
/// > As a workaround, you can get the `contextvars` from the current task locals using
419-
/// [`get_current_locals`] and [`TaskLocals::context`](`crate::TaskLocals::context`), then wrap your
420-
/// synchronous function in a call to `contextvars.Context.run`. This will set the context, call the
421-
/// synchronous function, and restore the previous context when it returns or raises an exception.
421+
/// > [`get_current_locals`] and [`TaskLocals::context`](`crate::TaskLocals::context`), then wrap your
422+
/// > synchronous function in a call to `contextvars.Context.run`. This will set the context, call the
423+
/// > synchronous function, and restore the previous context when it returns or raises an exception.
422424
///
423425
/// # Arguments
424426
/// * `py` - The current PyO3 GIL guard
@@ -573,8 +575,8 @@ pub fn into_future(
573575
/// # fn main() {}
574576
/// ```
575577
#[cfg(feature = "unstable-streams")]
576-
pub fn into_stream_v1<'p>(
577-
gen: Bound<'p, PyAny>,
578+
pub fn into_stream_v1(
579+
gen: Bound<'_, PyAny>,
578580
) -> PyResult<impl futures::Stream<Item = PyResult<PyObject>> + 'static> {
579581
generic::into_stream_v1::<AsyncStdRuntime>(gen)
580582
}
@@ -633,9 +635,9 @@ pub fn into_stream_v1<'p>(
633635
/// # fn main() {}
634636
/// ```
635637
#[cfg(feature = "unstable-streams")]
636-
pub fn into_stream_with_locals_v1<'p>(
638+
pub fn into_stream_with_locals_v1(
637639
locals: TaskLocals,
638-
gen: Bound<'p, PyAny>,
640+
gen: Bound<'_, PyAny>,
639641
) -> PyResult<impl futures::Stream<Item = PyResult<PyObject>> + 'static> {
640642
generic::into_stream_with_locals_v1::<AsyncStdRuntime>(locals, gen)
641643
}
@@ -694,9 +696,9 @@ pub fn into_stream_with_locals_v1<'p>(
694696
/// # fn main() {}
695697
/// ```
696698
#[cfg(feature = "unstable-streams")]
697-
pub fn into_stream_with_locals_v2<'p>(
699+
pub fn into_stream_with_locals_v2(
698700
locals: TaskLocals,
699-
gen: Bound<'p, PyAny>,
701+
gen: Bound<'_, PyAny>,
700702
) -> PyResult<impl futures::Stream<Item = PyObject> + 'static> {
701703
generic::into_stream_with_locals_v2::<AsyncStdRuntime>(locals, gen)
702704
}
@@ -751,8 +753,8 @@ pub fn into_stream_with_locals_v2<'p>(
751753
/// # fn main() {}
752754
/// ```
753755
#[cfg(feature = "unstable-streams")]
754-
pub fn into_stream_v2<'p>(
755-
gen: Bound<'p, PyAny>,
756+
pub fn into_stream_v2(
757+
gen: Bound<'_, PyAny>,
756758
) -> PyResult<impl futures::Stream<Item = PyObject> + 'static> {
757759
generic::into_stream_v2::<AsyncStdRuntime>(gen)
758760
}

0 commit comments

Comments
 (0)