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

Method setitem_by_slice

crates/vm/src/builtins/memory.rs:485–537  ·  view source on GitHub ↗
(
        &self,
        slice: &PySlice,
        src: PyObjectRef,
        vm: &VirtualMachine,
    )

Source from the content-addressed store, hash-verified

483
484impl Py<PyMemoryView> {
485 fn setitem_by_slice(
486 &self,
487 slice: &PySlice,
488 src: PyObjectRef,
489 vm: &VirtualMachine,
490 ) -> PyResult<()> {
491 if self.desc.ndim() != 1 {
492 return Err(vm.new_not_implemented_error("sub-view are not implemented"));
493 }
494
495 let mut dest = self.new_view();
496 dest.init_slice(slice, 0, vm)?;
497 dest.init_len();
498
499 if self.is(&src) {
500 return if !is_equiv_structure(&self.desc, &dest.desc) {
501 Err(vm.new_value_error(
502 "memoryview assignment: lvalue and rvalue have different structures",
503 ))
504 } else {
505 // assign self[:] to self
506 Ok(())
507 };
508 };
509
510 let src = if let Some(src) = src.downcast_ref::<PyMemoryView>() {
511 if self.buffer.obj.is(&src.buffer.obj) {
512 src.to_contiguous(vm)
513 } else {
514 AsBuffer::as_buffer(src, vm)?
515 }
516 } else {
517 PyBuffer::try_from_object(vm, src)?
518 };
519
520 if !is_equiv_structure(&src.desc, &dest.desc) {
521 return Err(vm.new_value_error(
522 "memoryview assignment: lvalue and rvalue have different structures",
523 ));
524 }
525
526 let mut bytes_mut = dest.buffer.obj_bytes_mut();
527 let src_bytes = src.obj_bytes();
528 dest.desc.zip_eq(&src.desc, true, |a_range, b_range| {
529 let a_range = (a_range.start + dest.start as isize) as usize
530 ..(a_range.end + dest.start as isize) as usize;
531 let b_range = b_range.start as usize..b_range.end as usize;
532 bytes_mut[a_range].copy_from_slice(&src_bytes[b_range]);
533 false
534 });
535
536 Ok(())
537 }
538}
539
540#[pyclass(

Callers 3

_setitemMethod · 0.45
__setitem__Method · 0.45
_setitemMethod · 0.45

Calls 11

is_equiv_structureFunction · 0.85
new_viewMethod · 0.80
init_sliceMethod · 0.80
init_lenMethod · 0.80
isMethod · 0.80
to_contiguousMethod · 0.80
zip_eqMethod · 0.80
ErrClass · 0.50
ndimMethod · 0.45
obj_bytes_mutMethod · 0.45
obj_bytesMethod · 0.45

Tested by

no test coverage detected