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

Function encode_code_page_errors

crates/vm/src/stdlib/_codecs.rs:872–1045  ·  view source on GitHub ↗

Encode character by character with error handling.

(
        code_page: u32,
        s: &PyStrRef,
        errors: &str,
        encoding_name: &str,
        vm: &VirtualMachine,
    )

Source from the content-addressed store, hash-verified

870
871 /// Encode character by character with error handling.
872 fn encode_code_page_errors(
873 code_page: u32,
874 s: &PyStrRef,
875 errors: &str,
876 encoding_name: &str,
877 vm: &VirtualMachine,
878 ) -> PyResult<(Vec<u8>, usize)> {
879 use crate::builtins::{PyBytes, PyStr, PyTuple};
880 use windows_sys::Win32::Globalization::WideCharToMultiByte;
881
882 let char_len = s.char_len();
883 let flags = encode_code_page_flags(code_page, errors);
884 let use_default_char = code_page != 65001 && code_page != 65000;
885 let encoding_str = vm.ctx.new_str(encoding_name);
886 let reason_str = vm.ctx.new_str("invalid character");
887
888 // For strict mode, find the first unencodable character and raise
889 if errors == "strict" {
890 // Find the failing position by trying each character
891 let mut fail_pos = 0;
892 for cp in s.as_wtf8().code_points() {
893 let ch = cp.to_u32();
894 if (0xD800..=0xDFFF).contains(&ch) {
895 break;
896 }
897 let mut wchars = [0u16; 2];
898 let wchar_len = if ch < 0x10000 {
899 wchars[0] = ch as u16;
900 1
901 } else {
902 wchars[0] = ((ch - 0x10000) >> 10) as u16 + 0xD800;
903 wchars[1] = ((ch - 0x10000) & 0x3FF) as u16 + 0xDC00;
904 2
905 };
906 let mut used_default_char: i32 = 0;
907 let pused = if use_default_char {
908 &mut used_default_char as *mut i32
909 } else {
910 core::ptr::null_mut()
911 };
912 let outsize = unsafe {
913 WideCharToMultiByte(
914 code_page,
915 flags,
916 wchars.as_ptr(),
917 wchar_len,
918 core::ptr::null_mut(),
919 0,
920 core::ptr::null(),
921 pused,
922 )
923 };
924 if outsize <= 0 || (use_default_char && used_default_char != 0) {
925 break;
926 }
927 fail_pos += 1;
928 }
929 return Err(vm.new_unicode_encode_error_real(

Callers 1

code_page_encodeFunction · 0.85

Calls 15

encode_code_page_flagsFunction · 0.85
newFunction · 0.85
code_pointsMethod · 0.80
lookup_errorMethod · 0.80
collectMethod · 0.80
as_mut_ptrMethod · 0.80
ok_or_elseMethod · 0.80
downcast_refMethod · 0.80
try_into_valueMethod · 0.80
ErrClass · 0.50
char_lenMethod · 0.45

Tested by

no test coverage detected