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)
| 452 | // isize::MIN or isize::MAX depending on type and value of step. |
| 453 | // Equivalent to PyEval_SliceIndex. |
| 454 | fn 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 | } |
no test coverage detected