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

Function getservbyport

crates/stdlib/src/socket.rs:2693–2712  ·  view source on GitHub ↗
(
        port: i32,
        protocolname: OptionalArg<PyStrRef>,
        vm: &VirtualMachine,
    )

Source from the content-addressed store, hash-verified

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>]) }

Callers

nothing calls this directly

Calls 10

cstr_opt_as_ptrFunction · 0.85
ok_or_elseMethod · 0.80
new_os_errorMethod · 0.80
into_ownedMethod · 0.80
ErrClass · 0.50
mapMethod · 0.45
as_refMethod · 0.45
to_cstringMethod · 0.45
to_ownedMethod · 0.45
to_string_lossyMethod · 0.45

Tested by

no test coverage detected