# HG changeset patch # User Raphaël Gomès # Date 2019-10-07 21:17:44 # Node ID 97097897557438e6bda24934a5d059d383b5aa93 # Parent 98d996a138defcb43e3f5bc27f159c5b277bd3ee rust-utils: introduce a debug util to print the python stack trace Differential Revision: https://phab.mercurial-scm.org/D7057 diff --git a/rust/hg-cpython/src/lib.rs b/rust/hg-cpython/src/lib.rs --- a/rust/hg-cpython/src/lib.rs +++ b/rust/hg-cpython/src/lib.rs @@ -35,6 +35,7 @@ pub mod discovery; pub mod exceptions; pub mod filepatterns; pub mod parsers; +pub mod utils; py_module_initializer!(rustext, initrustext, PyInit_rustext, |py, m| { m.add( diff --git a/rust/hg-cpython/src/utils.rs b/rust/hg-cpython/src/utils.rs new file mode 100644 --- /dev/null +++ b/rust/hg-cpython/src/utils.rs @@ -0,0 +1,13 @@ +use cpython::{PyDict, PyObject, PyResult, PyTuple, Python}; + +#[allow(unused)] +pub fn print_python_trace(py: Python) -> PyResult { + eprintln!("==============================="); + eprintln!("Printing Python stack from Rust"); + eprintln!("==============================="); + let traceback = py.import("traceback")?; + let sys = py.import("sys")?; + let kwargs = PyDict::new(py); + kwargs.set_item(py, "file", sys.get(py, "stderr")?)?; + traceback.call(py, "print_stack", PyTuple::new(py, &[]), Some(&kwargs)) +}