Skip to content

Commit 2d1158b

Browse files
committed
remove vm.try_index
1 parent a05712f commit 2d1158b

File tree

19 files changed

+56
-40
lines changed

19 files changed

+56
-40
lines changed

stdlib/src/bisect.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ mod _bisect {
2525
vm: &VirtualMachine,
2626
) -> PyResult<Option<isize>> {
2727
arg.into_option()
28-
.map(|v| vm.to_index(&v)?.try_to_primitive(vm))
28+
.map(|v| v.try_index(vm)?.try_to_primitive(vm))
2929
.transpose()
3030
}
3131

stdlib/src/math.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ mod math {
199199

200200
#[pyfunction]
201201
fn isqrt(x: PyObjectRef, vm: &VirtualMachine) -> PyResult<BigInt> {
202-
let index = vm.to_index(&x)?;
202+
let index = x.try_index(vm)?;
203203
let value = index.as_bigint();
204204

205205
if value.is_negative() {

stdlib/src/select.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ mod decl {
347347
Some(ms) => {
348348
let ms = if let Some(float) = ms.payload::<PyFloat>() {
349349
float.to_f64().to_i32()
350-
} else if let Some(int) = vm.to_index_opt(ms.clone()) {
350+
} else if let Some(int) = ms.try_index_opt(vm) {
351351
int?.as_bigint().to_i32()
352352
} else {
353353
return Err(vm.new_type_error(format!(

stdlib/src/socket.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -734,8 +734,8 @@ mod _socket {
734734
if obj.fast_isinstance(vm.ctx.types.float_type) {
735735
return Err(vm.new_type_error("integer argument expected, got float".to_owned()));
736736
}
737-
let int = vm
738-
.to_index_opt(obj)
737+
let int = obj
738+
.try_index_opt(vm)
739739
.unwrap_or_else(|| Err(vm.new_type_error("an integer is required".to_owned())))?;
740740
int.try_to_primitive::<CastFrom>(vm)
741741
.map(|sock| sock as RawSocket)
@@ -1382,7 +1382,7 @@ mod _socket {
13821382
let (flags, address) = match arg3 {
13831383
OptionalArg::Present(arg3) => {
13841384
// should just be i32::try_from_obj but tests check for error message
1385-
let int = vm.to_index_opt(arg2).unwrap_or_else(|| {
1385+
let int = arg2.try_index_opt(vm).unwrap_or_else(|| {
13861386
Err(vm.new_type_error("an integer is required".to_owned()))
13871387
})?;
13881388
let flags = int.try_to_primitive::<i32>(vm)?;

vm/src/buffer.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -491,15 +491,15 @@ fn get_int_or_index<T>(vm: &VirtualMachine, arg: PyObjectRef) -> PyResult<T>
491491
where
492492
T: PrimInt + for<'a> TryFrom<&'a BigInt>,
493493
{
494-
match vm.to_index_opt(arg) {
495-
Some(index) => index?
496-
.try_to_primitive(vm)
497-
.map_err(|_| new_struct_error(vm, "argument out of range".to_owned())),
498-
None => Err(new_struct_error(
494+
let index = arg.try_index_opt(vm).unwrap_or_else(|| {
495+
Err(new_struct_error(
499496
vm,
500497
"required argument is not an integer".to_owned(),
501-
)),
502-
}
498+
))
499+
})?;
500+
index
501+
.try_to_primitive(vm)
502+
.map_err(|_| new_struct_error(vm, "argument out of range".to_owned()))
503503
}
504504

505505
make_pack_primint!(i8);

vm/src/builtins/int.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl PyPayload for PyInt {
6969
}
7070

7171
fn special_retrieve(vm: &VirtualMachine, obj: &PyObject) -> Option<PyResult<PyRef<Self>>> {
72-
Some(vm.to_index(obj))
72+
Some(obj.try_index(vm))
7373
}
7474
}
7575

@@ -245,8 +245,8 @@ impl Constructor for PyInt {
245245
fn py_new(cls: PyTypeRef, options: Self::Args, vm: &VirtualMachine) -> PyResult {
246246
let value = if let OptionalArg::Present(val) = options.val_options {
247247
if let OptionalArg::Present(base) = options.base {
248-
let base = vm
249-
.to_index(&base)?
248+
let base = base
249+
.try_index(vm)?
250250
.as_bigint()
251251
.to_u32()
252252
.filter(|&v| v == 0 || (2..=36).contains(&v))

vm/src/builtins/memory.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,7 @@ impl TryFromObject for SubscriptNeedle {
904904
Ok(Self::Index(i.try_to_primitive(vm)?))
905905
} else if obj.payload_is::<PySlice>() {
906906
Ok(Self::Slice(unsafe { obj.downcast_unchecked::<PySlice>() }))
907-
} else if let Ok(i) = vm.to_index(&obj) {
907+
} else if let Ok(i) = obj.try_index(vm) {
908908
Ok(Self::Index(i.try_to_primitive(vm)?))
909909
} else {
910910
if let Some(tuple) = obj.payload::<PyTuple>() {

vm/src/builtins/range.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ impl TryFromObject for RangeIndex {
682682
i @ PyInt => Ok(RangeIndex::Int(i)),
683683
s @ PySlice => Ok(RangeIndex::Slice(s)),
684684
obj => {
685-
let val = vm.to_index(&obj).map_err(|_| vm.new_type_error(format!(
685+
let val = obj.try_index(vm).map_err(|_| vm.new_type_error(format!(
686686
"sequence indices be integers or slices or classes that override __index__ operator, not '{}'",
687687
obj.class().name()
688688
)))?;

vm/src/byte.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub fn bytes_from_object(vm: &VirtualMachine, obj: &PyObject) -> PyResult<Vec<u8
2020
}
2121

2222
pub fn value_from_object(vm: &VirtualMachine, obj: &PyObject) -> PyResult<u8> {
23-
vm.to_index(obj)?
23+
obj.try_index(vm)?
2424
.as_bigint()
2525
.to_u8()
2626
.ok_or_else(|| vm.new_value_error("byte must be in range(0, 256)".to_owned()))

vm/src/convert/try_from.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ impl TryFromObject for std::time::Duration {
108108
use std::time::Duration;
109109
if let Some(float) = obj.payload::<PyFloat>() {
110110
Ok(Duration::from_secs_f64(float.to_f64()))
111-
} else if let Some(int) = vm.to_index_opt(obj.clone()) {
111+
} else if let Some(int) = obj.try_index_opt(vm) {
112112
let sec = int?
113113
.as_bigint()
114114
.to_u64()

0 commit comments

Comments
 (0)