https://pantsbuild.org/ logo
h

hundreds-father-404

03/15/2021, 8:16 PM
I'm playing with replacing our
native.py
Externs.create_exception()
method with instead using CPython's
eval(code: &str)
mechanism, which will reduce FFI boilerplate. But I'm stuck on escaping of quotes. I'm writing
py.eval(&format!("Exception(\"{}\")", msg)
, and it results in a syntax error if
msg
has
"
in it. We need to modify
msg
so that
"
is transformed into
\"
. I'm not finding a good way to do this in Rust - the best I've thought of is to map over
.chars()
and create a new
String
?
f

fast-nail-55400

03/15/2021, 8:43 PM
Copy code
Example: return Err(PyErr::new::<exc::TypeError, _>(py, "Error message"));
h

hundreds-father-404

03/16/2021, 1:53 AM
We need the wrapping
Exception()
for the sake of generating a string But the above problem is solved by using
PyModule.call()
instead of
eval()
. Yay!