Skip to content

Commit 3485237

Browse files
authored
Match CPython HKEY argument conversion (#9586)
Accept signed and unsigned pointer-width integer handles, reject index-only objects, and report the same None and overflow errors as CPython. Preserve the existing PyHKEY handle path. Assisted-by: dot:frontier-intelligence
1 parent 9ac57f5 commit 3485237

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

‎crates/vm/src/stdlib/winreg.rs‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,29 @@ mod winreg {
5757
if let Some(hkey_obj) = obj.downcast_ref::<PyHkey>() {
5858
return Ok(Self(hkey_obj.hkey.load()));
5959
}
60-
// Then try int
61-
let handle = usize::try_from_object(vm, obj)?;
60+
if vm.is_none(&obj) {
61+
return Err(vm.new_type_error("None is not a valid HKEY in this context"));
62+
}
63+
let Some(value) = obj.downcast_ref::<PyInt>() else {
64+
return Err(vm.new_type_error("The object is not a PyHKEY object"));
65+
};
66+
let value = value.as_bigint();
67+
let negative = value.sign() == Sign::Minus;
68+
let handle = if negative {
69+
value.to_isize().map(|v| v as usize)
70+
} else {
71+
value.to_usize()
72+
}
73+
.ok_or_else(|| {
74+
let message = if cfg!(target_pointer_width = "64") {
75+
"int too big to convert"
76+
} else if negative {
77+
"Python int too large to convert to C long"
78+
} else {
79+
"Python int too large to convert to C unsigned long"
80+
};
81+
vm.new_overflow_error(message)
82+
})?;
6283
Ok(Self(handle as host_winreg::HKEY))
6384
}
6485
}

0 commit comments

Comments
 (0)