| 799 | |
| 800 | #[pyfunction] |
| 801 | fn factorial(x: PyIntRef, vm: &VirtualMachine) -> PyResult<BigInt> { |
| 802 | // Check for negative before overflow - negative values are always invalid |
| 803 | if x.as_bigint().is_negative() { |
| 804 | return Err(vm.new_value_error("factorial() not defined for negative values")); |
| 805 | } |
| 806 | let n: i64 = x.try_to_primitive(vm).map_err(|_| { |
| 807 | vm.new_overflow_error("factorial() argument should not exceed 9223372036854775807") |
| 808 | })?; |
| 809 | pymath::math::integer::factorial(n) |
| 810 | .map(|r| r.into()) |
| 811 | .map_err(|_| vm.new_value_error("factorial() not defined for negative values")) |
| 812 | } |
| 813 | |
| 814 | #[pyfunction] |
| 815 | fn perm( |