Skip to content

Commit 0eb883a

Browse files
authored
Merge branch 'main' into dependabot/cargo/clap-4.5
2 parents 14ee6ff + aec0c26 commit 0eb883a

File tree

10 files changed

+205
-210
lines changed

10 files changed

+205
-210
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ name: CI
33
on:
44
push:
55
branches:
6-
- master
6+
- main
77
pull_request:
88
branches:
9-
- master
9+
- main
1010

1111
env:
1212
CARGO_TERM_COLOR: always

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,17 @@ harness = false
114114
required-features = ["async-std-runtime", "testing"]
115115

116116
[dependencies]
117-
async-channel = { version = "1.6", optional = true }
117+
async-channel = { version = "2.3", optional = true }
118118
clap = { version = "4.5", optional = true }
119119
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: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,11 @@ async fn test_other_awaitables() -> PyResult<()> {
9696
#[pyo3_async_runtimes::async_std::test]
9797
async fn test_panic() -> PyResult<()> {
9898
let fut = Python::with_gil(|py| -> PyResult<_> {
99-
pyo3_async_runtimes::async_std::into_future(pyo3_async_runtimes::async_std::future_into_py::<
100-
_,
101-
(),
102-
>(py, async {
103-
panic!("this panic was intentional!")
104-
})?)
99+
pyo3_async_runtimes::async_std::into_future(
100+
pyo3_async_runtimes::async_std::future_into_py::<_, ()>(py, async {
101+
panic!("this panic was intentional!")
102+
})?,
103+
)
105104
})?;
106105

107106
match fut.await {
@@ -263,7 +262,7 @@ fn test_local_cancel(event_loop: PyObject) -> PyResult<()> {
263262

264263
/// This module is implemented in Rust.
265264
#[pymodule]
266-
fn test_mod(_py: Python, m: &PyModule) -> PyResult<()> {
265+
fn test_mod(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
267266
#![allow(deprecated)]
268267
#[pyfunction(name = "sleep")]
269268
fn sleep_(py: Python) -> PyResult<Bound<PyAny>> {
@@ -310,7 +309,7 @@ fn test_multiple_asyncio_run() -> PyResult<()> {
310309
}
311310

312311
#[pymodule]
313-
fn cvars_mod(_py: Python, m: &PyModule) -> PyResult<()> {
312+
fn cvars_mod(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
314313
#![allow(deprecated)]
315314
#[pyfunction]
316315
pub(crate) fn async_callback(py: Python, callback: PyObject) -> PyResult<Bound<PyAny>> {
@@ -385,5 +384,7 @@ fn test_contextvars() -> PyResult<()> {
385384
fn main() -> pyo3::PyResult<()> {
386385
pyo3::prepare_freethreaded_python();
387386

388-
Python::with_gil(|py| pyo3_async_runtimes::async_std::run(py, pyo3_async_runtimes::testing::main()))
387+
Python::with_gil(|py| {
388+
pyo3_async_runtimes::async_std::run(py, pyo3_async_runtimes::testing::main())
389+
})
389390
}

pytests/tokio_asyncio/mod.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,11 @@ fn test_local_future_into_py(event_loop: PyObject) -> PyResult<()> {
122122
#[pyo3_async_runtimes::tokio::test]
123123
async fn test_panic() -> PyResult<()> {
124124
let fut = Python::with_gil(|py| -> PyResult<_> {
125-
pyo3_async_runtimes::tokio::into_future(pyo3_async_runtimes::tokio::future_into_py::<_, ()>(
126-
py,
127-
async { panic!("this panic was intentional!") },
128-
)?)
125+
pyo3_async_runtimes::tokio::into_future(
126+
pyo3_async_runtimes::tokio::future_into_py::<_, ()>(py, async {
127+
panic!("this panic was intentional!")
128+
})?,
129+
)
129130
})?;
130131

131132
match fut.await {
@@ -239,7 +240,7 @@ fn test_local_cancel(event_loop: PyObject) -> PyResult<()> {
239240

240241
/// This module is implemented in Rust.
241242
#[pymodule]
242-
fn test_mod(_py: Python, m: &PyModule) -> PyResult<()> {
243+
fn test_mod(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
243244
#![allow(deprecated)]
244245
#[pyfunction(name = "sleep")]
245246
fn sleep_(py: Python) -> PyResult<Bound<PyAny>> {
@@ -286,7 +287,7 @@ fn test_multiple_asyncio_run() -> PyResult<()> {
286287
}
287288

288289
#[pymodule]
289-
fn cvars_mod(_py: Python, m: &PyModule) -> PyResult<()> {
290+
fn cvars_mod(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
290291
#![allow(deprecated)]
291292
#[pyfunction]
292293
fn async_callback(py: Python, callback: PyObject) -> PyResult<Bound<PyAny>> {

src/async_std.rs

Lines changed: 39 additions & 37 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},
@@ -34,13 +33,13 @@ pub mod re_exports {
3433

3534
/// <span class="module-item stab portability" style="display: inline; border-radius: 3px; padding: 2px; font-size: 80%; line-height: 1.2;"><code>attributes</code></span> Provides the boilerplate for the `async-std` runtime and runs an async fn as main
3635
#[cfg(feature = "attributes")]
37-
pub use pyo3_asyncio_macros_0_21::async_std_main as main;
36+
pub use pyo3_async_runtimes_macros::async_std_main as main;
3837

3938
/// <span class="module-item stab portability" style="display: inline; border-radius: 3px; padding: 2px; font-size: 80%; line-height: 1.2;"><code>attributes</code></span>
4039
/// <span class="module-item stab portability" style="display: inline; border-radius: 3px; padding: 2px; font-size: 80%; line-height: 1.2;"><code>testing</code></span>
4140
/// Registers an `async-std` test with the `pyo3-asyncio` test harness
4241
#[cfg(all(feature = "attributes", feature = "testing"))]
43-
pub use pyo3_asyncio_macros_0_21::async_std_test as test;
42+
pub use pyo3_async_runtimes_macros::async_std_test as test;
4443

4544
struct AsyncStdJoinErr(Box<dyn Any + Send + 'static>);
4645

@@ -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)