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

Function code_page_encode

crates/vm/src/stdlib/_codecs.rs:1048–1077  ·  view source on GitHub ↗
(
        args: CodePageEncodeArgs,
        vm: &VirtualMachine,
    )

Source from the content-addressed store, hash-verified

1046
1047 #[pyfunction]
1048 fn code_page_encode(
1049 args: CodePageEncodeArgs,
1050 vm: &VirtualMachine,
1051 ) -> PyResult<(Vec<u8>, usize)> {
1052 use crate::common::windows::ToWideString;
1053
1054 if args.code_page < 0 {
1055 return Err(vm.new_value_error("invalid code page number"));
1056 }
1057 let errors = args.errors.as_ref().map(|s| s.as_str()).unwrap_or("strict");
1058 let code_page = args.code_page as u32;
1059 let char_len = args.s.char_len();
1060
1061 if char_len == 0 {
1062 return Ok((Vec::new(), 0));
1063 }
1064
1065 let encoding_name = code_page_encoding_name(code_page);
1066
1067 // Fast path: try encoding the whole string at once (only if no surrogates)
1068 if let Some(str_data) = args.s.to_str() {
1069 let wide: Vec<u16> = std::ffi::OsStr::new(str_data).to_wide();
1070 if let Some(result) = try_encode_code_page_strict(code_page, &wide, vm)? {
1071 return Ok((result, char_len));
1072 }
1073 }
1074
1075 // Slow path: character by character with error handling
1076 encode_code_page_errors(code_page, &args.s, errors, &encoding_name, vm)
1077 }
1078
1079 #[derive(FromArgs)]
1080 struct CodePageDecodeArgs {

Callers 3

encodeFunction · 0.90
encodeMethod · 0.90
encodeMethod · 0.90

Calls 11

newFunction · 0.85
code_page_encoding_nameFunction · 0.85
encode_code_page_errorsFunction · 0.85
to_strMethod · 0.80
to_wideMethod · 0.80
ErrClass · 0.50
mapMethod · 0.45
as_refMethod · 0.45
as_strMethod · 0.45
char_lenMethod · 0.45

Tested by

no test coverage detected