I have a function that accepts a Python list. I expect the list to be made of strings. How do I extract them?
fn f(list: &PyList) -> PyResult<SomeClass> {
for obj in list.iter() {
let rust_string = PyString::from_object(obj, "ASCII", "strict")?.to_string()?;
// fails with `TypeError: decoding str is not supported`
if PyString::is_exact_instance(obj) {
let py_str: PyString = unsafe {std::mem::transmute(str)};
let rust_str = py_str.to_string()?;
// panics with failed assertion in PyString.as_bytes()
}
}
// ...
}
// Python call site
f(["string1", "string2"])
Software versions: Python 3.7, nightly Rust 1.33.0, pyo3 0.5.2.