-
Notifications
You must be signed in to change notification settings - Fork 760
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
always normalize exceptions before raising
- Loading branch information
1 parent
aeb7a95
commit 3310b33
Showing
7 changed files
with
214 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fix `IterNextOutput::Return` not returning a value on PyPy. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
hypothesis>=3.55 | ||
pytest>=6.0 | ||
pytest-asyncio>=0.21 | ||
pytest-benchmark>=3.4 | ||
psutil>=5.6 | ||
typing_extensions>=4.0.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
//! The following classes are examples of objects which implement Python's | ||
//! awaitable protocol. | ||
//! | ||
//! Both IterAwaitable and FutureAwaitable will return a value immediately | ||
//! when awaited, see guide examples related to pyo3-asyncio for ways | ||
//! to suspend tasks and await results. | ||
|
||
use pyo3::{prelude::*, pyclass::IterNextOutput}; | ||
|
||
#[pyclass] | ||
#[derive(Debug)] | ||
pub(crate) struct IterAwaitable { | ||
result: Option<PyResult<PyObject>>, | ||
} | ||
|
||
#[pymethods] | ||
impl IterAwaitable { | ||
#[new] | ||
fn new(result: PyObject) -> Self { | ||
IterAwaitable { | ||
result: Some(Ok(result)), | ||
} | ||
} | ||
|
||
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())), | ||
} | ||
} | ||
} | ||
|
||
#[pyclass] | ||
pub(crate) struct FutureAwaitable { | ||
#[pyo3(get, set, name = "_asyncio_future_blocking")] | ||
py_block: bool, | ||
result: Option<PyResult<PyObject>>, | ||
} | ||
|
||
#[pymethods] | ||
impl FutureAwaitable { | ||
#[new] | ||
fn new(result: PyObject) -> Self { | ||
FutureAwaitable { | ||
py_block: false, | ||
result: Some(Ok(result)), | ||
} | ||
} | ||
|
||
fn __await__(pyself: PyRef<'_, Self>) -> PyRef<'_, Self> { | ||
pyself | ||
} | ||
|
||
fn __iter__(pyself: PyRef<'_, Self>) -> PyRef<'_, Self> { | ||
pyself | ||
} | ||
|
||
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)), | ||
} | ||
} | ||
} | ||
|
||
#[pymodule] | ||
pub fn awaitable(_py: Python<'_>, m: &PyModule) -> PyResult<()> { | ||
m.add_class::<IterAwaitable>()?; | ||
m.add_class::<FutureAwaitable>()?; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import pytest | ||
|
||
from pyo3_pytests.awaitable import IterAwaitable, FutureAwaitable | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_iter_awaitable(): | ||
assert await IterAwaitable(5) == 5 | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_future_awaitable(): | ||
assert await FutureAwaitable(5) == 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters