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

Method adjust_encoder_state_for_bom

crates/vm/src/stdlib/_io.rs:2892–2916  ·  view source on GitHub ↗
(
            encoder: &PyObjectRef,
            encoding: &str,
            buffer: &PyObject,
            vm: &VirtualMachine,
        )

Source from the content-addressed store, hash-verified

2890 }
2891
2892 fn adjust_encoder_state_for_bom(
2893 encoder: &PyObjectRef,
2894 encoding: &str,
2895 buffer: &PyObject,
2896 vm: &VirtualMachine,
2897 ) -> PyResult<()> {
2898 let needs_bom = matches!(encoding, "utf-8-sig" | "utf-16" | "utf-32");
2899 if !needs_bom {
2900 return Ok(());
2901 }
2902 let seekable = vm.call_method(buffer, "seekable", ())?.try_to_bool(vm)?;
2903 if !seekable {
2904 return Ok(());
2905 }
2906 let pos = vm.call_method(buffer, "tell", ())?;
2907 if vm.bool_eq(&pos, vm.ctx.new_int(0).as_ref())? {
2908 return Ok(());
2909 }
2910 if let Err(err) = vm.call_method(encoder, "setstate", (0,))
2911 && !err.fast_isinstance(vm.ctx.exceptions.attribute_error)
2912 {
2913 return Err(err);
2914 }
2915 Ok(())
2916 }
2917
2918 #[allow(clippy::type_complexity)]
2919 fn find_coder(

Callers

nothing calls this directly

Calls 7

try_to_boolMethod · 0.80
bool_eqMethod · 0.80
fast_isinstanceMethod · 0.80
ErrClass · 0.50
call_methodMethod · 0.45
as_refMethod · 0.45
new_intMethod · 0.45

Tested by

no test coverage detected