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

Function WriteFile

crates/vm/src/stdlib/_winapi.rs:1289–1382  ·  view source on GitHub ↗
(args: WriteFileArgs, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

1287 /// WriteFile - Write data to a file or I/O device.
1288 #[pyfunction]
1289 fn WriteFile(args: WriteFileArgs, vm: &VirtualMachine) -> PyResult<PyObjectRef> {
1290 use windows_sys::Win32::Storage::FileSystem::WriteFile as WinWriteFile;
1291
1292 let handle = args.handle;
1293 let use_overlapped = args.overlapped;
1294 let buf = args.buffer.borrow_buf();
1295 let len = core::cmp::min(buf.len(), u32::MAX as usize) as u32;
1296
1297 if use_overlapped {
1298 use windows_sys::Win32::Foundation::ERROR_IO_PENDING;
1299
1300 let ov = Overlapped::new_with_handle(handle.0);
1301 let err = {
1302 let mut inner = ov.inner.lock();
1303 inner.write_buffer = Some(buf.to_vec());
1304 let write_buf = inner.write_buffer.as_ref().unwrap();
1305 let mut written: u32 = 0;
1306 let ret = unsafe {
1307 WinWriteFile(
1308 handle.0,
1309 write_buf.as_ptr() as *const _,
1310 len,
1311 &mut written,
1312 &mut *inner.overlapped,
1313 )
1314 };
1315
1316 let err = if ret == 0 {
1317 unsafe { windows_sys::Win32::Foundation::GetLastError() }
1318 } else {
1319 0
1320 };
1321
1322 if ret == 0 && err != ERROR_IO_PENDING {
1323 return Err(vm.new_last_os_error());
1324 }
1325 if ret == 0 && err == ERROR_IO_PENDING {
1326 inner.pending = true;
1327 }
1328
1329 err
1330 };
1331
1332 // Without GIL, the Python-level PipeConnection._send_bytes has a
1333 // race on _send_ov when the caller (SimpleQueue) skips locking on
1334 // Windows. Wait for completion here so the caller never sees
1335 // ERROR_IO_PENDING and never blocks in WaitForMultipleObjects,
1336 // keeping the _send_ov window negligibly small.
1337 if err == ERROR_IO_PENDING {
1338 let event = ov.inner.lock().overlapped.hEvent;
1339 vm.allow_threads(|| unsafe {
1340 windows_sys::Win32::System::Threading::WaitForSingleObject(
1341 event,
1342 windows_sys::Win32::System::Threading::INFINITE,
1343 );
1344 });
1345 let result = vm
1346 .ctx

Callers 2

WriteFileMethod · 0.85
query_thread_implFunction · 0.85

Calls 15

minFunction · 0.85
WaitForSingleObjectFunction · 0.85
to_vecMethod · 0.80
new_last_os_errorMethod · 0.80
allow_threadsMethod · 0.80
GetLastErrorFunction · 0.70
SomeClass · 0.50
ErrClass · 0.50
borrow_bufMethod · 0.45
lenMethod · 0.45
lockMethod · 0.45
unwrapMethod · 0.45

Tested by

no test coverage detected