(
port: i32,
protocolname: OptionalArg<PyStrRef>,
vm: &VirtualMachine,
)
| 2691 | |
| 2692 | #[pyfunction] |
| 2693 | fn getservbyport( |
| 2694 | port: i32, |
| 2695 | protocolname: OptionalArg<PyStrRef>, |
| 2696 | vm: &VirtualMachine, |
| 2697 | ) -> PyResult<String> { |
| 2698 | let port = port |
| 2699 | .to_u16() |
| 2700 | .ok_or_else(|| vm.new_overflow_error("getservbyport: port must be 0-65535."))?; |
| 2701 | let cstr_proto = protocolname |
| 2702 | .as_ref() |
| 2703 | .map(|s| s.to_cstring(vm)) |
| 2704 | .transpose()?; |
| 2705 | let cstr_proto = cstr_opt_as_ptr(&cstr_proto); |
| 2706 | let serv = unsafe { c::getservbyport(port.to_be() as _, cstr_proto as _) }; |
| 2707 | if serv.is_null() { |
| 2708 | return Err(vm.new_os_error("port/proto not found".to_owned())); |
| 2709 | } |
| 2710 | let s = unsafe { ffi::CStr::from_ptr((*serv).s_name as _) }; |
| 2711 | Ok(s.to_string_lossy().into_owned()) |
| 2712 | } |
| 2713 | |
| 2714 | unsafe fn slice_as_uninit<T>(v: &mut [T]) -> &mut [MaybeUninit<T>] { |
| 2715 | unsafe { &mut *(v as *mut [T] as *mut [MaybeUninit<T>]) } |
nothing calls this directly
no test coverage detected