13
13
//! features = ["unstable-streams"]
14
14
//! ```
15
15
16
- use std:: { any:: Any , cell:: RefCell , future:: Future , panic:: AssertUnwindSafe , pin:: Pin } ;
17
-
18
16
use async_std:: task;
19
17
use futures:: FutureExt ;
20
18
use pyo3:: prelude:: * ;
19
+ use std:: { any:: Any , cell:: RefCell , future:: Future , panic:: AssertUnwindSafe , pin:: Pin } ;
21
20
22
21
use crate :: {
23
22
generic:: { self , ContextExt , JoinError , LocalContextExt , Runtime , SpawnLocalExt } ,
@@ -34,13 +33,13 @@ pub mod re_exports {
34
33
35
34
/// <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
36
35
#[ 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;
38
37
39
38
/// <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>
40
39
/// <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>
41
40
/// Registers an `async-std` test with the `pyo3-asyncio` test harness
42
41
#[ 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;
44
43
45
44
struct AsyncStdJoinErr ( Box < dyn Any + Send + ' static > ) ;
46
45
@@ -71,7 +70,7 @@ impl Runtime for AsyncStdRuntime {
71
70
AssertUnwindSafe ( fut)
72
71
. catch_unwind ( )
73
72
. await
74
- . map_err ( |e| AsyncStdJoinErr ( e ) )
73
+ . map_err ( AsyncStdJoinErr )
75
74
} )
76
75
}
77
76
}
@@ -90,10 +89,13 @@ impl ContextExt for AsyncStdRuntime {
90
89
}
91
90
92
91
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 ( )
97
99
}
98
100
}
99
101
@@ -236,13 +238,13 @@ where
236
238
/// via [`into_future`] (new behaviour in `v0.15`).
237
239
///
238
240
/// > 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.
241
243
/// >
242
244
/// > 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.
246
248
///
247
249
/// # Arguments
248
250
/// * `py` - PyO3 GIL guard
@@ -291,13 +293,13 @@ where
291
293
/// via [`into_future`] (new behaviour in `v0.15`).
292
294
///
293
295
/// > 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.
296
298
/// >
297
299
/// > 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.
301
303
///
302
304
/// # Arguments
303
305
/// * `py` - The current PyO3 GIL guard
@@ -337,13 +339,13 @@ where
337
339
/// via [`into_future`] (new behaviour in `v0.15`).
338
340
///
339
341
/// > 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.
342
344
/// >
343
345
/// > 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.
347
349
///
348
350
/// # Arguments
349
351
/// * `py` - PyO3 GIL guard
@@ -412,13 +414,13 @@ where
412
414
/// via [`into_future`] (new behaviour in `v0.15`).
413
415
///
414
416
/// > 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.
417
419
/// >
418
420
/// > 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.
422
424
///
423
425
/// # Arguments
424
426
/// * `py` - The current PyO3 GIL guard
@@ -573,8 +575,8 @@ pub fn into_future(
573
575
/// # fn main() {}
574
576
/// ```
575
577
#[ 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 > ,
578
580
) -> PyResult < impl futures:: Stream < Item = PyResult < PyObject > > + ' static > {
579
581
generic:: into_stream_v1 :: < AsyncStdRuntime > ( gen)
580
582
}
@@ -633,9 +635,9 @@ pub fn into_stream_v1<'p>(
633
635
/// # fn main() {}
634
636
/// ```
635
637
#[ cfg( feature = "unstable-streams" ) ]
636
- pub fn into_stream_with_locals_v1 < ' p > (
638
+ pub fn into_stream_with_locals_v1 (
637
639
locals : TaskLocals ,
638
- gen : Bound < ' p , PyAny > ,
640
+ gen : Bound < ' _ , PyAny > ,
639
641
) -> PyResult < impl futures:: Stream < Item = PyResult < PyObject > > + ' static > {
640
642
generic:: into_stream_with_locals_v1 :: < AsyncStdRuntime > ( locals, gen)
641
643
}
@@ -694,9 +696,9 @@ pub fn into_stream_with_locals_v1<'p>(
694
696
/// # fn main() {}
695
697
/// ```
696
698
#[ cfg( feature = "unstable-streams" ) ]
697
- pub fn into_stream_with_locals_v2 < ' p > (
699
+ pub fn into_stream_with_locals_v2 (
698
700
locals : TaskLocals ,
699
- gen : Bound < ' p , PyAny > ,
701
+ gen : Bound < ' _ , PyAny > ,
700
702
) -> PyResult < impl futures:: Stream < Item = PyObject > + ' static > {
701
703
generic:: into_stream_with_locals_v2 :: < AsyncStdRuntime > ( locals, gen)
702
704
}
@@ -751,8 +753,8 @@ pub fn into_stream_with_locals_v2<'p>(
751
753
/// # fn main() {}
752
754
/// ```
753
755
#[ 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 > ,
756
758
) -> PyResult < impl futures:: Stream < Item = PyObject > + ' static > {
757
759
generic:: into_stream_v2 :: < AsyncStdRuntime > ( gen)
758
760
}
0 commit comments