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

Method ReadFile

crates/stdlib/src/overlapped.rs:568–616  ·  view source on GitHub ↗
(zelf: &Py<Self>, handle: isize, size: u32, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

566 // ReadFile
567 #[pymethod]
568 fn ReadFile(zelf: &Py<Self>, handle: isize, size: u32, vm: &VirtualMachine) -> PyResult {
569 use windows_sys::Win32::Foundation::{
570 ERROR_BROKEN_PIPE, ERROR_IO_PENDING, ERROR_MORE_DATA, ERROR_SUCCESS,
571 };
572 use windows_sys::Win32::Storage::FileSystem::ReadFile;
573
574 let mut inner = zelf.inner.lock();
575 if !matches!(inner.data, OverlappedData::None) {
576 return Err(vm.new_value_error("operation already attempted"));
577 }
578
579 #[cfg(target_pointer_width = "32")]
580 let size = core::cmp::min(size, isize::MAX as u32);
581
582 let buf = vec![0u8; core::cmp::max(size, 1) as usize];
583 let buf = vm.ctx.new_bytes(buf);
584 inner.handle = handle as HANDLE;
585 inner.data = OverlappedData::Read(buf.clone());
586
587 let mut nread: u32 = 0;
588 let ret = unsafe {
589 ReadFile(
590 handle as HANDLE,
591 buf.as_bytes().as_ptr() as *mut _,
592 size,
593 &mut nread,
594 &mut inner.overlapped,
595 )
596 };
597
598 let err = if ret != 0 {
599 ERROR_SUCCESS
600 } else {
601 unsafe { GetLastError() }
602 };
603 inner.error = err;
604
605 match err {
606 ERROR_BROKEN_PIPE => {
607 mark_as_completed(&mut inner.overlapped);
608 Err(set_from_windows_err(err, vm))
609 }
610 ERROR_SUCCESS | ERROR_MORE_DATA | ERROR_IO_PENDING => Ok(vm.ctx.none()),
611 _ => {
612 inner.data = OverlappedData::NotStarted;
613 Err(set_from_windows_err(err, vm))
614 }
615 }
616 }
617
618 // ReadFileInto
619 #[pymethod]

Callers 6

recvMethod · 0.95
test_pipe_overlappedMethod · 0.95
test_popenMethod · 0.95
_recv_bytesMethod · 0.80
_get_more_dataMethod · 0.80
waitFunction · 0.80

Calls 13

minFunction · 0.85
ReadInterface · 0.85
ReadFileFunction · 0.85
mark_as_completedFunction · 0.85
set_from_windows_errFunction · 0.85
noneMethod · 0.80
ErrClass · 0.50
GetLastErrorFunction · 0.50
lockMethod · 0.45
new_bytesMethod · 0.45
cloneMethod · 0.45
as_ptrMethod · 0.45

Tested by 2

test_pipe_overlappedMethod · 0.76
test_popenMethod · 0.76