MCPcopy Index your code
hub / github.com/RustPython/RustPython / try_from_object

Method try_from_object

crates/stdlib/src/pystruct.rs:28–44  ·  view source on GitHub ↗
(vm: &VirtualMachine, obj: PyObjectRef)

Source from the content-addressed store, hash-verified

26
27 impl TryFromObject for IntoStructFormatBytes {
28 fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
29 // CPython turns str to bytes but we do reversed way here
30 // The only performance difference is this transition cost
31 let fmt = match_class!(match obj {
32 s @ PyStr => s.isascii().then_some(s),
33 b @ PyBytes => ascii::AsciiStr::from_ascii(&b)
34 .ok()
35 .map(|s| vm.ctx.new_str(s)),
36 other =>
37 return Err(vm.new_type_error(format!(
38 "Struct() argument 1 must be a str or bytes object, not {}",
39 other.class().name()
40 ))),
41 })
42 .ok_or_else(|| vm.new_unicode_decode_error("Struct format must be a ascii string"))?;
43 Ok(Self(fmt))
44 }
45 }
46
47 impl IntoStructFormatBytes {

Callers

nothing calls this directly

Calls 2

SelfFunction · 0.85
ok_or_elseMethod · 0.80

Tested by

no test coverage detected