MCPcopy Index your code
hub / github.com/RustPython/RustPython / to_isize_index

Function to_isize_index

crates/vm/src/sliceable.rs:454–468  ·  view source on GitHub ↗

Go from PyObjectRef to isize w/o overflow error, out of range values are substituted by isize::MIN or isize::MAX depending on type and value of step. Equivalent to PyEval_SliceIndex.

(vm: &VirtualMachine, obj: &PyObject)

Source from the content-addressed store, hash-verified

452// isize::MIN or isize::MAX depending on type and value of step.
453// Equivalent to PyEval_SliceIndex.
454fn to_isize_index(vm: &VirtualMachine, obj: &PyObject) -> PyResult<Option<isize>> {
455 if vm.is_none(obj) {
456 return Ok(None);
457 }
458 let result = obj.try_index_opt(vm).unwrap_or_else(|| {
459 Err(vm.new_type_error("slice indices must be integers or None or have an __index__ method"))
460 })?;
461 let value = result.as_bigint();
462 let is_negative = value.is_negative();
463 Ok(Some(value.to_isize().unwrap_or(if is_negative {
464 isize::MIN
465 } else {
466 isize::MAX
467 })))
468}

Callers 1

with_sliceMethod · 0.85

Calls 5

try_index_optMethod · 0.80
as_bigintMethod · 0.80
ErrClass · 0.50
SomeClass · 0.50
is_noneMethod · 0.45

Tested by

no test coverage detected