Patchwork D7057: rust-utils: introduce a debug util to print the python stack trace

login
register
mail settings
Submitter phabricator
Date Oct. 11, 2019, 11:43 a.m.
Message ID <differential-rev-PHID-DREV-24yg57jndmqfbvnkxshs-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/42216/
State Superseded
Headers show

Comments

phabricator - Oct. 11, 2019, 11:43 a.m.
Alphare created this revision.
Herald added subscribers: mercurial-devel, kevincox, durin42.
Herald added a reviewer: hg-reviewers.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D7057

AFFECTED FILES
  rust/hg-cpython/src/lib.rs
  rust/hg-cpython/src/utils.rs

CHANGE DETAILS




To: Alphare, #hg-reviewers
Cc: durin42, kevincox, mercurial-devel
phabricator - Oct. 11, 2019, 11:53 a.m.
gracinet added a comment.


  nice tool for debugging sessions, especially for obscure broken tests.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST ACTION
  https://phab.mercurial-scm.org/D7057/new/

REVISION DETAIL
  https://phab.mercurial-scm.org/D7057

To: Alphare, #hg-reviewers
Cc: gracinet, durin42, kevincox, mercurial-devel

Patch

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<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))
+}
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 exceptions;
 pub mod filepatterns;
 pub mod parsers;
+pub mod utils;
 
 py_module_initializer!(rustext, initrustext, PyInit_rustext, |py, m| {
     m.add(