@@ -67,27 +67,28 @@ def __init__(self, selector=None):
6767 self ._transports = weakref .WeakValueDictionary ()
6868
6969 def _make_socket_transport (self , sock , protocol , waiter = None , * ,
70- extra = None , server = None ):
70+ extra = None , server = None , context = None ):
7171 self ._ensure_fd_no_transport (sock )
7272 return _SelectorSocketTransport (self , sock , protocol , waiter ,
73- extra , server )
73+ extra , server , context = context )
7474
7575 def _make_ssl_transport (
7676 self , rawsock , protocol , sslcontext , waiter = None ,
7777 * , server_side = False , server_hostname = None ,
7878 extra = None , server = None ,
7979 ssl_handshake_timeout = constants .SSL_HANDSHAKE_TIMEOUT ,
8080 ssl_shutdown_timeout = constants .SSL_SHUTDOWN_TIMEOUT ,
81+ context = None ,
8182 ):
8283 self ._ensure_fd_no_transport (rawsock )
8384 ssl_protocol = sslproto .SSLProtocol (
8485 self , protocol , sslcontext , waiter ,
8586 server_side , server_hostname ,
8687 ssl_handshake_timeout = ssl_handshake_timeout ,
87- ssl_shutdown_timeout = ssl_shutdown_timeout
88+ ssl_shutdown_timeout = ssl_shutdown_timeout ,
8889 )
8990 _SelectorSocketTransport (self , rawsock , ssl_protocol ,
90- extra = extra , server = server )
91+ extra = extra , server = server , context = context )
9192 return ssl_protocol ._app_transport
9293
9394 def _make_datagram_transport (self , sock , protocol ,
@@ -159,16 +160,16 @@ def _write_to_self(self):
159160 def _start_serving (self , protocol_factory , sock ,
160161 sslcontext = None , server = None , backlog = 100 ,
161162 ssl_handshake_timeout = constants .SSL_HANDSHAKE_TIMEOUT ,
162- ssl_shutdown_timeout = constants .SSL_SHUTDOWN_TIMEOUT ):
163+ ssl_shutdown_timeout = constants .SSL_SHUTDOWN_TIMEOUT , context = None ):
163164 self ._add_reader (sock .fileno (), self ._accept_connection ,
164165 protocol_factory , sock , sslcontext , server , backlog ,
165- ssl_handshake_timeout , ssl_shutdown_timeout )
166+ ssl_handshake_timeout , ssl_shutdown_timeout , context )
166167
167168 def _accept_connection (
168169 self , protocol_factory , sock ,
169170 sslcontext = None , server = None , backlog = 100 ,
170171 ssl_handshake_timeout = constants .SSL_HANDSHAKE_TIMEOUT ,
171- ssl_shutdown_timeout = constants .SSL_SHUTDOWN_TIMEOUT ):
172+ ssl_shutdown_timeout = constants .SSL_SHUTDOWN_TIMEOUT , context = None ):
172173 # This method is only called once for each event loop tick where the
173174 # listening socket has triggered an EVENT_READ. There may be multiple
174175 # connections waiting for an .accept() so it is called in a loop.
@@ -204,21 +205,22 @@ def _accept_connection(
204205 self ._start_serving ,
205206 protocol_factory , sock , sslcontext , server ,
206207 backlog , ssl_handshake_timeout ,
207- ssl_shutdown_timeout )
208+ ssl_shutdown_timeout , context )
208209 else :
209210 raise # The event loop will catch, log and ignore it.
210211 else :
211212 extra = {'peername' : addr }
213+ conn_context = context .copy () if context is not None else None
212214 accept = self ._accept_connection2 (
213215 protocol_factory , conn , extra , sslcontext , server ,
214- ssl_handshake_timeout , ssl_shutdown_timeout )
215- self .create_task (accept )
216+ ssl_handshake_timeout , ssl_shutdown_timeout , context = conn_context )
217+ self .create_task (accept , context = conn_context )
216218
217219 async def _accept_connection2 (
218220 self , protocol_factory , conn , extra ,
219221 sslcontext = None , server = None ,
220222 ssl_handshake_timeout = constants .SSL_HANDSHAKE_TIMEOUT ,
221- ssl_shutdown_timeout = constants .SSL_SHUTDOWN_TIMEOUT ):
223+ ssl_shutdown_timeout = constants .SSL_SHUTDOWN_TIMEOUT , context = None ):
222224 protocol = None
223225 transport = None
224226 try :
@@ -229,11 +231,12 @@ async def _accept_connection2(
229231 conn , protocol , sslcontext , waiter = waiter ,
230232 server_side = True , extra = extra , server = server ,
231233 ssl_handshake_timeout = ssl_handshake_timeout ,
232- ssl_shutdown_timeout = ssl_shutdown_timeout )
234+ ssl_shutdown_timeout = ssl_shutdown_timeout ,
235+ context = context )
233236 else :
234237 transport = self ._make_socket_transport (
235238 conn , protocol , waiter = waiter , extra = extra ,
236- server = server )
239+ server = server , context = context )
237240
238241 try :
239242 await waiter
@@ -275,9 +278,9 @@ def _ensure_fd_no_transport(self, fd):
275278 f'File descriptor { fd !r} is used by transport '
276279 f'{ transport !r} ' )
277280
278- def _add_reader (self , fd , callback , * args ):
281+ def _add_reader (self , fd , callback , * args , context = None ):
279282 self ._check_closed ()
280- handle = events .Handle (callback , args , self , None )
283+ handle = events .Handle (callback , args , self , context = context )
281284 key = self ._selector .get_map ().get (fd )
282285 if key is None :
283286 self ._selector .register (fd , selectors .EVENT_READ ,
@@ -309,9 +312,9 @@ def _remove_reader(self, fd):
309312 else :
310313 return False
311314
312- def _add_writer (self , fd , callback , * args ):
315+ def _add_writer (self , fd , callback , * args , context = None ):
313316 self ._check_closed ()
314- handle = events .Handle (callback , args , self , None )
317+ handle = events .Handle (callback , args , self , context = context )
315318 key = self ._selector .get_map ().get (fd )
316319 if key is None :
317320 self ._selector .register (fd , selectors .EVENT_WRITE ,
@@ -770,7 +773,7 @@ class _SelectorTransport(transports._FlowControlMixin,
770773 # exception)
771774 _sock = None
772775
773- def __init__ (self , loop , sock , protocol , extra = None , server = None ):
776+ def __init__ (self , loop , sock , protocol , extra = None , server = None , context = None ):
774777 super ().__init__ (extra , loop )
775778 self ._extra ['socket' ] = trsock .TransportSocket (sock )
776779 try :
@@ -784,7 +787,7 @@ def __init__(self, loop, sock, protocol, extra=None, server=None):
784787 self ._extra ['peername' ] = None
785788 self ._sock = sock
786789 self ._sock_fd = sock .fileno ()
787-
790+ self . _context = context
788791 self ._protocol_connected = False
789792 self .set_protocol (protocol )
790793
@@ -866,7 +869,7 @@ def close(self):
866869 if not self ._buffer :
867870 self ._conn_lost += 1
868871 self ._loop ._remove_writer (self ._sock_fd )
869- self ._loop . call_soon (self ._call_connection_lost , None )
872+ self ._call_soon (self ._call_connection_lost , None )
870873
871874 def __del__ (self , _warn = warnings .warn ):
872875 if self ._sock is not None :
@@ -899,7 +902,7 @@ def _force_close(self, exc):
899902 self ._closing = True
900903 self ._loop ._remove_reader (self ._sock_fd )
901904 self ._conn_lost += 1
902- self ._loop . call_soon (self ._call_connection_lost , exc )
905+ self ._call_soon (self ._call_connection_lost , exc )
903906
904907 def _call_connection_lost (self , exc ):
905908 try :
@@ -921,19 +924,23 @@ def get_write_buffer_size(self):
921924 def _add_reader (self , fd , callback , * args ):
922925 if not self .is_reading ():
923926 return
924- self ._loop ._add_reader (fd , callback , * args )
927+ self ._loop ._add_reader (fd , callback , * args , context = self . _context )
925928
929+ def _add_writer (self , fd , callback , * args ):
930+ self ._loop ._add_writer (fd , callback , * args , context = self ._context )
931+
932+ def _call_soon (self , callback , * args ):
933+ self ._loop .call_soon (callback , * args , context = self ._context )
926934
927935class _SelectorSocketTransport (_SelectorTransport ):
928936
929937 _start_tls_compatible = True
930938 _sendfile_compatible = constants ._SendfileMode .TRY_NATIVE
931939
932940 def __init__ (self , loop , sock , protocol , waiter = None ,
933- extra = None , server = None ):
934-
941+ extra = None , server = None , context = None ):
935942 self ._read_ready_cb = None
936- super ().__init__ (loop , sock , protocol , extra , server )
943+ super ().__init__ (loop , sock , protocol , extra , server , context )
937944 self ._eof = False
938945 self ._empty_waiter = None
939946 if _HAS_SENDMSG :
@@ -945,14 +952,12 @@ def __init__(self, loop, sock, protocol, waiter=None,
945952 # decreases the latency (in some cases significantly.)
946953 base_events ._set_nodelay (self ._sock )
947954
948- self ._loop . call_soon (self ._protocol .connection_made , self )
955+ self ._call_soon (self ._protocol .connection_made , self )
949956 # only start reading when connection_made() has been called
950- self ._loop .call_soon (self ._add_reader ,
951- self ._sock_fd , self ._read_ready )
957+ self ._call_soon (self ._add_reader , self ._sock_fd , self ._read_ready )
952958 if waiter is not None :
953959 # only wake up the waiter when connection_made() has been called
954- self ._loop .call_soon (futures ._set_result_unless_cancelled ,
955- waiter , None )
960+ self ._call_soon (futures ._set_result_unless_cancelled , waiter , None )
956961
957962 def set_protocol (self , protocol ):
958963 if isinstance (protocol , protocols .BufferedProtocol ):
@@ -1081,7 +1086,7 @@ def write(self, data):
10811086 if not data :
10821087 return
10831088 # Not all was written; register write handler.
1084- self ._loop . _add_writer (self ._sock_fd , self ._write_ready )
1089+ self ._add_writer (self ._sock_fd , self ._write_ready )
10851090
10861091 # Add it to the buffer.
10871092 self ._buffer .append (data )
@@ -1185,7 +1190,7 @@ def writelines(self, list_of_data):
11851190 self ._write_ready ()
11861191 # If the entire buffer couldn't be written, register a write handler
11871192 if self ._buffer :
1188- self ._loop . _add_writer (self ._sock_fd , self ._write_ready )
1193+ self ._add_writer (self ._sock_fd , self ._write_ready )
11891194 self ._maybe_pause_protocol ()
11901195
11911196 def can_write_eof (self ):
@@ -1226,14 +1231,12 @@ def __init__(self, loop, sock, protocol, address=None,
12261231 super ().__init__ (loop , sock , protocol , extra )
12271232 self ._address = address
12281233 self ._buffer_size = 0
1229- self ._loop . call_soon (self ._protocol .connection_made , self )
1234+ self ._call_soon (self ._protocol .connection_made , self )
12301235 # only start reading when connection_made() has been called
1231- self ._loop .call_soon (self ._add_reader ,
1232- self ._sock_fd , self ._read_ready )
1236+ self ._call_soon (self ._add_reader , self ._sock_fd , self ._read_ready )
12331237 if waiter is not None :
12341238 # only wake up the waiter when connection_made() has been called
1235- self ._loop .call_soon (futures ._set_result_unless_cancelled ,
1236- waiter , None )
1239+ self ._call_soon (futures ._set_result_unless_cancelled , waiter , None )
12371240
12381241 def get_write_buffer_size (self ):
12391242 return self ._buffer_size
@@ -1280,7 +1283,7 @@ def sendto(self, data, addr=None):
12801283 self ._sock .sendto (data , addr )
12811284 return
12821285 except (BlockingIOError , InterruptedError ):
1283- self ._loop . _add_writer (self ._sock_fd , self ._sendto_ready )
1286+ self ._add_writer (self ._sock_fd , self ._sendto_ready )
12841287 except OSError as exc :
12851288 self ._protocol .error_received (exc )
12861289 return
0 commit comments