Skip to content

Commit 2b8d079

Browse files
committed
trying new python string handling
1 parent 7a56e73 commit 2b8d079

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

python/src/lib.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ fn ontology_location_from_py(location: &Bound<'_, PyAny>) -> PyResult<ResolvedLo
5353
})
5454
.map_err(anyhow_to_pyerr);
5555
}
56-
let fspath: String = fspath_obj.str()?.to_str()?.to_owned();
56+
let fspath: String = bound_pystring_to_string(fspath_obj.str()?)?;
5757
return OntologyLocation::from_str(&fspath)
5858
.map(|loc| ResolvedLocation {
5959
location: loc,
@@ -64,7 +64,7 @@ fn ontology_location_from_py(location: &Bound<'_, PyAny>) -> PyResult<ResolvedLo
6464

6565
if let Ok(base_attr) = location.getattr("base") {
6666
if !base_attr.is_none() {
67-
let base: String = base_attr.str()?.to_str()?.to_owned();
67+
let base: String = bound_pystring_to_string(base_attr.str()?)?;
6868
if !base.is_empty() {
6969
if let Ok(loc) = OntologyLocation::from_str(&base) {
7070
return Ok(ResolvedLocation {
@@ -78,7 +78,7 @@ fn ontology_location_from_py(location: &Bound<'_, PyAny>) -> PyResult<ResolvedLo
7878

7979
if let Ok(identifier_attr) = location.getattr("identifier") {
8080
if !identifier_attr.is_none() {
81-
let identifier_str: String = identifier_attr.str()?.to_str()?.to_owned();
81+
let identifier_str: String = bound_pystring_to_string(identifier_attr.str()?)?;
8282
if !identifier_str.is_empty()
8383
&& (identifier_str.starts_with("file:") || Path::new(&identifier_str).exists())
8484
{
@@ -102,7 +102,7 @@ fn ontology_location_from_py(location: &Bound<'_, PyAny>) -> PyResult<ResolvedLo
102102
});
103103
}
104104

105-
let as_string: String = location.str()?.to_str()?.to_owned();
105+
let as_string: String = bound_pystring_to_string(location.str()?)?;
106106

107107
if as_string.starts_with("file:") || Path::new(&as_string).exists() {
108108
return OntologyLocation::from_str(&as_string)
@@ -147,7 +147,7 @@ fn extract_ontology_subject(graph: &Bound<'_, PyAny>) -> PyResult<Option<String>
147147

148148
if let Some(first_res) = iterator.next() {
149149
let first = first_res?;
150-
let subject_str = first.str()?.to_str()?.to_owned();
150+
let subject_str = bound_pystring_to_string(first.str()?)?;
151151
if !subject_str.is_empty() {
152152
return Ok(Some(subject_str));
153153
}
@@ -249,6 +249,10 @@ fn term_to_python<'a>(
249249
Ok(res)
250250
}
251251

252+
fn bound_pystring_to_string(py_str: Bound<'_, PyString>) -> PyResult<String> {
253+
Ok(py_str.to_cow()?.into_owned())
254+
}
255+
252256
/// Run the Rust CLI implementation and return its process-style exit code.
253257
#[pyfunction]
254258
#[cfg(feature = "cli")]

0 commit comments

Comments
 (0)