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

Method resolve_encoding

crates/vm/src/stdlib/_io.rs:2841–2890  ·  view source on GitHub ↗
(
            encoding: Option<PyUtf8StrRef>,
            vm: &VirtualMachine,
        )

Source from the content-addressed store, hash-verified

2839 }
2840
2841 fn resolve_encoding(
2842 encoding: Option<PyUtf8StrRef>,
2843 vm: &VirtualMachine,
2844 ) -> PyResult<PyUtf8StrRef> {
2845 // Note: Do not issue EncodingWarning here. The warning should only
2846 // be issued by io.text_encoding(), the public API. This function
2847 // is used internally (e.g., for stdin/stdout/stderr initialization)
2848 // where no warning should be emitted.
2849 let encoding = match encoding {
2850 None if vm.state.config.settings.utf8_mode > 0 => {
2851 identifier_utf8!(vm, utf_8).to_owned()
2852 }
2853 Some(enc) if enc.as_str() == "locale" => match vm.import("locale", 0) {
2854 Ok(locale) => locale
2855 .get_attr("getencoding", vm)?
2856 .call((), vm)?
2857 .try_into_value(vm)?,
2858 Err(err)
2859 if err.fast_isinstance(vm.ctx.exceptions.import_error)
2860 || err.fast_isinstance(vm.ctx.exceptions.module_not_found_error) =>
2861 {
2862 identifier_utf8!(vm, utf_8).to_owned()
2863 }
2864 Err(err) => return Err(err),
2865 },
2866 Some(enc) => {
2867 if enc.as_str().contains('\0') {
2868 return Err(cstring_error(vm));
2869 }
2870 enc
2871 }
2872 _ => match vm.import("locale", 0) {
2873 Ok(locale) => locale
2874 .get_attr("getencoding", vm)?
2875 .call((), vm)?
2876 .try_into_value(vm)?,
2877 Err(err)
2878 if err.fast_isinstance(vm.ctx.exceptions.import_error)
2879 || err.fast_isinstance(vm.ctx.exceptions.module_not_found_error) =>
2880 {
2881 identifier_utf8!(vm, utf_8).to_owned()
2882 }
2883 Err(err) => return Err(err),
2884 },
2885 };
2886 if encoding.as_str().contains('\0') {
2887 return Err(cstring_error(vm));
2888 }
2889 Ok(encoding)
2890 }
2891
2892 fn adjust_encoder_state_for_bom(
2893 encoder: &PyObjectRef,

Callers

nothing calls this directly

Calls 10

cstring_errorFunction · 0.85
try_into_valueMethod · 0.80
fast_isinstanceMethod · 0.80
ErrClass · 0.50
to_ownedMethod · 0.45
as_strMethod · 0.45
importMethod · 0.45
callMethod · 0.45
get_attrMethod · 0.45
containsMethod · 0.45

Tested by

no test coverage detected