Skip to content

Commit

Permalink
tidy up some uses of Bound<'_, PyString>::to_str
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Nov 19, 2024
1 parent 6472887 commit ff9f426
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 11 deletions.
11 changes: 3 additions & 8 deletions src/serializers/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ pub(crate) fn infer_to_python_known(
v.into_py(py)
}
ObType::Decimal => value.to_string().into_py(py),
ObType::StrSubclass => value.downcast::<PyString>()?.to_str()?.into_py(py),
ObType::StrSubclass => PyString::new_bound(py, value.downcast::<PyString>()?.to_str()?).into(),
ObType::Bytes => extra
.config
.bytes_mode
Expand Down Expand Up @@ -609,16 +609,11 @@ pub(crate) fn infer_json_key_known<'a>(
}
ObType::Decimal => Ok(Cow::Owned(key.to_string())),
ObType::Bool => super::type_serializers::simple::bool_json_key(key),
ObType::Str | ObType::StrSubclass => {
let py_str = key.downcast::<PyString>()?;
Ok(Cow::Owned(py_str.to_str()?.to_string()))
}
ObType::Str | ObType::StrSubclass => key.downcast::<PyString>()?.to_cow(),
ObType::Bytes => extra
.config
.bytes_mode
.bytes_to_string(key.py(), key.downcast::<PyBytes>()?.as_bytes())
// FIXME it would be nice to have a "PyCow" which carries ownership of the Python type too
.map(|s| Cow::Owned(s.into_owned())),
.bytes_to_string(key.py(), key.downcast::<PyBytes>()?.as_bytes()),
ObType::Bytearray => {
let py_byte_array = key.downcast::<PyByteArray>()?;
// Safety: the GIL is held while serialize_bytes is running; it doesn't run
Expand Down
5 changes: 2 additions & 3 deletions src/serializers/type_serializers/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,15 @@ impl BuildSerializer for FormatSerializer {
) -> PyResult<CombinedSerializer> {
let py = schema.py();
let formatting_string: Bound<'_, PyString> = schema.get_as_req(intern!(py, "formatting_string"))?;
let formatting_string = formatting_string.to_str()?;
if formatting_string.is_empty() {
if formatting_string.is_empty()? {
ToStringSerializer::build(schema, config, definitions)
} else {
Ok(Self {
format_func: py
.import_bound(intern!(py, "builtins"))?
.getattr(intern!(py, "format"))?
.into_py(py),
formatting_string: PyString::new_bound(py, formatting_string).into(),
formatting_string: formatting_string.unbind(),
when_used: WhenUsed::new(schema, WhenUsed::JsonUnlessNone)?,
}
.into())
Expand Down

0 comments on commit ff9f426

Please sign in to comment.