@@ -400,7 +400,7 @@ use pyo3::{
400400} ;
401401
402402static ASYNCIO : OnceCell < PyObject > = OnceCell :: new ( ) ;
403- static CONTEXTVARS : OnceCell < Option < PyObject > > = OnceCell :: new ( ) ;
403+ static CONTEXTVARS : OnceCell < PyObject > = OnceCell :: new ( ) ;
404404static ENSURE_FUTURE : OnceCell < PyObject > = OnceCell :: new ( ) ;
405405static GET_RUNNING_LOOP : OnceCell < PyObject > = OnceCell :: new ( ) ;
406406
@@ -445,46 +445,27 @@ fn asyncio(py: Python) -> PyResult<&PyAny> {
445445/// Get a reference to the Python Event Loop from Rust
446446///
447447/// 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.
451448pub fn get_running_loop ( py : Python ) -> PyResult < & PyAny > {
452449 // Ideally should call get_running_loop, but calls get_event_loop for compatibility when
453450 // get_running_loop is not available.
454451 GET_RUNNING_LOOP
455452 . get_or_try_init ( || -> PyResult < PyObject > {
456453 let asyncio = asyncio ( py) ?;
457454
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 ( ) )
465456 } ) ?
466457 . as_ref ( py)
467458 . call0 ( )
468459}
469460
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) )
479465}
480466
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" ) ?)
488469}
489470
490471/// Task-local data to store for Python conversions.
@@ -520,12 +501,7 @@ impl TaskLocals {
520501
521502 /// Capture the current task's contextvars
522503 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) ?) )
529505 }
530506
531507 /// Get a reference to the event loop
@@ -596,12 +572,7 @@ fn call_soon_threadsafe(
596572 let py = event_loop. py ( ) ;
597573
598574 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) ?;
605576
606577 event_loop. call_method ( "call_soon_threadsafe" , args, Some ( kwargs) ) ?;
607578 Ok ( ( ) )
@@ -632,6 +603,7 @@ fn call_soon_threadsafe(
632603/// await asyncio.sleep(duration)
633604/// "#;
634605///
606+ /// # #[cfg(feature = "tokio-runtime")]
635607/// async fn py_sleep(seconds: f32) -> PyResult<()> {
636608/// let test_mod = Python::with_gil(|py| -> PyResult<PyObject> {
637609/// Ok(
@@ -646,8 +618,8 @@ fn call_soon_threadsafe(
646618/// })?;
647619///
648620/// 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)?,
651623/// test_mod
652624/// .call_method1(py, "py_sleep", (seconds.into_py(py),))?
653625/// .as_ref(py),
0 commit comments