diff --git a/vm/src/obj/objint.rs b/vm/src/obj/objint.rs index 88e0233b2..cfa5ba22b 100644 --- a/vm/src/obj/objint.rs +++ b/vm/src/obj/objint.rs @@ -9,12 +9,13 @@ use crate::function::{KwArgs, OptionalArg, PyFuncArgs}; use crate::pyhash; use crate::pyobject::{ IntoPyObject, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, - TypeProtocol, + TypeProtocol, IdProtocol, }; use crate::vm::VirtualMachine; use super::objbyteinner::PyByteInner; use super::objbytes::PyBytes; +use super::objint; use super::objstr::{PyString, PyStringRef}; use super::objtype; use crate::obj::objtype::PyClassRef; @@ -434,8 +435,29 @@ impl PyInt { zelf: PyRef, _precision: OptionalArg, _vm: &VirtualMachine, - ) -> PyIntRef { - zelf + ) -> PyResult { + let _ndigits = match _precision { + OptionalArg::Missing => None, + OptionalArg::Present(ref value) => { + if !_vm.get_none().is(value) { + if !objtype::isinstance(value, &_vm.ctx.int_type()) { + return Err(_vm.new_type_error(format!( + "'{}' object cannot be interpreted as an integer", + value.class().name + ))); + }; + // Only accept int type _ndigits + let _ndigits = objint::get_value(value); + Some(_ndigits) + } else { + return Err(_vm.new_type_error(format!( + "'{}' object cannot be interpreted as an integer", + value.class().name + ))); + } + } + }; + Ok(zelf) } #[pymethod(name = "__int__")]