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

Method getsockopt

crates/stdlib/src/socket.rs:2250–2299  ·  view source on GitHub ↗
(
            &self,
            level: i32,
            name: i32,
            buflen: OptionalArg<i32>,
            vm: &VirtualMachine,
        )

Source from the content-addressed store, hash-verified

2248
2249 #[pymethod]
2250 fn getsockopt(
2251 &self,
2252 level: i32,
2253 name: i32,
2254 buflen: OptionalArg<i32>,
2255 vm: &VirtualMachine,
2256 ) -> Result<PyObjectRef, IoOrPyException> {
2257 let sock = self.sock()?;
2258 let fd = sock_fileno(&sock);
2259 let buflen = buflen.unwrap_or(0);
2260 if buflen == 0 {
2261 let mut flag: libc::c_int = 0;
2262 let mut flagsize = core::mem::size_of::<libc::c_int>() as _;
2263 let ret = unsafe {
2264 c::getsockopt(
2265 fd as _,
2266 level,
2267 name,
2268 &mut flag as *mut libc::c_int as *mut _,
2269 &mut flagsize,
2270 )
2271 };
2272 if ret < 0 {
2273 return Err(crate::common::os::errno_io_error().into());
2274 }
2275 Ok(vm.ctx.new_int(flag).into())
2276 } else {
2277 if buflen <= 0 || buflen > 1024 {
2278 return Err(vm
2279 .new_os_error("getsockopt buflen out of range".to_owned())
2280 .into());
2281 }
2282 let mut buf = vec![0u8; buflen as usize];
2283 let mut buflen = buflen as _;
2284 let ret = unsafe {
2285 c::getsockopt(
2286 fd as _,
2287 level,
2288 name,
2289 buf.as_mut_ptr() as *mut _,
2290 &mut buflen,
2291 )
2292 };
2293 if ret < 0 {
2294 return Err(crate::common::os::errno_io_error().into());
2295 }
2296 buf.truncate(buflen as usize);
2297 Ok(vm.ctx.new_bytes(buf).into())
2298 }
2299 }
2300
2301 #[pymethod]
2302 fn setsockopt(

Callers

nothing calls this directly

Calls 10

sock_filenoFunction · 0.85
errno_io_errorFunction · 0.85
sockMethod · 0.80
new_os_errorMethod · 0.80
as_mut_ptrMethod · 0.80
ErrClass · 0.50
new_intMethod · 0.45
to_ownedMethod · 0.45
truncateMethod · 0.45
new_bytesMethod · 0.45

Tested by

no test coverage detected