Skip to content

Commit f845484

Browse files
refactor 1
1 parent 6990bb9 commit f845484

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

Lib/asyncio/selector_events.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,7 @@ def close(self):
866866
if not self._buffer:
867867
self._conn_lost += 1
868868
self._loop._remove_writer(self._sock_fd)
869-
self._loop.call_soon(self._call_connection_lost, None, context=self._context)
869+
self._call_soon(self._call_connection_lost, None)
870870

871871
def __del__(self, _warn=warnings.warn):
872872
if self._sock is not None:
@@ -899,7 +899,7 @@ def _force_close(self, exc):
899899
self._closing = True
900900
self._loop._remove_reader(self._sock_fd)
901901
self._conn_lost += 1
902-
self._loop.call_soon(self._call_connection_lost, exc, context=self._context)
902+
self._call_soon(self._call_connection_lost, exc)
903903

904904
def _call_connection_lost(self, exc):
905905
try:
@@ -923,6 +923,11 @@ def _add_reader(self, fd, callback, *args):
923923
return
924924
self._loop._add_reader(fd, callback, *args, context=self._context)
925925

926+
def _add_writer(self, fd, callback, *args):
927+
self._loop._add_writer(fd, callback, *args, context=self._context)
928+
929+
def _call_soon(self, callback, *args):
930+
self._loop.call_soon(callback, *args, context=self._context)
926931

927932
class _SelectorSocketTransport(_SelectorTransport):
928933

@@ -945,14 +950,12 @@ def __init__(self, loop, sock, protocol, waiter=None,
945950
# decreases the latency (in some cases significantly.)
946951
base_events._set_nodelay(self._sock)
947952

948-
self._loop.call_soon(self._protocol.connection_made, self, context=context)
953+
self._call_soon(self._protocol.connection_made, self)
949954
# only start reading when connection_made() has been called
950-
self._loop.call_soon(self._add_reader,
951-
self._sock_fd, self._read_ready, context=context)
955+
self._call_soon(self._add_reader, self._sock_fd, self._read_ready)
952956
if waiter is not None:
953957
# only wake up the waiter when connection_made() has been called
954-
self._loop.call_soon(futures._set_result_unless_cancelled,
955-
waiter, None, context=context)
958+
self._call_soon(futures._set_result_unless_cancelled, waiter, None)
956959

957960
def set_protocol(self, protocol):
958961
if isinstance(protocol, protocols.BufferedProtocol):
@@ -1081,7 +1084,7 @@ def write(self, data):
10811084
if not data:
10821085
return
10831086
# Not all was written; register write handler.
1084-
self._loop._add_writer(self._sock_fd, self._write_ready, context=self._context)
1087+
self._add_writer(self._sock_fd, self._write_ready)
10851088

10861089
# Add it to the buffer.
10871090
self._buffer.append(data)
@@ -1185,7 +1188,7 @@ def writelines(self, list_of_data):
11851188
self._write_ready()
11861189
# If the entire buffer couldn't be written, register a write handler
11871190
if self._buffer:
1188-
self._loop._add_writer(self._sock_fd, self._write_ready, context=self._context)
1191+
self._add_writer(self._sock_fd, self._write_ready)
11891192
self._maybe_pause_protocol()
11901193

11911194
def can_write_eof(self):
@@ -1226,14 +1229,12 @@ def __init__(self, loop, sock, protocol, address=None,
12261229
super().__init__(loop, sock, protocol, extra)
12271230
self._address = address
12281231
self._buffer_size = 0
1229-
self._loop.call_soon(self._protocol.connection_made, self)
1232+
self._call_soon(self._protocol.connection_made, self)
12301233
# only start reading when connection_made() has been called
1231-
self._loop.call_soon(self._add_reader,
1232-
self._sock_fd, self._read_ready)
1234+
self._call_soon(self._add_reader, self._sock_fd, self._read_ready)
12331235
if waiter is not None:
12341236
# only wake up the waiter when connection_made() has been called
1235-
self._loop.call_soon(futures._set_result_unless_cancelled,
1236-
waiter, None)
1237+
self._call_soon(futures._set_result_unless_cancelled, waiter, None)
12371238

12381239
def get_write_buffer_size(self):
12391240
return self._buffer_size
@@ -1280,7 +1281,7 @@ def sendto(self, data, addr=None):
12801281
self._sock.sendto(data, addr)
12811282
return
12821283
except (BlockingIOError, InterruptedError):
1283-
self._loop._add_writer(self._sock_fd, self._sendto_ready)
1284+
self._add_writer(self._sock_fd, self._sendto_ready)
12841285
except OSError as exc:
12851286
self._protocol.error_received(exc)
12861287
return

0 commit comments

Comments
 (0)