forked from M-Labs/nac3
1
0
Fork 0
nac3/nac3embedded/src/lib.rs

25 lines
773 B
Rust
Raw Normal View History

2020-12-18 10:09:35 +08:00
use pyo3::prelude::*;
use pyo3::wrap_pyfunction;
2020-12-19 00:52:06 +08:00
use pyo3::exceptions;
use rustpython_parser::parser;
2020-12-18 10:09:35 +08:00
#[pyfunction]
2020-12-18 23:44:45 +08:00
fn add_host_object(obj: PyObject) -> PyResult<()> {
Python::with_gil(|py| -> PyResult<()> {
let obj: &PyAny = obj.extract(py)?;
let inspect = PyModule::import(py, "inspect")?;
let source = inspect.call1("getsource", (obj.get_type(), ))?;
2020-12-19 00:52:06 +08:00
let ast = parser::parse_program(source.extract()?).map_err(|e|
exceptions::PySyntaxError::new_err(format!("failed to parse host object source: {}", e)))?;
println!("{:?}", ast);
2020-12-18 23:44:45 +08:00
Ok(())
})?;
Ok(())
2020-12-18 10:09:35 +08:00
}
#[pymodule]
2020-12-18 23:44:45 +08:00
fn nac3embedded(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(add_host_object, m)?)?;
2020-12-18 10:09:35 +08:00
Ok(())
}