Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to Pyo3 v0.22 #7

Merged
merged 4 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ futures = "0.3"
inventory = { version = "0.3", optional = true }
once_cell = "1.14"
pin-project-lite = "0.2"
pyo3 = "0.21"
pyo3 = "0.22"
pyo3-async-runtimes-macros = { path = "pyo3-asyncio-macros", version = "=0.21.0", optional = true }

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

[dependencies.async-std]
version = "1.12"
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ fn rust_sleep(py: Python) -> PyResult<Bound<PyAny>> {
}

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

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

#[pymodule]
fn my_async_module(py: Python, m: &PyModule) -> PyResult<()> {
fn my_async_module(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(rust_sleep, m)?)?;
Ok(())
}
Expand Down Expand Up @@ -453,7 +453,7 @@ fn rust_sleep(py: Python) -> PyResult<Bound<PyAny>> {
}

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

Ok(())
Expand Down
4 changes: 2 additions & 2 deletions pytests/test_async_std_asyncio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ fn test_local_cancel(event_loop: PyObject) -> PyResult<()> {

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

#[pymodule]
fn cvars_mod(_py: Python, m: &PyModule) -> PyResult<()> {
fn cvars_mod(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
#![allow(deprecated)]
#[pyfunction]
pub(crate) fn async_callback(py: Python, callback: PyObject) -> PyResult<Bound<PyAny>> {
Expand Down
4 changes: 2 additions & 2 deletions pytests/tokio_asyncio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ fn test_local_cancel(event_loop: PyObject) -> PyResult<()> {

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

#[pymodule]
fn cvars_mod(_py: Python, m: &PyModule) -> PyResult<()> {
fn cvars_mod(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
#![allow(deprecated)]
#[pyfunction]
fn async_callback(py: Python, callback: PyObject) -> PyResult<Bound<PyAny>> {
Expand Down
72 changes: 37 additions & 35 deletions src/async_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
//! features = ["unstable-streams"]
//! ```

use std::{any::Any, cell::RefCell, future::Future, panic::AssertUnwindSafe, pin::Pin};

use async_std::task;
use futures::FutureExt;
use pyo3::prelude::*;
use std::{any::Any, cell::RefCell, future::Future, panic::AssertUnwindSafe, pin::Pin};

use crate::{
generic::{self, ContextExt, JoinError, LocalContextExt, Runtime, SpawnLocalExt},
Expand Down Expand Up @@ -71,7 +70,7 @@ impl Runtime for AsyncStdRuntime {
AssertUnwindSafe(fut)
.catch_unwind()
.await
.map_err(|e| AsyncStdJoinErr(e))
.map_err(AsyncStdJoinErr)
})
}
}
Expand All @@ -90,10 +89,13 @@ impl ContextExt for AsyncStdRuntime {
}

fn get_task_locals() -> Option<TaskLocals> {
match TASK_LOCALS.try_with(|c| c.borrow().clone()) {
Ok(locals) => locals,
Err(_) => None,
}
TASK_LOCALS
.try_with(|c| {
c.borrow()
.as_ref()
.map(|locals| Python::with_gil(|py| locals.clone_ref(py)))
})
.unwrap_or_default()
}
}

Expand Down Expand Up @@ -236,13 +238,13 @@ where
/// via [`into_future`] (new behaviour in `v0.15`).
///
/// > Although `contextvars` are preserved for async Python functions, synchronous functions will
/// unfortunately fail to resolve them when called within the Rust future. This is because the
/// function is being called from a Rust thread, not inside an actual Python coroutine context.
/// > unfortunately fail to resolve them when called within the Rust future. This is because the
/// > function is being called from a Rust thread, not inside an actual Python coroutine context.
/// >
/// > As a workaround, you can get the `contextvars` from the current task locals using
/// [`get_current_locals`] and [`TaskLocals::context`](`crate::TaskLocals::context`), then wrap your
/// synchronous function in a call to `contextvars.Context.run`. This will set the context, call the
/// synchronous function, and restore the previous context when it returns or raises an exception.
/// > [`get_current_locals`] and [`TaskLocals::context`](`crate::TaskLocals::context`), then wrap your
/// > synchronous function in a call to `contextvars.Context.run`. This will set the context, call the
/// > synchronous function, and restore the previous context when it returns or raises an exception.
///
/// # Arguments
/// * `py` - PyO3 GIL guard
Expand Down Expand Up @@ -291,13 +293,13 @@ where
/// via [`into_future`] (new behaviour in `v0.15`).
///
/// > Although `contextvars` are preserved for async Python functions, synchronous functions will
/// unfortunately fail to resolve them when called within the Rust future. This is because the
/// function is being called from a Rust thread, not inside an actual Python coroutine context.
/// > unfortunately fail to resolve them when called within the Rust future. This is because the
/// > function is being called from a Rust thread, not inside an actual Python coroutine context.
/// >
/// > As a workaround, you can get the `contextvars` from the current task locals using
/// [`get_current_locals`] and [`TaskLocals::context`](`crate::TaskLocals::context`), then wrap your
/// synchronous function in a call to `contextvars.Context.run`. This will set the context, call the
/// synchronous function, and restore the previous context when it returns or raises an exception.
/// > [`get_current_locals`] and [`TaskLocals::context`](`crate::TaskLocals::context`), then wrap your
/// > synchronous function in a call to `contextvars.Context.run`. This will set the context, call the
/// > synchronous function, and restore the previous context when it returns or raises an exception.
///
/// # Arguments
/// * `py` - The current PyO3 GIL guard
Expand Down Expand Up @@ -337,13 +339,13 @@ where
/// via [`into_future`] (new behaviour in `v0.15`).
///
/// > Although `contextvars` are preserved for async Python functions, synchronous functions will
/// unfortunately fail to resolve them when called within the Rust future. This is because the
/// function is being called from a Rust thread, not inside an actual Python coroutine context.
/// > unfortunately fail to resolve them when called within the Rust future. This is because the
/// > function is being called from a Rust thread, not inside an actual Python coroutine context.
/// >
/// > As a workaround, you can get the `contextvars` from the current task locals using
/// [`get_current_locals`] and [`TaskLocals::context`](`crate::TaskLocals::context`), then wrap your
/// synchronous function in a call to `contextvars.Context.run`. This will set the context, call the
/// synchronous function, and restore the previous context when it returns or raises an exception.
/// > [`get_current_locals`] and [`TaskLocals::context`](`crate::TaskLocals::context`), then wrap your
/// > synchronous function in a call to `contextvars.Context.run`. This will set the context, call the
/// > synchronous function, and restore the previous context when it returns or raises an exception.
///
/// # Arguments
/// * `py` - PyO3 GIL guard
Expand Down Expand Up @@ -412,13 +414,13 @@ where
/// via [`into_future`] (new behaviour in `v0.15`).
///
/// > Although `contextvars` are preserved for async Python functions, synchronous functions will
/// unfortunately fail to resolve them when called within the Rust future. This is because the
/// function is being called from a Rust thread, not inside an actual Python coroutine context.
/// > unfortunately fail to resolve them when called within the Rust future. This is because the
/// > function is being called from a Rust thread, not inside an actual Python coroutine context.
/// >
/// > As a workaround, you can get the `contextvars` from the current task locals using
/// [`get_current_locals`] and [`TaskLocals::context`](`crate::TaskLocals::context`), then wrap your
/// synchronous function in a call to `contextvars.Context.run`. This will set the context, call the
/// synchronous function, and restore the previous context when it returns or raises an exception.
/// > [`get_current_locals`] and [`TaskLocals::context`](`crate::TaskLocals::context`), then wrap your
/// > synchronous function in a call to `contextvars.Context.run`. This will set the context, call the
/// > synchronous function, and restore the previous context when it returns or raises an exception.
///
/// # Arguments
/// * `py` - The current PyO3 GIL guard
Expand Down Expand Up @@ -573,8 +575,8 @@ pub fn into_future(
/// # fn main() {}
/// ```
#[cfg(feature = "unstable-streams")]
pub fn into_stream_v1<'p>(
gen: Bound<'p, PyAny>,
pub fn into_stream_v1(
gen: Bound<'_, PyAny>,
) -> PyResult<impl futures::Stream<Item = PyResult<PyObject>> + 'static> {
generic::into_stream_v1::<AsyncStdRuntime>(gen)
}
Expand Down Expand Up @@ -633,9 +635,9 @@ pub fn into_stream_v1<'p>(
/// # fn main() {}
/// ```
#[cfg(feature = "unstable-streams")]
pub fn into_stream_with_locals_v1<'p>(
pub fn into_stream_with_locals_v1(
locals: TaskLocals,
gen: Bound<'p, PyAny>,
gen: Bound<'_, PyAny>,
) -> PyResult<impl futures::Stream<Item = PyResult<PyObject>> + 'static> {
generic::into_stream_with_locals_v1::<AsyncStdRuntime>(locals, gen)
}
Expand Down Expand Up @@ -694,9 +696,9 @@ pub fn into_stream_with_locals_v1<'p>(
/// # fn main() {}
/// ```
#[cfg(feature = "unstable-streams")]
pub fn into_stream_with_locals_v2<'p>(
pub fn into_stream_with_locals_v2(
locals: TaskLocals,
gen: Bound<'p, PyAny>,
gen: Bound<'_, PyAny>,
) -> PyResult<impl futures::Stream<Item = PyObject> + 'static> {
generic::into_stream_with_locals_v2::<AsyncStdRuntime>(locals, gen)
}
Expand Down Expand Up @@ -751,8 +753,8 @@ pub fn into_stream_with_locals_v2<'p>(
/// # fn main() {}
/// ```
#[cfg(feature = "unstable-streams")]
pub fn into_stream_v2<'p>(
gen: Bound<'p, PyAny>,
pub fn into_stream_v2(
gen: Bound<'_, PyAny>,
) -> PyResult<impl futures::Stream<Item = PyObject> + 'static> {
generic::into_stream_v2::<AsyncStdRuntime>(gen)
}
Loading
Loading