Fix pow() raising ValueError instead of TypeError for non-int exponent (#7725)

* Validate pow() exponent type before zero-modulus check

* Add tests for pow() exponent type check
This commit is contained in:
Jeongseop Lim
2026-04-29 18:18:11 +09:00
committed by GitHub
parent 3ebcab70c0
commit c8ddbd2326
2 changed files with 6 additions and 0 deletions

View File

@@ -395,6 +395,9 @@ impl PyInt {
}
fn modpow(&self, other: PyObjectRef, modulus: PyObjectRef, vm: &VirtualMachine) -> PyResult {
if other.downcast_ref::<Self>().is_none() {
return Ok(vm.ctx.not_implemented());
}
let modulus = match modulus.downcast_ref::<Self>() {
Some(val) => val.as_bigint(),
None => return Ok(vm.ctx.not_implemented()),

View File

@@ -22,6 +22,9 @@ assert_raises(TypeError, pow, 2.0, 4, 5)
assert pow(2, -1, 5) == 3
assert_raises(ValueError, pow, 2, 2, 0)
assert_raises(TypeError, pow, 1, None, 0)
assert_raises(TypeError, pow, True, 1.5, False)
assert_almost_equal = assert_equal