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

Function oem_decode

crates/vm/src/stdlib/_codecs.rs:666–750  ·  view source on GitHub ↗
(args: OemDecodeArgs, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

664
665 #[pyfunction]
666 fn oem_decode(args: OemDecodeArgs, vm: &VirtualMachine) -> PyResult<(String, usize)> {
667 use windows_sys::Win32::Globalization::{
668 CP_OEMCP, MB_ERR_INVALID_CHARS, MultiByteToWideChar,
669 };
670
671 let _errors = args.errors.as_ref().map(|s| s.as_str()).unwrap_or("strict");
672 let data = args.data.borrow_buf();
673 let len = data.len();
674
675 if data.is_empty() {
676 return Ok((String::new(), 0));
677 }
678
679 // Get the required buffer size for UTF-16
680 let size = unsafe {
681 MultiByteToWideChar(
682 CP_OEMCP,
683 MB_ERR_INVALID_CHARS,
684 data.as_ptr().cast(),
685 len as i32,
686 core::ptr::null_mut(),
687 0,
688 )
689 };
690
691 if size == 0 {
692 // Try without MB_ERR_INVALID_CHARS for non-strict mode (replacement behavior)
693 let size = unsafe {
694 MultiByteToWideChar(
695 CP_OEMCP,
696 0,
697 data.as_ptr().cast(),
698 len as i32,
699 core::ptr::null_mut(),
700 0,
701 )
702 };
703 if size == 0 {
704 let err = std::io::Error::last_os_error();
705 return Err(vm.new_os_error(format!("oem_decode failed: {}", err)));
706 }
707
708 let mut buffer = vec![0u16; size as usize];
709 let result = unsafe {
710 MultiByteToWideChar(
711 CP_OEMCP,
712 0,
713 data.as_ptr().cast(),
714 len as i32,
715 buffer.as_mut_ptr(),
716 size,
717 )
718 };
719 if result == 0 {
720 let err = std::io::Error::last_os_error();
721 return Err(vm.new_os_error(format!("oem_decode failed: {}", err)));
722 }
723 buffer.truncate(result as usize);

Callers 1

decodeFunction · 0.90

Calls 13

newFunction · 0.85
new_os_errorMethod · 0.80
as_mut_ptrMethod · 0.80
ErrClass · 0.50
mapMethod · 0.45
as_refMethod · 0.45
as_strMethod · 0.45
borrow_bufMethod · 0.45
lenMethod · 0.45
is_emptyMethod · 0.45
castMethod · 0.45
as_ptrMethod · 0.45

Tested by

no test coverage detected