1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
use pyo3::prelude::*;

/// Accepts any Python object and dumps it using the Python ast module.
pub fn dump(o: &PyAny, indent: Option<u8>) -> PyResult<String> {

    let pymodule_code = include_str!("__init__.py");

    Python::with_gil(|py| -> PyResult<String> {
        // We want to call tokenize.tokenize from Python.
        let pymodule = PyModule::from_code(py, pymodule_code, "dump.py", "parser")?;
        let t = pymodule.getattr("dump")?;
        assert!(t.is_callable());
        let args = (o, indent);

        Ok(t.call1(args)?.extract()?)
    })

}