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

Method _init

crates/stdlib/src/socket.rs:1459–1602  ·  view source on GitHub ↗
(
            zelf: PyRef<Self>,
            args: <Self as Initializer>::Args,
            vm: &VirtualMachine,
        )

Source from the content-addressed store, hash-verified

1457 )]
1458 impl PySocket {
1459 fn _init(
1460 zelf: PyRef<Self>,
1461 args: <Self as Initializer>::Args,
1462 vm: &VirtualMachine,
1463 ) -> Result<(), IoOrPyException> {
1464 let mut family = args.family.unwrap_or(-1);
1465 let mut socket_kind = args.r#type.unwrap_or(-1);
1466 let mut proto = args.proto.unwrap_or(-1);
1467
1468 let fileno = args.fileno;
1469 let sock;
1470
1471 // On Windows, fileno can be bytes from socket.share() for fromshare()
1472 #[cfg(windows)]
1473 if let Some(fileno_obj) = fileno.flatten() {
1474 use crate::vm::builtins::PyBytes;
1475 if let Ok(bytes) = fileno_obj.clone().downcast::<PyBytes>() {
1476 let bytes_data = bytes.as_bytes();
1477 let expected_size = core::mem::size_of::<c::WSAPROTOCOL_INFOW>();
1478
1479 if bytes_data.len() != expected_size {
1480 return Err(vm
1481 .new_value_error(format!(
1482 "socket descriptor string has wrong size, should be {} bytes",
1483 expected_size
1484 ))
1485 .into());
1486 }
1487
1488 let mut info: c::WSAPROTOCOL_INFOW = unsafe { core::mem::zeroed() };
1489 unsafe {
1490 core::ptr::copy_nonoverlapping(
1491 bytes_data.as_ptr(),
1492 &mut info as *mut c::WSAPROTOCOL_INFOW as *mut u8,
1493 expected_size,
1494 );
1495 }
1496
1497 let fd = unsafe {
1498 c::WSASocketW(
1499 c::FROM_PROTOCOL_INFO,
1500 c::FROM_PROTOCOL_INFO,
1501 c::FROM_PROTOCOL_INFO,
1502 &info,
1503 0,
1504 c::WSA_FLAG_OVERLAPPED,
1505 )
1506 };
1507
1508 if fd == c::INVALID_SOCKET {
1509 return Err(Self::wsa_error().into());
1510 }
1511
1512 crate::vm::stdlib::nt::raw_set_handle_inheritable(fd as _, false)?;
1513
1514 family = info.iAddressFamily;
1515 socket_kind = info.iSocketType;
1516 proto = info.iProtocol;

Callers

nothing calls this directly

Calls 15

sock_from_raw_uncheckedFunction · 0.85
get_raw_sockFunction · 0.85
sock_from_rawFunction · 0.85
forgetFunction · 0.85
newFunction · 0.85
init_innerMethod · 0.80
ErrClass · 0.50
SomeClass · 0.50
flattenMethod · 0.45
cloneMethod · 0.45
as_bytesMethod · 0.45

Tested by

no test coverage detected