Skip to content

Commit a7f0def

Browse files
author
Andrew J Westlake
committed
Tested under feature powerset to ensure all docs/tests work under any set of features
1 parent 746e44a commit a7f0def

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

pytests/tokio_asyncio/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,8 @@ fn test_mod(_py: Python, m: &PyModule) -> PyResult<()> {
203203
#![allow(deprecated)]
204204
#[pyfn(m, "sleep")]
205205
fn sleep(py: Python) -> PyResult<&PyAny> {
206-
pyo3_asyncio::async_std::future_into_py(py, async move {
207-
async_std::task::sleep(Duration::from_millis(500)).await;
206+
pyo3_asyncio::tokio::future_into_py(py, async move {
207+
tokio::time::sleep(Duration::from_millis(500)).await;
208208
Ok(Python::with_gil(|py| py.None()))
209209
})
210210
}

src/lib.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@
5151
//! One clear disadvantage to this approach (aside from the verbose naming) is that the Rust application has to explicitly track its references to the Python event loop. In native libraries, we can't make any assumptions about the underlying event loop, so the only reliable way to make sure our conversions work properly is to store a reference to the current event loop at the callsite to use later on.
5252
//!
5353
//! ```rust
54-
//! use pyo3::prelude::*;
54+
//! use pyo3::{wrap_pyfunction, prelude::*};
5555
//!
56+
//! # #[cfg(feature = "tokio-runtime")]
5657
//! #[pyfunction]
5758
//! fn sleep(py: Python) -> PyResult<&PyAny> {
5859
//! let current_loop = pyo3_asyncio::get_running_loop(py)?;
@@ -76,6 +77,7 @@
7677
//! })
7778
//! }
7879
//!
80+
//! # #[cfg(feature = "tokio-runtime")]
7981
//! #[pymodule]
8082
//! fn my_mod(py: Python, m: &PyModule) -> PyResult<()> {
8183
//! m.add_function(wrap_pyfunction!(sleep, m)?)?;
@@ -101,6 +103,7 @@
101103
//! ```rust no_run
102104
//! use pyo3::prelude::*;
103105
//!
106+
//! # #[cfg(feature = "tokio-runtime")]
104107
//! #[pyfunction]
105108
//! fn sleep(py: Python) -> PyResult<&PyAny> {
106109
//! // get the current event loop through task-local data
@@ -126,6 +129,7 @@
126129
//! )
127130
//! }
128131
//!
132+
//! # #[cfg(feature = "tokio-runtime")]
129133
//! #[pyfunction]
130134
//! fn wrap_sleep(py: Python) -> PyResult<&PyAny> {
131135
//! // get the current event loop through task-local data
@@ -152,6 +156,7 @@
152156
//! )
153157
//! }
154158
//!
159+
//! # #[cfg(feature = "tokio-runtime")]
155160
//! #[pymodule]
156161
//! fn my_mod(py: Python, m: &PyModule) -> PyResult<()> {
157162
//! m.add_function(wrap_pyfunction!(sleep, m)?)?;
@@ -174,6 +179,7 @@
174179
//! ```rust
175180
//! use pyo3::prelude::*;
176181
//!
182+
//! # #[cfg(feature = "tokio-runtime")]
177183
//! #[pyfunction]
178184
//! fn sleep(py: Python) -> PyResult<&PyAny> {
179185
//! pyo3_asyncio::tokio::future_into_py(py, async move {
@@ -189,6 +195,7 @@
189195
//! })
190196
//! }
191197
//!
198+
//! # #[cfg(feature = "tokio-runtime")]
192199
//! #[pyfunction]
193200
//! fn wrap_sleep(py: Python) -> PyResult<&PyAny> {
194201
//! pyo3_asyncio::tokio::future_into_py(py, async move {
@@ -202,6 +209,7 @@
202209
//! })
203210
//! }
204211
//!
212+
//! # #[cfg(feature = "tokio-runtime")]
205213
//! #[pymodule]
206214
//! fn my_mod(py: Python, m: &PyModule) -> PyResult<()> {
207215
//! m.add_function(wrap_pyfunction!(sleep, m)?)?;
@@ -391,7 +399,7 @@ fn create_future(event_loop: &PyAny) -> PyResult<&PyAny> {
391399
/// ```
392400
#[deprecated(
393401
since = "0.14.0",
394-
note = "Use the pyo3_asyncio::async_std::run or pyo3_asyncio::tokio::run instead\n\t\t(see the [migration guide](https://github.com/awestlake87/pyo3-asyncio/#migrating-from-013-to-014) for more details)"
402+
note = "Use the pyo3_asyncio::async_std::run or pyo3_asyncio::tokio::run instead\n (see the [migration guide](https://github.com/awestlake87/pyo3-asyncio/#migrating-from-013-to-014) for more details)"
395403
)]
396404
#[allow(deprecated)]
397405
pub fn with_runtime<F, R>(py: Python, f: F) -> PyResult<R>

src/testing.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,24 @@
3030
//!
3131
//! `pytests/test_example.rs` for the `tokio` runtime:
3232
//! ```rust
33+
//! # #[cfg(all(feature = "tokio-runtime", feature = "attributes"))]
3334
//! #[pyo3_asyncio::tokio::main]
3435
//! async fn main() -> pyo3::PyResult<()> {
3536
//! pyo3_asyncio::testing::main().await
3637
//! }
38+
//! # #[cfg(not(all(feature = "tokio-runtime", feature = "attributes")))]
39+
//! # fn main() {}
3740
//! ```
3841
//!
3942
//! `pytests/test_example.rs` for the `async-std` runtime:
4043
//! ```rust
44+
//! # #[cfg(all(feature = "async-std-runtime", feature = "attributes"))]
4145
//! #[pyo3_asyncio::async_std::main]
4246
//! async fn main() -> pyo3::PyResult<()> {
4347
//! pyo3_asyncio::testing::main().await
4448
//! }
49+
//! # #[cfg(not(all(feature = "async-std-runtime", feature = "attributes")))]
50+
//! # fn main() {}
4551
//! ```
4652
//!
4753
//! ## Cargo Configuration
@@ -69,6 +75,7 @@
6975
//!
7076
//! For `async-std` use the [`pyo3_asyncio::async_std::test`](https://docs.rs/pyo3-asyncio/latest/pyo3_asyncio/async_std/attr.test.html) attribute:
7177
//! ```rust
78+
//! # #[cfg(all(feature = "async-std-runtime", feature = "attributes"))]
7279
//! mod tests {
7380
//! use std::{time::Duration, thread};
7481
//!
@@ -89,14 +96,18 @@
8996
//! }
9097
//! }
9198
//!
99+
//! # #[cfg(all(feature = "async-std-runtime", feature = "attributes"))]
92100
//! #[pyo3_asyncio::async_std::main]
93101
//! async fn main() -> pyo3::PyResult<()> {
94102
//! pyo3_asyncio::testing::main().await
95103
//! }
104+
//! # #[cfg(not(all(feature = "async-std-runtime", feature = "attributes")))]
105+
//! # fn main() {}
96106
//! ```
97107
//!
98108
//! For `tokio` use the [`pyo3_asyncio::tokio::test`](https://docs.rs/pyo3-asyncio/latest/pyo3_asyncio/tokio/attr.test.html) attribute:
99109
//! ```rust
110+
//! # #[cfg(all(feature = "tokio-runtime", feature = "attributes"))]
100111
//! mod tests {
101112
//! use std::{time::Duration, thread};
102113
//!
@@ -117,10 +128,13 @@
117128
//! }
118129
//! }
119130
//!
131+
//! # #[cfg(all(feature = "tokio-runtime", feature = "attributes"))]
120132
//! #[pyo3_asyncio::tokio::main]
121133
//! async fn main() -> pyo3::PyResult<()> {
122134
//! pyo3_asyncio::testing::main().await
123135
//! }
136+
//! # #[cfg(not(all(feature = "tokio-runtime", feature = "attributes")))]
137+
//! # fn main() {}
124138
//! ```
125139
//!
126140
//! ## Lib Tests

0 commit comments

Comments
 (0)