(vm: &VirtualMachine, obj: PyObjectRef)
| 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 { |
nothing calls this directly
no test coverage detected