From 7bcd7aca2b3fbad0c31ded1bf733ed480f5212b4 Mon Sep 17 00:00:00 2001 From: goodboy Date: Thu, 26 Feb 2026 19:26:15 -0500 Subject: [PATCH] Reorg `socket` conditional imports a bit Move the multi-platorm-supporting conditional/dynamic `socket` constant imports to *after* the main cross-platform ones. Also add constant typing and reformat comments a bit for the macOS case. --- tractor/ipc/_uds.py | 51 ++++++++++++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 19 deletions(-) diff --git a/tractor/ipc/_uds.py b/tractor/ipc/_uds.py index 56eeec26..637a9a9a 100644 --- a/tractor/ipc/_uds.py +++ b/tractor/ipc/_uds.py @@ -30,22 +30,6 @@ from socket import ( SOL_SOCKET, ) import struct - -# Platform-specific credential passing constants -# See: https://stackoverflow.com/a/7982749 -if sys.platform == 'linux': - from socket import SO_PASSCRED, SO_PEERCRED -elif sys.platform == 'darwin': # macOS - # macOS uses LOCAL_PEERCRED instead of SO_PEERCRED - # and doesn't need SO_PASSCRED (credential passing is always enabled) - # Value from : #define LOCAL_PEERCRED 0x001 - LOCAL_PEERCRED = 0x0001 - SO_PEERCRED = LOCAL_PEERCRED # Alias for compatibility - SO_PASSCRED = None # Not needed/available on macOS -else: - # Other Unix platforms - may need additional handling - SO_PASSCRED = None - SO_PEERCRED = None from typing import ( Type, TYPE_CHECKING, @@ -68,7 +52,7 @@ from tractor.log import get_logger from tractor.ipc._transport import ( MsgpackTransport, ) -from .._state import ( +from tractor._state import ( get_rt_dir, current_actor, is_root_process, @@ -78,6 +62,28 @@ if TYPE_CHECKING: from ._runtime import Actor +# Platform-specific credential passing constants +# See: https://stackoverflow.com/a/7982749 +if sys.platform == 'linux': + from socket import ( + SO_PASSCRED, + SO_PEERCRED, + ) + +# NOTE, macOS uses `LOCAL_PEERCRED` instead of `SO_PEERCRED` and +# doesn't need `SO_PASSCRED` (credential passing is always enabled). +# XXX See code in : `#define LOCAL_PEERCRED 0x001` +# +elif sys.platform == 'darwin': # macOS + LOCAL_PEERCRED: int = 0x0001 + SO_PEERCRED:int|None = LOCAL_PEERCRED # Alias for compatibility + SO_PASSCRED: int|None = None # Not needed/available on macOS +else: + # Other Unix platforms - may need additional handling + SO_PASSCRED = None + SO_PEERCRED = None + + log = get_logger() @@ -307,7 +313,12 @@ def close_listener( async def open_unix_socket_w_passcred( - filename: str|bytes|os.PathLike[str]|os.PathLike[bytes], + filename: ( + str + |bytes + |os.PathLike[str] + |os.PathLike[bytes] + ), ) -> trio.SocketStream: ''' Literally the exact same as `trio.open_unix_socket()` except we set the additiona @@ -336,7 +347,9 @@ async def open_unix_socket_w_passcred( return trio.SocketStream(sock) -def get_peer_info(sock: trio.socket.socket) -> tuple[ +def get_peer_info( + sock: trio.socket.socket, +) -> tuple[ int, # pid int, # uid int, # guid