Skip to content

Commit c8ddbd2

Browse files
authored
Fix pow() raising ValueError instead of TypeError for non-int exponent (RustPython#7725)
* Validate pow() exponent type before zero-modulus check * Add tests for pow() exponent type check
1 parent 3ebcab7 commit c8ddbd2

2 files changed

Lines changed: 6 additions & 0 deletions

File tree

crates/vm/src/builtins/int.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,9 @@ impl PyInt {
395395
}
396396

397397
fn modpow(&self, other: PyObjectRef, modulus: PyObjectRef, vm: &VirtualMachine) -> PyResult {
398+
if other.downcast_ref::<Self>().is_none() {
399+
return Ok(vm.ctx.not_implemented());
400+
}
398401
let modulus = match modulus.downcast_ref::<Self>() {
399402
Some(val) => val.as_bigint(),
400403
None => return Ok(vm.ctx.not_implemented()),

extra_tests/snippets/builtin_pow.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
assert pow(2, -1, 5) == 3
2323
assert_raises(ValueError, pow, 2, 2, 0)
2424

25+
assert_raises(TypeError, pow, 1, None, 0)
26+
assert_raises(TypeError, pow, True, 1.5, False)
27+
2528

2629
assert_almost_equal = assert_equal
2730

0 commit comments

Comments
 (0)