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

Method read

crates/stdlib/src/mmap.rs:1106–1135  ·  view source on GitHub ↗
(&self, n: OptionalArg<PyObjectRef>, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

1104
1105 #[pymethod]
1106 fn read(&self, n: OptionalArg<PyObjectRef>, vm: &VirtualMachine) -> PyResult<PyBytesRef> {
1107 let num_bytes = n
1108 .map(|obj| {
1109 let class = obj.class().to_owned();
1110 obj.try_into_value::<Option<isize>>(vm).map_err(|_| {
1111 vm.new_type_error(format!(
1112 "read argument must be int or None, not {}",
1113 class.name()
1114 ))
1115 })
1116 })
1117 .transpose()?
1118 .flatten();
1119 let mmap = self.check_valid(vm)?;
1120 let pos = self.pos();
1121 let remaining = self.__len__().saturating_sub(pos);
1122 let num_bytes = num_bytes
1123 .filter(|&n| n >= 0 && (n as usize) <= remaining)
1124 .map(|n| n as usize)
1125 .unwrap_or(remaining);
1126
1127 let end_pos = pos + num_bytes;
1128 let bytes = mmap.deref().as_ref().unwrap().as_slice()[pos..end_pos].to_vec();
1129
1130 let result = PyBytes::from(bytes).into_ref(&vm.ctx);
1131
1132 self.advance_pos(num_bytes);
1133
1134 Ok(result)
1135 }
1136
1137 #[pymethod]
1138 fn read_byte(&self, vm: &VirtualMachine) -> PyResult<PyIntRef> {

Callers

nothing calls this directly

Calls 15

check_validMethod · 0.80
to_vecMethod · 0.80
advance_posMethod · 0.80
flattenMethod · 0.45
mapMethod · 0.45
to_ownedMethod · 0.45
classMethod · 0.45
posMethod · 0.45
__len__Method · 0.45
filterMethod · 0.45
as_sliceMethod · 0.45
unwrapMethod · 0.45

Tested by

no test coverage detected