Weird behaviour with awaitable Rust structs and PyPy #3439
-
Hi all 👋 I recently encountered some weird behaviour with PyPy and Rust structs implementing await behaviour, as tracked in this issue: emmett-framework/granian#120 To give you a bit of context, I have two Rust structs which behave as Python awaitables, in particular one implementing what in #[pyclass]
pub(crate) struct PyIterAwaitable {
result: Option<PyResult<PyObject>>
}
#[pymethods]
impl PyIterAwaitable {
fn __await__(pyself: PyRef<'_, Self>) -> PyRef<'_, Self> {
pyself
}
fn __iter__(pyself: PyRef<'_, Self>) -> PyRef<'_, Self> {
pyself
}
fn __next__(&mut self, py: Python) -> PyResult<IterNextOutput<PyObject, PyObject>> {
match self.result.take() {
Some(res) => {
match res {
Ok(v) => Ok(IterNextOutput::Return(v)),
Err(err) => Err(err)
}
},
_ => Ok(IterNextOutput::Yield(py.None()))
}
}
} and one emulating an #[pyclass]
pub(crate) struct PyFutureAwaitable {
py_block: bool,
result: Option<PyResult<PyObject>>
}
#[pymethods]
impl PyFutureAwaitable {
fn __await__(pyself: PyRef<'_, Self>) -> PyRef<'_, Self> {
pyself
}
fn __iter__(pyself: PyRef<'_, Self>) -> PyRef<'_, Self> {
pyself
}
#[getter(_asyncio_future_blocking)]
fn get_block(&self) -> bool {
self.py_block
}
#[setter(_asyncio_future_blocking)]
fn set_block(&mut self, val: bool) {
self.py_block = val
}
fn __next__(
mut pyself: PyRefMut<'_, Self>
) -> PyResult<IterNextOutput<PyRefMut<'_, Self>, PyObject>> {
match pyself.result {
Some(_) => {
match pyself.result.take().unwrap() {
Ok(v) => Ok(IterNextOutput::Return(v)),
Err(err) => Err(err)
}
},
_ => Ok(IterNextOutput::Yield(pyself))
}
}
} Now, both implementations correctly works in CPython, so the value returned with an Has anybody experienced this? I'm not sure is a PyO3 issue, but I'm not a PyPy expert, thus I have no clues on what's going on here :/ |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hey, thanks for sharing this. I don't have a reason why PyPy should behave any differently, so it feels like a bug. I'll try to investigate later. Might be related to #3190 |
Beta Was this translation helpful? Give feedback.
Confirmed as a bug, #3455 should fix it in the next release.