@@ -117,10 +117,6 @@ async def connect_tcp(
117117 local_address : typing .Optional [str ] = None ,
118118 socket_options : typing .Optional [typing .Iterable [SOCKET_OPTION ]] = None ,
119119 ) -> AsyncNetworkStream :
120- # By default for TCP sockets, trio enables TCP_NODELAY.
121- # https://trio.readthedocs.io/en/stable/reference-io.html#trio.SocketStream
122- if socket_options is None :
123- socket_options = [] # pragma: no cover
124120 timeout_or_inf = float ("inf" ) if timeout is None else timeout
125121 exc_map : ExceptionMapping = {
126122 trio .TooSlowError : ConnectTimeout ,
@@ -132,18 +128,15 @@ async def connect_tcp(
132128 stream : trio .abc .Stream = await trio .open_tcp_stream (
133129 host = host , port = port , local_address = local_address
134130 )
135- for option in socket_options :
136- stream .setsockopt (* option ) # type: ignore[attr-defined] # pragma: no cover
131+ self ._set_socket_options (stream , socket_options )
137132 return TrioStream (stream )
138133
139134 async def connect_unix_socket (
140135 self ,
141136 path : str ,
142137 timeout : typing .Optional [float ] = None ,
143138 socket_options : typing .Optional [typing .Iterable [SOCKET_OPTION ]] = None ,
144- ) -> AsyncNetworkStream : # pragma: nocover
145- if socket_options is None :
146- socket_options = []
139+ ) -> AsyncNetworkStream :
147140 timeout_or_inf = float ("inf" ) if timeout is None else timeout
148141 exc_map : ExceptionMapping = {
149142 trio .TooSlowError : ConnectTimeout ,
@@ -153,9 +146,20 @@ async def connect_unix_socket(
153146 with map_exceptions (exc_map ):
154147 with trio .fail_after (timeout_or_inf ):
155148 stream : trio .abc .Stream = await trio .open_unix_socket (path )
156- for option in socket_options :
157- stream .setsockopt (* option ) # type: ignore[attr-defined] # pragma: no cover
149+ self ._set_socket_options (stream , socket_options )
158150 return TrioStream (stream )
159151
160152 async def sleep (self , seconds : float ) -> None :
161153 await trio .sleep (seconds ) # pragma: nocover
154+
155+ def _set_socket_options (
156+ self ,
157+ stream : trio .abc .Stream ,
158+ socket_options : typing .Optional [typing .Iterable [SOCKET_OPTION ]] = None ,
159+ ) -> None :
160+ # By default for TCP sockets, trio enables TCP_NODELAY.
161+ # https://trio.readthedocs.io/en/stable/reference-io.html#trio.SocketStream
162+ if not socket_options :
163+ return
164+ for option in socket_options :
165+ stream .setsockopt (* option ) # type: ignore[attr-defined]
0 commit comments