Skip to content

Commit

Permalink
Merge pull request #340 from chrysn-pull-requests/where-is-the-python…
Browse files Browse the repository at this point in the history
…-error-from

python: Include line number in state error
  • Loading branch information
geonnave authored Feb 3, 2025
2 parents 0ba2ca4 + 3311f91 commit 30fdd30
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lakers-python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,16 @@ impl std::fmt::Display for StateMismatch {
}
}
impl From<StateMismatch> for PyErr {
#[track_caller]
fn from(err: StateMismatch) -> PyErr {
pyo3::exceptions::PyRuntimeError::new_err(err.to_string())
let location = std::panic::Location::caller();
// It would be nice to inject something more idiomatic on the Python side, eg. setting a
// cause with a Rust file and line number, but to create such an object we'd need the GIL,
// and that'd required doing a lot of things custom, eg. by creating a custom class where
// we pass that extra info in extra arguments, or re-implementing PyErr's lazy state (which
// we can't hook into because that's private) -- having the location in the text is the
// second best option.
pyo3::exceptions::PyRuntimeError::new_err(format!("{} (internally {})", err, location))
}
}

Expand Down

0 comments on commit 30fdd30

Please sign in to comment.