diff --git a/vm/src/obj/objbytearray.rs b/vm/src/obj/objbytearray.rs index 47300e3b0..29194142a 100644 --- a/vm/src/obj/objbytearray.rs +++ b/vm/src/obj/objbytearray.rs @@ -2,8 +2,6 @@ use std::cell::{Cell, RefCell}; use std::convert::TryFrom; -use num_traits::ToPrimitive; - use super::objbyteinner::{ ByteInnerExpandtabsOptions, ByteInnerFindOptions, ByteInnerNewOptions, ByteInnerPaddingOptions, ByteInnerPosition, ByteInnerSplitOptions, ByteInnerSplitlinesOptions, @@ -506,18 +504,13 @@ impl PyByteArrayRef { } #[pymethod(name = "insert")] - fn insert(self, index: PyIntRef, x: PyIntRef, vm: &VirtualMachine) -> PyResult<()> { + fn insert(self, mut index: isize, x: PyIntRef, vm: &VirtualMachine) -> PyResult<()> { let bytes = &mut self.inner.borrow_mut().elements; let len = isize::try_from(bytes.len()) .map_err(|_e| vm.new_overflow_error("bytearray too big".to_string()))?; let x = x.as_bigint().byte_or(vm)?; - let mut index = index - .as_bigint() - .to_isize() - .ok_or_else(|| vm.new_overflow_error("index too big".to_string()))?; - if index >= len { bytes.push(x); return Ok(()); @@ -550,17 +543,17 @@ impl PyByteArrayRef { } #[pymethod(name = "__mul__")] - fn repeat(self, n: PyIntRef, vm: &VirtualMachine) -> PyResult { + fn repeat(self, n: isize, vm: &VirtualMachine) -> PyResult { Ok(vm.ctx.new_bytearray(self.inner.borrow().repeat(n, vm)?)) } #[pymethod(name = "__rmul__")] - fn rmul(self, n: PyIntRef, vm: &VirtualMachine) -> PyResult { + fn rmul(self, n: isize, vm: &VirtualMachine) -> PyResult { self.repeat(n, vm) } #[pymethod(name = "__imul__")] - fn irepeat(self, n: PyIntRef, vm: &VirtualMachine) -> PyResult<()> { + fn irepeat(self, n: isize, vm: &VirtualMachine) -> PyResult<()> { self.inner.borrow_mut().irepeat(n, vm) } diff --git a/vm/src/obj/objbyteinner.rs b/vm/src/obj/objbyteinner.rs index 30d21db99..5b6ca53af 100644 --- a/vm/src/obj/objbyteinner.rs +++ b/vm/src/obj/objbyteinner.rs @@ -1073,16 +1073,12 @@ impl PyByteInner { res } - pub fn repeat(&self, n: PyIntRef, vm: &VirtualMachine) -> PyResult> { + pub fn repeat(&self, n: isize, _vm: &VirtualMachine) -> PyResult> { if self.elements.is_empty() { // We can multiple an empty vector by any integer, even if it doesn't fit in an isize. return Ok(vec![]); } - let n = n.as_bigint().to_isize().ok_or_else(|| { - vm.new_overflow_error("can't multiply bytes that many times".to_string()) - })?; - if n <= 0 { Ok(vec![]) } else { @@ -1097,16 +1093,12 @@ impl PyByteInner { } } - pub fn irepeat(&mut self, n: PyIntRef, vm: &VirtualMachine) -> PyResult<()> { + pub fn irepeat(&mut self, n: isize, _vm: &VirtualMachine) -> PyResult<()> { if self.elements.is_empty() { // We can multiple an empty vector by any integer, even if it doesn't fit in an isize. return Ok(()); } - let n = n.as_bigint().to_isize().ok_or_else(|| { - vm.new_overflow_error("can't multiply bytes that many times".to_string()) - })?; - if n <= 0 { self.elements.clear(); } else { diff --git a/vm/src/obj/objbytes.rs b/vm/src/obj/objbytes.rs index 2aa532c87..41b329d75 100644 --- a/vm/src/obj/objbytes.rs +++ b/vm/src/obj/objbytes.rs @@ -421,12 +421,12 @@ impl PyBytesRef { } #[pymethod(name = "__mul__")] - fn repeat(self, n: PyIntRef, vm: &VirtualMachine) -> PyResult { + fn repeat(self, n: isize, vm: &VirtualMachine) -> PyResult { Ok(vm.ctx.new_bytes(self.inner.repeat(n, vm)?)) } #[pymethod(name = "__rmul__")] - fn rmul(self, n: PyIntRef, vm: &VirtualMachine) -> PyResult { + fn rmul(self, n: isize, vm: &VirtualMachine) -> PyResult { self.repeat(n, vm) } diff --git a/vm/src/obj/objstr.rs b/vm/src/obj/objstr.rs index 7b06adadf..0eded6e76 100644 --- a/vm/src/obj/objstr.rs +++ b/vm/src/obj/objstr.rs @@ -292,14 +292,10 @@ impl PyString { } #[pymethod(name = "__mul__")] - fn mul(&self, val: PyObjectRef, vm: &VirtualMachine) -> PyResult { - if !objtype::isinstance(&val, &vm.ctx.int_type()) { - return Err(vm.new_type_error(format!("Cannot multiply {} and {}", self, val))); - } - objint::get_value(&val) - .to_isize() - .map(|multiplier| multiplier.max(0)) - .and_then(|multiplier| multiplier.to_usize()) + fn mul(&self, multiplier: isize, vm: &VirtualMachine) -> PyResult { + multiplier + .max(0) + .to_usize() .map(|multiplier| self.value.repeat(multiplier)) .ok_or_else(|| { vm.new_overflow_error("cannot fit 'int' into an index-sized integer".to_string()) @@ -307,7 +303,7 @@ impl PyString { } #[pymethod(name = "__rmul__")] - fn rmul(&self, val: PyObjectRef, vm: &VirtualMachine) -> PyResult { + fn rmul(&self, val: isize, vm: &VirtualMachine) -> PyResult { self.mul(val, vm) } @@ -1358,7 +1354,9 @@ fn try_update_quantity_from_tuple( Ok(tuple_index) } } - None => Err(vm.new_type_error("not enough arguments for format string".to_string())), + None => { + Err(vm.new_type_error("not enough arguments for format string".to_string())) + } } } _ => Ok(tuple_index),