Skip to content

Commit 70a22ab

Browse files
committed
Use isize directly
1 parent be4a3f3 commit 70a22ab

4 files changed

Lines changed: 16 additions & 33 deletions

File tree

vm/src/obj/objbytearray.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
use std::cell::{Cell, RefCell};
33
use std::convert::TryFrom;
44

5-
use num_traits::ToPrimitive;
6-
75
use super::objbyteinner::{
86
ByteInnerExpandtabsOptions, ByteInnerFindOptions, ByteInnerNewOptions, ByteInnerPaddingOptions,
97
ByteInnerPosition, ByteInnerSplitOptions, ByteInnerSplitlinesOptions,
@@ -506,18 +504,13 @@ impl PyByteArrayRef {
506504
}
507505

508506
#[pymethod(name = "insert")]
509-
fn insert(self, index: PyIntRef, x: PyIntRef, vm: &VirtualMachine) -> PyResult<()> {
507+
fn insert(self, mut index: isize, x: PyIntRef, vm: &VirtualMachine) -> PyResult<()> {
510508
let bytes = &mut self.inner.borrow_mut().elements;
511509
let len = isize::try_from(bytes.len())
512510
.map_err(|_e| vm.new_overflow_error("bytearray too big".to_string()))?;
513511

514512
let x = x.as_bigint().byte_or(vm)?;
515513

516-
let mut index = index
517-
.as_bigint()
518-
.to_isize()
519-
.ok_or_else(|| vm.new_overflow_error("index too big".to_string()))?;
520-
521514
if index >= len {
522515
bytes.push(x);
523516
return Ok(());
@@ -550,17 +543,17 @@ impl PyByteArrayRef {
550543
}
551544

552545
#[pymethod(name = "__mul__")]
553-
fn repeat(self, n: PyIntRef, vm: &VirtualMachine) -> PyResult {
546+
fn repeat(self, n: isize, vm: &VirtualMachine) -> PyResult {
554547
Ok(vm.ctx.new_bytearray(self.inner.borrow().repeat(n, vm)?))
555548
}
556549

557550
#[pymethod(name = "__rmul__")]
558-
fn rmul(self, n: PyIntRef, vm: &VirtualMachine) -> PyResult {
551+
fn rmul(self, n: isize, vm: &VirtualMachine) -> PyResult {
559552
self.repeat(n, vm)
560553
}
561554

562555
#[pymethod(name = "__imul__")]
563-
fn irepeat(self, n: PyIntRef, vm: &VirtualMachine) -> PyResult<()> {
556+
fn irepeat(self, n: isize, vm: &VirtualMachine) -> PyResult<()> {
564557
self.inner.borrow_mut().irepeat(n, vm)
565558
}
566559

vm/src/obj/objbyteinner.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,16 +1073,12 @@ impl PyByteInner {
10731073
res
10741074
}
10751075

1076-
pub fn repeat(&self, n: PyIntRef, vm: &VirtualMachine) -> PyResult<Vec<u8>> {
1076+
pub fn repeat(&self, n: isize, _vm: &VirtualMachine) -> PyResult<Vec<u8>> {
10771077
if self.elements.is_empty() {
10781078
// We can multiple an empty vector by any integer, even if it doesn't fit in an isize.
10791079
return Ok(vec![]);
10801080
}
10811081

1082-
let n = n.as_bigint().to_isize().ok_or_else(|| {
1083-
vm.new_overflow_error("can't multiply bytes that many times".to_string())
1084-
})?;
1085-
10861082
if n <= 0 {
10871083
Ok(vec![])
10881084
} else {
@@ -1097,16 +1093,12 @@ impl PyByteInner {
10971093
}
10981094
}
10991095

1100-
pub fn irepeat(&mut self, n: PyIntRef, vm: &VirtualMachine) -> PyResult<()> {
1096+
pub fn irepeat(&mut self, n: isize, _vm: &VirtualMachine) -> PyResult<()> {
11011097
if self.elements.is_empty() {
11021098
// We can multiple an empty vector by any integer, even if it doesn't fit in an isize.
11031099
return Ok(());
11041100
}
11051101

1106-
let n = n.as_bigint().to_isize().ok_or_else(|| {
1107-
vm.new_overflow_error("can't multiply bytes that many times".to_string())
1108-
})?;
1109-
11101102
if n <= 0 {
11111103
self.elements.clear();
11121104
} else {

vm/src/obj/objbytes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,12 +421,12 @@ impl PyBytesRef {
421421
}
422422

423423
#[pymethod(name = "__mul__")]
424-
fn repeat(self, n: PyIntRef, vm: &VirtualMachine) -> PyResult {
424+
fn repeat(self, n: isize, vm: &VirtualMachine) -> PyResult {
425425
Ok(vm.ctx.new_bytes(self.inner.repeat(n, vm)?))
426426
}
427427

428428
#[pymethod(name = "__rmul__")]
429-
fn rmul(self, n: PyIntRef, vm: &VirtualMachine) -> PyResult {
429+
fn rmul(self, n: isize, vm: &VirtualMachine) -> PyResult {
430430
self.repeat(n, vm)
431431
}
432432

vm/src/obj/objstr.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -292,22 +292,18 @@ impl PyString {
292292
}
293293

294294
#[pymethod(name = "__mul__")]
295-
fn mul(&self, val: PyObjectRef, vm: &VirtualMachine) -> PyResult<String> {
296-
if !objtype::isinstance(&val, &vm.ctx.int_type()) {
297-
return Err(vm.new_type_error(format!("Cannot multiply {} and {}", self, val)));
298-
}
299-
objint::get_value(&val)
300-
.to_isize()
301-
.map(|multiplier| multiplier.max(0))
302-
.and_then(|multiplier| multiplier.to_usize())
295+
fn mul(&self, multiplier: isize, vm: &VirtualMachine) -> PyResult<String> {
296+
multiplier
297+
.max(0)
298+
.to_usize()
303299
.map(|multiplier| self.value.repeat(multiplier))
304300
.ok_or_else(|| {
305301
vm.new_overflow_error("cannot fit 'int' into an index-sized integer".to_string())
306302
})
307303
}
308304

309305
#[pymethod(name = "__rmul__")]
310-
fn rmul(&self, val: PyObjectRef, vm: &VirtualMachine) -> PyResult<String> {
306+
fn rmul(&self, val: isize, vm: &VirtualMachine) -> PyResult<String> {
311307
self.mul(val, vm)
312308
}
313309

@@ -1358,7 +1354,9 @@ fn try_update_quantity_from_tuple(
13581354
Ok(tuple_index)
13591355
}
13601356
}
1361-
None => Err(vm.new_type_error("not enough arguments for format string".to_string())),
1357+
None => {
1358+
Err(vm.new_type_error("not enough arguments for format string".to_string()))
1359+
}
13621360
}
13631361
}
13641362
_ => Ok(tuple_index),

0 commit comments

Comments
 (0)