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

Function pyobject_to_opt_usize

crates/vm/src/stdlib/itertools.rs:718–737  ·  view source on GitHub ↗

Restrict obj to ints with value 0 <= val <= sys.maxsize On failure (out of range, non-int object) a ValueError is raised.

(
        obj: PyObjectRef,
        name: &'static str,
        vm: &VirtualMachine,
    )

Source from the content-addressed store, hash-verified

716 // Restrict obj to ints with value 0 <= val <= sys.maxsize
717 // On failure (out of range, non-int object) a ValueError is raised.
718 fn pyobject_to_opt_usize(
719 obj: PyObjectRef,
720 name: &'static str,
721 vm: &VirtualMachine,
722 ) -> PyResult<usize> {
723 let is_int = obj.fast_isinstance(vm.ctx.types.int_type);
724 if is_int {
725 let value = int::get_value(&obj).to_usize();
726 if let Some(value) = value {
727 // Only succeeds for values for which 0 <= value <= sys.maxsize
728 if value <= sys::MAXSIZE as usize {
729 return Ok(value);
730 }
731 }
732 }
733 // We don't have an int or value was < 0 or > sys.maxsize
734 Err(vm.new_value_error(format!(
735 "{name} argument for islice() must be None or an integer: 0 <= x <= sys.maxsize."
736 )))
737 }
738
739 #[pyclass(with(IterNext, Iterable), flags(BASETYPE))]
740 impl PyItertoolsIslice {

Callers 1

slot_newMethod · 0.85

Calls 4

fast_isinstanceMethod · 0.80
get_valueFunction · 0.50
ErrClass · 0.50
to_usizeMethod · 0.45

Tested by

no test coverage detected