Skip to content

Commit

Permalink
Merge pull request #9 from vlaci/master
Browse files Browse the repository at this point in the history
demangle: raise ValueError on string formatting issues as well
  • Loading branch information
benfred authored May 1, 2023
2 parents 6df7e35 + 4f005bd commit 61d4587
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,24 @@ use pyo3::exceptions;
/// >>> demangle('invalid c++ symbol')
/// Traceback (most recent call last):
/// ...
/// ValueError: mangled symbol is not well-formed
/// ValueError: ('Could not demangle symbol', 'mangled symbol is not well-formed')
#[pymodule]
fn cpp_demangle(_py: Python, m: &PyModule) -> PyResult<()> {
// This adds a function to the python module:
/// Demangles a mangled c++ linker symbol name and returns it as a string
#[pyfn(m)]
fn demangle(mangled: String) -> PyResult<String> {
match ::cpp_demangle::Symbol::new(&mangled[..]) {
// Return the output as a string to Python
Ok(sym) => Ok(sym.to_string()),
let symbol = ::cpp_demangle::Symbol::new(&mangled[..]).map_err(|error| {
exceptions::PyValueError::new_err(("Could not demangle symbol", error.to_string()))
})?;
let demangled = symbol.demangle(&Default::default()).map_err(|error| {
exceptions::PyValueError::new_err((
"Could not format demangled name as string",
error.to_string(),
))
})?;

// on an error, this will raise a python ValueError exception!
Err(error) => return Err(exceptions::PyValueError::new_err(error.to_string()))
}
Ok(demangled)
}

Ok(())
Expand Down

0 comments on commit 61d4587

Please sign in to comment.