rust-utils: introduce a debug util to print the python stack trace
authorRaphaël Gomès <rgomes@octobus.net>
Mon, 07 Oct 2019 23:17:44 +0200
changeset 43251 970978975574
parent 43250 98d996a138de
child 43252 32187ae9eeb3
rust-utils: introduce a debug util to print the python stack trace Differential Revision: https://phab.mercurial-scm.org/D7057
rust/hg-cpython/src/lib.rs
rust/hg-cpython/src/utils.rs
--- a/rust/hg-cpython/src/lib.rs	Wed Oct 16 17:16:23 2019 +0300
+++ b/rust/hg-cpython/src/lib.rs	Mon Oct 07 23:17:44 2019 +0200
@@ -35,6 +35,7 @@
 pub mod exceptions;
 pub mod filepatterns;
 pub mod parsers;
+pub mod utils;
 
 py_module_initializer!(rustext, initrustext, PyInit_rustext, |py, m| {
     m.add(
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rust/hg-cpython/src/utils.rs	Mon Oct 07 23:17:44 2019 +0200
@@ -0,0 +1,13 @@
+use cpython::{PyDict, PyObject, PyResult, PyTuple, Python};
+
+#[allow(unused)]
+pub fn print_python_trace(py: Python) -> PyResult<PyObject> {
+    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))
+}