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

Method __getitem__

crates/vm/src/builtins/range.rs:323–348  ·  view source on GitHub ↗
(&self, subscript: PyObjectRef, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

321 }
322
323 fn __getitem__(&self, subscript: PyObjectRef, vm: &VirtualMachine) -> PyResult {
324 match RangeIndex::try_from_object(vm, subscript)? {
325 RangeIndex::Slice(slice) => {
326 let (mut sub_start, mut sub_stop, mut sub_step) =
327 slice.inner_indices(&self.compute_length(), vm)?;
328 let range_step = &self.step;
329 let range_start = &self.start;
330
331 sub_step *= range_step.as_bigint();
332 sub_start = (sub_start * range_step.as_bigint()) + range_start.as_bigint();
333 sub_stop = (sub_stop * range_step.as_bigint()) + range_start.as_bigint();
334
335 Ok(Self {
336 start: vm.ctx.new_pyref(sub_start),
337 stop: vm.ctx.new_pyref(sub_stop),
338 step: vm.ctx.new_pyref(sub_step),
339 }
340 .into_ref(&vm.ctx)
341 .into())
342 }
343 RangeIndex::Int(index) => match self.get(index.as_bigint()) {
344 Some(value) => Ok(vm.ctx.new_int(value).into()),
345 None => Err(vm.new_index_error("range object index out of range")),
346 },
347 }
348 }
349
350 #[pyslot]
351 fn slot_new(cls: PyTypeRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult {

Callers

nothing calls this directly

Calls 8

inner_indicesMethod · 0.80
compute_lengthMethod · 0.80
as_bigintMethod · 0.80
new_pyrefMethod · 0.80
ErrClass · 0.50
into_refMethod · 0.45
getMethod · 0.45
new_intMethod · 0.45

Tested by

no test coverage detected