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

Method ioctl

crates/stdlib/src/socket.rs:2363–2459  ·  view source on GitHub ↗
(
            &self,
            cmd: PyObjectRef,
            option: PyObjectRef,
            vm: &VirtualMachine,
        )

Source from the content-addressed store, hash-verified

2361 #[cfg(windows)]
2362 #[pymethod]
2363 fn ioctl(
2364 &self,
2365 cmd: PyObjectRef,
2366 option: PyObjectRef,
2367 vm: &VirtualMachine,
2368 ) -> Result<u32, IoOrPyException> {
2369 use crate::vm::builtins::PyInt;
2370 use crate::vm::convert::TryFromObject;
2371
2372 let sock = self.sock()?;
2373 let fd = sock_fileno(&sock);
2374 let mut recv: u32 = 0;
2375
2376 // Convert cmd to u32, returning ValueError for invalid/negative values
2377 let cmd_int = cmd
2378 .downcast::<PyInt>()
2379 .map_err(|_| vm.new_type_error("an integer is required"))?;
2380 let cmd_val = cmd_int.as_bigint();
2381 let cmd: u32 = cmd_val
2382 .to_u32()
2383 .ok_or_else(|| vm.new_value_error(format!("invalid ioctl command {}", cmd_val)))?;
2384
2385 match cmd {
2386 c::SIO_RCVALL | c::SIO_LOOPBACK_FAST_PATH => {
2387 // Option must be an integer, not None
2388 if vm.is_none(&option) {
2389 return Err(vm
2390 .new_type_error("an integer is required (got type NoneType)")
2391 .into());
2392 }
2393 let option_val: u32 = TryFromObject::try_from_object(vm, option)?;
2394 let ret = unsafe {
2395 c::WSAIoctl(
2396 fd as _,
2397 cmd,
2398 &option_val as *const u32 as *const _,
2399 core::mem::size_of::<u32>() as u32,
2400 core::ptr::null_mut(),
2401 0,
2402 &mut recv,
2403 core::ptr::null_mut(),
2404 None,
2405 )
2406 };
2407 if ret == c::SOCKET_ERROR {
2408 return Err(Self::wsa_error().into());
2409 }
2410 Ok(recv)
2411 }
2412 c::SIO_KEEPALIVE_VALS => {
2413 let tuple: PyTupleRef = option
2414 .downcast()
2415 .map_err(|_| vm.new_type_error("SIO_KEEPALIVE_VALS requires a tuple"))?;
2416 if tuple.len() != 3 {
2417 return Err(vm
2418 .new_type_error(
2419 "SIO_KEEPALIVE_VALS requires (onoff, keepalivetime, keepaliveinterval)",
2420 )

Callers 8

_ficloneFunction · 0.80
get_cidFunction · 0.80
test_sock_ioctlMethod · 0.80
test_ioctl.pyFile · 0.80
test_ioctlMethod · 0.80

Calls 10

sock_filenoFunction · 0.85
sockMethod · 0.80
as_bigintMethod · 0.80
ok_or_elseMethod · 0.80
downcastMethod · 0.80
ErrClass · 0.50
to_u32Method · 0.45
is_noneMethod · 0.45
lenMethod · 0.45
cloneMethod · 0.45

Tested by 6

get_cidFunction · 0.64
test_sock_ioctlMethod · 0.64
test_ioctlMethod · 0.64