@@ -400,7 +400,7 @@ use pyo3::{
400
400
} ;
401
401
402
402
static ASYNCIO : OnceCell < PyObject > = OnceCell :: new ( ) ;
403
- static CONTEXTVARS : OnceCell < Option < PyObject > > = OnceCell :: new ( ) ;
403
+ static CONTEXTVARS : OnceCell < PyObject > = OnceCell :: new ( ) ;
404
404
static ENSURE_FUTURE : OnceCell < PyObject > = OnceCell :: new ( ) ;
405
405
static GET_RUNNING_LOOP : OnceCell < PyObject > = OnceCell :: new ( ) ;
406
406
@@ -445,46 +445,27 @@ fn asyncio(py: Python) -> PyResult<&PyAny> {
445
445
/// Get a reference to the Python Event Loop from Rust
446
446
///
447
447
/// Equivalent to `asyncio.get_running_loop()` in Python 3.7+.
448
- /// > For Python 3.6, this function falls back to `asyncio.get_event_loop()` which has slightly
449
- /// different behaviour. See the [`asyncio.get_event_loop`](https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.get_event_loop)
450
- /// docs to better understand the differences.
451
448
pub fn get_running_loop ( py : Python ) -> PyResult < & PyAny > {
452
449
// Ideally should call get_running_loop, but calls get_event_loop for compatibility when
453
450
// get_running_loop is not available.
454
451
GET_RUNNING_LOOP
455
452
. get_or_try_init ( || -> PyResult < PyObject > {
456
453
let asyncio = asyncio ( py) ?;
457
454
458
- if asyncio. hasattr ( "get_running_loop" ) ? {
459
- // correct behaviour with Python 3.7+
460
- Ok ( asyncio. getattr ( "get_running_loop" ) ?. into ( ) )
461
- } else {
462
- // Python 3.6 compatibility mode
463
- Ok ( asyncio. getattr ( "get_event_loop" ) ?. into ( ) )
464
- }
455
+ Ok ( asyncio. getattr ( "get_running_loop" ) ?. into ( ) )
465
456
} ) ?
466
457
. as_ref ( py)
467
458
. call0 ( )
468
459
}
469
460
470
- /// Returns None only if contextvars cannot be imported (Python 3.6 fallback)
471
- fn contextvars ( py : Python ) -> Option < & PyAny > {
472
- CONTEXTVARS
473
- . get_or_init ( || match py. import ( "contextvars" ) {
474
- Ok ( contextvars) => Some ( contextvars. into ( ) ) ,
475
- Err ( _) => None ,
476
- } )
477
- . as_ref ( )
478
- . map ( |contextvars| contextvars. as_ref ( py) )
461
+ fn contextvars ( py : Python ) -> PyResult < & PyAny > {
462
+ Ok ( CONTEXTVARS
463
+ . get_or_try_init ( || py. import ( "contextvars" ) . map ( |m| m. into ( ) ) ) ?
464
+ . as_ref ( py) )
479
465
}
480
466
481
- /// Returns Ok(None) only if contextvars cannot be imported (Python 3.6 fallback)
482
- fn copy_context ( py : Python ) -> PyResult < Option < & PyAny > > {
483
- if let Some ( contextvars) = contextvars ( py) {
484
- Ok ( Some ( contextvars. call_method0 ( "copy_context" ) ?) )
485
- } else {
486
- Ok ( None )
487
- }
467
+ fn copy_context ( py : Python ) -> PyResult < & PyAny > {
468
+ Ok ( contextvars ( py) ?. call_method0 ( "copy_context" ) ?)
488
469
}
489
470
490
471
/// Task-local data to store for Python conversions.
@@ -520,12 +501,7 @@ impl TaskLocals {
520
501
521
502
/// Capture the current task's contextvars
522
503
pub fn copy_context ( self , py : Python ) -> PyResult < Self > {
523
- // No-op if context cannot be copied (Python 3.6 fallback)
524
- if let Some ( cx) = copy_context ( py) ? {
525
- Ok ( self . with_context ( cx) )
526
- } else {
527
- Ok ( self )
528
- }
504
+ Ok ( self . with_context ( copy_context ( py) ?) )
529
505
}
530
506
531
507
/// Get a reference to the event loop
@@ -596,12 +572,7 @@ fn call_soon_threadsafe(
596
572
let py = event_loop. py ( ) ;
597
573
598
574
let kwargs = PyDict :: new ( py) ;
599
-
600
- // Accommodate for the Python 3.6 fallback
601
- // (call_soon_threadsafe does not support the context kwarg in 3.6)
602
- if !context. is_none ( ) {
603
- kwargs. set_item ( "context" , context) ?;
604
- }
575
+ kwargs. set_item ( "context" , context) ?;
605
576
606
577
event_loop. call_method ( "call_soon_threadsafe" , args, Some ( kwargs) ) ?;
607
578
Ok ( ( ) )
@@ -632,6 +603,7 @@ fn call_soon_threadsafe(
632
603
/// await asyncio.sleep(duration)
633
604
/// "#;
634
605
///
606
+ /// # #[cfg(feature = "tokio-runtime")]
635
607
/// async fn py_sleep(seconds: f32) -> PyResult<()> {
636
608
/// let test_mod = Python::with_gil(|py| -> PyResult<PyObject> {
637
609
/// Ok(
@@ -646,8 +618,8 @@ fn call_soon_threadsafe(
646
618
/// })?;
647
619
///
648
620
/// Python::with_gil(|py| {
649
- /// pyo3_asyncio::into_future_with_loop (
650
- /// pyo3_asyncio::get_running_loop (py)?,
621
+ /// pyo3_asyncio::into_future_with_locals (
622
+ /// & pyo3_asyncio::tokio::get_current_locals (py)?,
651
623
/// test_mod
652
624
/// .call_method1(py, "py_sleep", (seconds.into_py(py),))?
653
625
/// .as_ref(py),
0 commit comments