-
Notifications
You must be signed in to change notification settings - Fork 778
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add coroutine
__name__
/__qualname__
and not-awaited warning
- Loading branch information
Showing
6 changed files
with
180 additions
and
16 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 @@ | ||
Add `__name__`/`__qualname__` attributes to `Coroutine`, as well as a Python warning when the coroutine is dropped without having been awaited |
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
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,19 +1,36 @@ | ||
use crate::coroutine::Coroutine; | ||
use crate::impl_::wrap::OkWrap; | ||
use crate::{IntoPy, PyErr, PyObject, Python}; | ||
use std::future::Future; | ||
|
||
/// Used to wrap the result of async `#[pyfunction]` and `#[pymethods]`. | ||
pub fn wrap_future<F, R, T>(future: F) -> Coroutine | ||
use crate::{ | ||
coroutine::Coroutine, | ||
types::{PyModule, PyString}, | ||
IntoPy, PyClass, PyErr, PyObject, Python, | ||
}; | ||
|
||
pub fn new_coroutine<F, T, E>(name: &PyString, qualname: &PyString, future: F) -> Coroutine | ||
where | ||
F: Future<Output = R> + Send + 'static, | ||
R: OkWrap<T>, | ||
F: Future<Output = Result<T, E>> + Send + 'static, | ||
T: IntoPy<PyObject>, | ||
PyErr: From<R::Error>, | ||
PyErr: From<E>, | ||
{ | ||
let future = async move { | ||
// SAFETY: GIL is acquired when future is polled (see `Coroutine::poll`) | ||
future.await.wrap(unsafe { Python::assume_gil_acquired() }) | ||
Coroutine::new(Some(name.into()), Some(qualname.into()), future) | ||
} | ||
|
||
pub fn coroutine_qualname<'py>( | ||
py: Python<'py>, | ||
module: Option<&PyModule>, | ||
name: &str, | ||
) -> &'py PyString { | ||
match module.and_then(|m| m.name().ok()) { | ||
Some(module) => PyString::new(py, &format!("{}.{}", module, name)), | ||
None => PyString::new(py, name), | ||
} | ||
} | ||
|
||
pub fn method_coroutine_qualname<'py, T: PyClass>(py: Python<'py>, name: &str) -> &'py PyString { | ||
let class = T::NAME; | ||
let qualname = match T::MODULE { | ||
Some(module) => format!("{}.{}.{}", module, class, name), | ||
None => format!("{}.{}", class, name), | ||
}; | ||
Coroutine::from_future(future) | ||
PyString::new(py, &qualname) | ||
} |
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