python_ast/ast/dump/mod.rs
1use pyo3::prelude::*;
2
3/// A wrapper for the Python ast.dump function. This is a convenience function for dumping the AST
4/// to the terminal.
5pub fn dump(o: &PyAny, indent: Option<u8>) -> PyResult<String> {
6 let pymodule_code = include_str!("__init__.py");
7
8 Python::with_gil(|py| -> PyResult<String> {
9 // We want to call tokenize.tokenize from Python.
10 let pymodule = PyModule::from_code_bound(py, pymodule_code, "dump.py", "parser")?;
11 let t = pymodule.getattr("dump")?;
12 assert!(t.is_callable());
13 let args = (o, indent);
14
15 t.call1(args)?.extract()
16 })
17}