11# SPDX-License-Identifier: LGPL-2.1-or-later
2+ from __future__ import annotations
23
34from socket import AF_UNSPEC as _AF_UNSPEC
5+ import socket as _socket
6+ import typing as _typing
47
58from ._daemon import (__version__ ,
69 booted ,
1518 _is_mq ,
1619 LISTEN_FDS_START )
1720
18- def _convert_fileobj (fileobj ):
19- try :
20- return fileobj .fileno ()
21- except AttributeError :
21+ if _typing .TYPE_CHECKING :
22+ from _typeshed import FileDescriptorLike , StrOrBytesPath
23+
24+ def _convert_fileobj (fileobj : FileDescriptorLike ) -> int :
25+ if isinstance (fileobj , int ):
2226 return fileobj
27+ return fileobj .fileno ()
2328
24- def is_fifo (fileobj , path = None ):
29+ def is_fifo (fileobj : FileDescriptorLike ,
30+ path : StrOrBytesPath | None = None ) -> bool :
2531 fd = _convert_fileobj (fileobj )
2632 return _is_fifo (fd , path )
2733
28- def is_socket (fileobj , family = _AF_UNSPEC , type = 0 , listening = - 1 ):
34+ def is_socket (fileobj : FileDescriptorLike ,
35+ family : _socket .AddressFamily = _AF_UNSPEC ,
36+ type : _socket .SocketKind | None = None ,
37+ listening : int = - 1 ) -> bool :
2938 fd = _convert_fileobj (fileobj )
30- return _is_socket (fd , family , type , listening )
39+ return _is_socket (fd , family , type or 0 , listening )
3140
32- def is_socket_inet (fileobj , family = _AF_UNSPEC , type = 0 , listening = - 1 , port = 0 ):
41+ def is_socket_inet (fileobj : FileDescriptorLike ,
42+ family : _socket .AddressFamily = _AF_UNSPEC ,
43+ type : _socket .SocketKind | None = None ,
44+ listening : int = - 1 ,
45+ port : int = 0 ) -> bool :
3346 fd = _convert_fileobj (fileobj )
34- return _is_socket_inet (fd , family , type , listening , port )
47+ return _is_socket_inet (fd , family , type or 0 , listening , port )
3548
36- def is_socket_sockaddr (fileobj , address , type = 0 , flowinfo = 0 , listening = - 1 ):
49+ def is_socket_sockaddr (fileobj : FileDescriptorLike ,
50+ address : str ,
51+ type : _socket .SocketKind | None = None ,
52+ flowinfo : int = 0 ,
53+ listening : int = - 1 ) -> bool :
3754 """Check socket type, address and/or port, flowinfo, listening state.
3855
3956 Wraps sd_is_socket_inet_sockaddr(3).
@@ -45,17 +62,20 @@ def is_socket_sockaddr(fileobj, address, type=0, flowinfo=0, listening=-1):
4562 Constants for `family` are defined in the socket module.
4663 """
4764 fd = _convert_fileobj (fileobj )
48- return _is_socket_sockaddr (fd , address , type , flowinfo , listening )
65+ return _is_socket_sockaddr (fd , address , type or 0 , flowinfo , listening )
4966
50- def is_socket_unix (fileobj , type = 0 , listening = - 1 , path = None ):
67+ def is_socket_unix (fileobj : FileDescriptorLike ,
68+ type : _socket .SocketKind | None = None ,
69+ listening : int = - 1 ,
70+ path : StrOrBytesPath | None = None ) -> bool :
5171 fd = _convert_fileobj (fileobj )
52- return _is_socket_unix (fd , type , listening , path )
72+ return _is_socket_unix (fd , type or 0 , listening , path )
5373
54- def is_mq (fileobj , path = None ):
74+ def is_mq (fileobj : FileDescriptorLike , path : StrOrBytesPath | None = None ) -> bool :
5575 fd = _convert_fileobj (fileobj )
5676 return _is_mq (fd , path )
5777
58- def listen_fds (unset_environment = True ):
78+ def listen_fds (unset_environment : bool = True ) -> list [ int ] :
5979 """Return a list of socket activated descriptors
6080
6181 Example::
@@ -73,7 +93,7 @@ def listen_fds(unset_environment=True):
7393 num = _listen_fds (unset_environment )
7494 return list (range (LISTEN_FDS_START , LISTEN_FDS_START + num ))
7595
76- def listen_fds_with_names (unset_environment = True ):
96+ def listen_fds_with_names (unset_environment : bool = True ) -> dict [ int , str ] :
7797 """Return a dictionary of socket activated descriptors as {fd: name}
7898
7999 Example::
@@ -88,8 +108,5 @@ def listen_fds_with_names(unset_environment=True):
88108 Execing python3 (...)
89109 [3]
90110 """
91- composite = _listen_fds_with_names (unset_environment )
92- retval = {}
93- for i in range (0 , composite [0 ]):
94- retval [i + LISTEN_FDS_START ] = composite [1 + i ]
95- return retval
111+ _ , * names = _listen_fds_with_names (unset_environment )
112+ return {i : name for i , name in enumerate (names , LISTEN_FDS_START )}
0 commit comments