-
Notifications
You must be signed in to change notification settings - Fork 763
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
Joseph Perez
committed
Nov 20, 2023
1 parent
49ae17c
commit d29b9cd
Showing
5 changed files
with
169 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
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,41 @@ | ||
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, Py, PyClass, PyErr, PyObject, Python, | ||
}; | ||
|
||
pub fn new_coroutine<F, T, E>( | ||
name: Option<Py<PyString>>, | ||
qualname: Option<Py<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(name, qualname, future) | ||
} | ||
|
||
pub fn coroutine_name(py: Python<'_>, name: &str) -> Py<PyString> { | ||
PyString::new(py, name).into() | ||
} | ||
|
||
pub unsafe fn coroutine_qualname( | ||
py: Python<'_>, | ||
module: Option<&PyModule>, | ||
name: &str, | ||
) -> Option<Py<PyString>> { | ||
Some(PyString::new(py, &format!("{}.{name}", module?.name().ok()?)).into()) | ||
} | ||
|
||
pub fn method_coroutine_qualname<T: PyClass>(py: Python<'_>, 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).into() | ||
} |
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