-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce
FuncMetadata
to represent some information about a Python…
… function
- Loading branch information
Showing
7 changed files
with
121 additions
and
99 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
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
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,80 @@ | ||
use pyo3::prelude::*; | ||
|
||
// Captures some information about a Python function. | ||
#[derive(Debug, PartialEq)] | ||
pub struct FuncMetadata { | ||
pub name: String, | ||
pub is_coroutine: bool, | ||
pub num_args: usize, | ||
} | ||
|
||
// Returns `FuncMetadata` for given `func`. | ||
pub fn func_metadata(py: Python, func: &PyObject) -> PyResult<FuncMetadata> { | ||
let name = func.getattr(py, "__name__")?.extract::<String>(py)?; | ||
let is_coroutine = is_coroutine(py, func)?; | ||
let inspect = py.import("inspect")?; | ||
let args = inspect | ||
.call_method1("getargs", (func.getattr(py, "__code__")?,))? | ||
.getattr("args")? | ||
.extract::<Vec<String>>()?; | ||
Ok(FuncMetadata { | ||
name, | ||
is_coroutine, | ||
num_args: args.len(), | ||
}) | ||
} | ||
|
||
// Check if a Python function is a coroutine. Since the function has not run yet, | ||
// we cannot use `asyncio.iscoroutine()`, we need to use `inspect.iscoroutinefunction()`. | ||
fn is_coroutine(py: Python, func: &PyObject) -> PyResult<bool> { | ||
let inspect = py.import("inspect")?; | ||
// NOTE: that `asyncio.iscoroutine()` doesn't work here. | ||
inspect | ||
.call_method1("iscoroutinefunction", (func,))? | ||
.extract::<bool>() | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn function_metadata() -> PyResult<()> { | ||
Python::with_gil(|py| { | ||
let module = PyModule::from_code( | ||
py, | ||
r#" | ||
def regular_func(first_arg, second_arg): | ||
pass | ||
async def async_func(): | ||
pass | ||
"#, | ||
"", | ||
"", | ||
)?; | ||
|
||
let regular_func = module.getattr("regular_func")?.into_py(py); | ||
assert_eq!( | ||
FuncMetadata { | ||
name: "regular_func".to_string(), | ||
is_coroutine: false, | ||
num_args: 2, | ||
}, | ||
func_metadata(py, ®ular_func)? | ||
); | ||
|
||
let async_func = module.getattr("async_func")?.into_py(py); | ||
assert_eq!( | ||
FuncMetadata { | ||
name: "async_func".to_string(), | ||
is_coroutine: true, | ||
num_args: 0, | ||
}, | ||
func_metadata(py, &async_func)? | ||
); | ||
|
||
Ok(()) | ||
}) | ||
} | ||
} |