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

Function py2reg

crates/vm/src/stdlib/winreg.rs:953–1038  ·  view source on GitHub ↗
(value: PyObjectRef, typ: u32, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

951 }
952
953 fn py2reg(value: PyObjectRef, typ: u32, vm: &VirtualMachine) -> PyResult<Option<Vec<u8>>> {
954 match typ {
955 REG_DWORD => {
956 if vm.is_none(&value) {
957 return Ok(Some(0u32.to_le_bytes().to_vec()));
958 }
959 let val = value
960 .downcast_ref::<PyInt>()
961 .ok_or_else(|| vm.new_type_error("value must be an integer"))?;
962 let bigint = val.as_bigint();
963 // Check for negative value - raise OverflowError
964 if bigint.sign() == Sign::Minus {
965 return Err(vm.new_overflow_error("int too big to convert"));
966 }
967 let val = bigint
968 .to_u32()
969 .ok_or_else(|| vm.new_overflow_error("int too big to convert"))?;
970 Ok(Some(val.to_le_bytes().to_vec()))
971 }
972 REG_QWORD => {
973 if vm.is_none(&value) {
974 return Ok(Some(0u64.to_le_bytes().to_vec()));
975 }
976 let val = value
977 .downcast_ref::<PyInt>()
978 .ok_or_else(|| vm.new_type_error("value must be an integer"))?;
979 let bigint = val.as_bigint();
980 // Check for negative value - raise OverflowError
981 if bigint.sign() == Sign::Minus {
982 return Err(vm.new_overflow_error("int too big to convert"));
983 }
984 let val = bigint
985 .to_u64()
986 .ok_or_else(|| vm.new_overflow_error("int too big to convert"))?;
987 Ok(Some(val.to_le_bytes().to_vec()))
988 }
989 REG_SZ | REG_EXPAND_SZ => {
990 if vm.is_none(&value) {
991 // Return empty string as UTF-16 null terminator
992 return Ok(Some(vec![0u8, 0u8]));
993 }
994 let s = value
995 .downcast::<PyStr>()
996 .map_err(|_| vm.new_type_error("value must be a string"))?;
997 let wide = s.as_wtf8().to_wide_with_nul();
998 // Convert Vec<u16> to Vec<u8>
999 let bytes: Vec<u8> = wide.iter().flat_map(|&c| c.to_le_bytes()).collect();
1000 Ok(Some(bytes))
1001 }
1002 REG_MULTI_SZ => {
1003 if vm.is_none(&value) {
1004 // Empty list = double null terminator
1005 return Ok(Some(vec![0u8, 0u8, 0u8, 0u8]));
1006 }
1007 let list = value
1008 .downcast::<crate::builtins::PyList>()
1009 .map_err(|_| vm.new_type_error("value must be a list of strings"))?;
1010

Callers 1

SetValueExFunction · 0.85

Calls 15

newFunction · 0.85
to_vecMethod · 0.80
ok_or_elseMethod · 0.80
as_bigintMethod · 0.80
to_wide_with_nulMethod · 0.80
collectMethod · 0.80
borrow_vecMethod · 0.80
SomeClass · 0.50
ErrClass · 0.50
is_noneMethod · 0.45
to_u32Method · 0.45
as_wtf8Method · 0.45

Tested by

no test coverage detected