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

Method ass_subscript

crates/stdlib/src/_sqlite3.rs:2547–2631  ·  view source on GitHub ↗
(
            &self,
            needle: &PyObject,
            value: Option<PyObjectRef>,
            vm: &VirtualMachine,
        )

Source from the content-addressed store, hash-verified

2545 }
2546
2547 fn ass_subscript(
2548 &self,
2549 needle: &PyObject,
2550 value: Option<PyObjectRef>,
2551 vm: &VirtualMachine,
2552 ) -> PyResult<()> {
2553 let Some(value) = value else {
2554 return Err(vm.new_type_error("Blob doesn't support slice deletion"));
2555 };
2556 self.ensure_connection_open(vm)?;
2557 let inner = self.inner(vm)?;
2558
2559 if let Some(index) = needle.try_index_opt(vm) {
2560 // Handle single item assignment: blob[i] = b
2561 let Some(value) = value.downcast_ref::<PyInt>() else {
2562 return Err(vm.new_type_error(format!(
2563 "'{}' object cannot be interpreted as an integer",
2564 value.class()
2565 )));
2566 };
2567 let value = value.try_to_primitive::<u8>(vm)?;
2568 let blob_len = inner.blob.bytes();
2569 let index = Self::wrapped_index(index?, blob_len, vm)?;
2570 Self::expect_write(blob_len, 1, index, vm)?;
2571 let ret = inner.blob.write_single(value, index);
2572 self.check(ret, vm)
2573 } else if let Some(slice) = needle.downcast_ref::<PySlice>() {
2574 // Handle slice assignment: blob[a:b:c] = b"..."
2575 let value_buf = PyBuffer::try_from_borrowed_object(vm, &value)?;
2576
2577 let buf = value_buf
2578 .as_contiguous()
2579 .ok_or_else(|| vm.new_buffer_error("underlying buffer is not C-contiguous"))?;
2580
2581 let blob_len = inner.blob.bytes();
2582 let slice = slice.to_saturated(vm)?;
2583 let (range, step, slice_len) = slice.adjust_indices(blob_len as usize);
2584
2585 if step == 0 {
2586 return Err(vm.new_value_error("slice step cannot be zero"));
2587 }
2588
2589 if buf.len() != slice_len {
2590 return Err(vm.new_index_error("Blob slice assignment is wrong size"));
2591 }
2592
2593 if slice_len == 0 {
2594 return Ok(());
2595 }
2596
2597 if step == 1 {
2598 let ret = inner.blob.write(
2599 buf.as_ptr().cast(),
2600 buf.len() as c_int,
2601 range.start as c_int,
2602 );
2603 self.check(ret, vm)
2604 } else {

Callers

nothing calls this directly

Calls 15

try_index_optMethod · 0.80
write_singleMethod · 0.80
ok_or_elseMethod · 0.80
to_saturatedMethod · 0.80
adjust_indicesMethod · 0.80
as_mut_ptrMethod · 0.80
ErrClass · 0.50
innerMethod · 0.45
bytesMethod · 0.45
checkMethod · 0.45
as_contiguousMethod · 0.45

Tested by

no test coverage detected