From 4f005bd0e604ab495a13625cfc1ae69c4537a84a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20Vask=C3=B3?= <1771332+vlaci@users.noreply.github.com> Date: Fri, 27 Jan 2023 13:40:12 +0100 Subject: [PATCH] demangle: raise ValueError on string formatting issues as well Resolves #8 --- src/lib.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 0e87693..6a38222 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 { - 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(())