Skip to content

Commit bdddc49

Browse files
author
Andrew J Westlake
committed
Removed 3.6 fallbacks / workarounds
1 parent 3b570e0 commit bdddc49

File tree

5 files changed

+22
-60
lines changed

5 files changed

+22
-60
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ jobs:
140140
run: cargo build --features=${{env.features}} --verbose --target ${{ matrix.platform.rust-target }}
141141

142142
# uvloop doesn't compile under Windows or Python 3.11-dev
143-
- if: ${{ matrix.platform.os != 'windows-latest' && matrix.python-version != "3.11-dev" }}
143+
- if: ${{ matrix.platform.os != 'windows-latest' && matrix.python-version != '3.11-dev' }}
144144
name: Install pyo3-asyncio test dependencies
145145
run: |
146146
python -m pip install -U uvloop

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -656,10 +656,12 @@ There have been a few changes to the API in order to support proper cancellation
656656
let event_loop = pyo3_asyncio::get_running_loop(py)?;
657657

658658
// *_with_loop conversions in 0.14
659-
let fut = pyo3_asyncio::tokio::future_into_py_with_loop(
660-
event_loop,
661-
async move { Ok(Python::with_gil(|py| py.None())) }
662-
)?;
659+
//
660+
// let fut = pyo3_asyncio::tokio::future_into_py_with_loop(
661+
// event_loop,
662+
// async move { Ok(Python::with_gil(|py| py.None())) }
663+
// )?;
664+
//
663665
// should be replaced with *_with_locals in 0.15
664666
//
665667
// contextvars can be copied with `copy_context` or supplied

pytests/test_async_std_asyncio.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -300,15 +300,9 @@ asyncio.run(main())
300300
#[pyo3_asyncio::async_std::test]
301301
fn test_contextvars() -> PyResult<()> {
302302
Python::with_gil(|py| {
303-
let contextvars = match py.import("contextvars") {
304-
Ok(contextvars) => contextvars,
305-
// Python 3.6 does not support contextvars, so early-out here
306-
Err(_) => return Ok(()),
307-
};
308-
309303
let d = [
310304
("asyncio", py.import("asyncio")?.into()),
311-
("contextvars", contextvars.into()),
305+
("contextvars", py.import("contextvars")?.into()),
312306
("cvars_mod", wrap_pymodule!(cvars_mod)(py)),
313307
]
314308
.into_py_dict(py);

pytests/tokio_asyncio/mod.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -304,15 +304,9 @@ asyncio.run(main())
304304
#[pyo3_asyncio::tokio::test]
305305
fn test_contextvars() -> PyResult<()> {
306306
Python::with_gil(|py| {
307-
let contextvars = match py.import("contextvars") {
308-
Ok(contextvars) => contextvars,
309-
// Python 3.6 does not support contextvars, so early-out here
310-
Err(_) => return Ok(()),
311-
};
312-
313307
let d = [
314308
("asyncio", py.import("asyncio")?.into()),
315-
("contextvars", contextvars.into()),
309+
("contextvars", py.import("contextvars")?.into()),
316310
("cvars_mod", wrap_pymodule!(cvars_mod)(py)),
317311
]
318312
.into_py_dict(py);

src/lib.rs

Lines changed: 13 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ use pyo3::{
400400
};
401401

402402
static ASYNCIO: OnceCell<PyObject> = OnceCell::new();
403-
static CONTEXTVARS: OnceCell<Option<PyObject>> = OnceCell::new();
403+
static CONTEXTVARS: OnceCell<PyObject> = OnceCell::new();
404404
static ENSURE_FUTURE: OnceCell<PyObject> = OnceCell::new();
405405
static 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.
451448
pub 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

Comments
 (0)