@@ -64,65 +64,69 @@ async def async_sendall(sock: Union[socket.socket, _sslConn], buf: bytes) -> Non
64
64
loop = asyncio .get_event_loop ()
65
65
try :
66
66
if _HAVE_SSL and isinstance (sock , (SSLSocket , _sslConn )):
67
- if sys .platform == "win32" :
68
- await asyncio .wait_for (_async_sendall_ssl_windows (sock , buf ), timeout = timeout )
69
- else :
70
- await asyncio .wait_for (_async_sendall_ssl (sock , buf , loop ), timeout = timeout )
67
+ await asyncio .wait_for (_async_sendall_ssl (sock , buf , loop ), timeout = timeout )
71
68
else :
72
69
await asyncio .wait_for (loop .sock_sendall (sock , buf ), timeout = timeout ) # type: ignore[arg-type]
70
+ except asyncio .TimeoutError as exc :
71
+ # Convert the asyncio.wait_for timeout error to socket.timeout which pool.py understands.
72
+ raise socket .timeout ("timed out" ) from exc
73
73
finally :
74
74
sock .settimeout (timeout )
75
75
76
76
77
- async def _async_sendall_ssl (
78
- sock : Union [socket .socket , _sslConn ], buf : bytes , loop : AbstractEventLoop
79
- ) -> None :
80
- view = memoryview (buf )
81
- fd = sock .fileno ()
82
- sent = 0
83
-
84
- def _is_ready (fut : Future ) -> None :
85
- loop .remove_writer (fd )
86
- loop .remove_reader (fd )
87
- if fut .done ():
88
- return
89
- fut .set_result (None )
90
-
91
- while sent < len (buf ):
92
- try :
93
- sent += sock .send (view [sent :])
94
- except BLOCKING_IO_ERRORS as exc :
95
- fd = sock .fileno ()
96
- # Check for closed socket.
97
- if fd == - 1 :
98
- raise SSLError ("Underlying socket has been closed" ) from None
99
- if isinstance (exc , BLOCKING_IO_READ_ERROR ):
100
- fut = loop .create_future ()
101
- loop .add_reader (fd , _is_ready , fut )
102
- await fut
103
- if isinstance (exc , BLOCKING_IO_WRITE_ERROR ):
104
- fut = loop .create_future ()
105
- loop .add_writer (fd , _is_ready , fut )
106
- await fut
107
- if _HAVE_PYOPENSSL and isinstance (exc , BLOCKING_IO_LOOKUP_ERROR ):
108
- fut = loop .create_future ()
109
- loop .add_reader (fd , _is_ready , fut )
110
- loop .add_writer (fd , _is_ready , fut )
111
- await fut
112
-
113
-
114
- # The default Windows asyncio event loop does not support loop.add_reader/add_writer: https://docs.python.org/3/library/asyncio-platforms.html#asyncio-platform-support
115
- async def _async_sendall_ssl_windows (sock : Union [socket .socket , _sslConn ], buf : bytes ) -> None :
116
- view = memoryview (buf )
117
- total_length = len (buf )
118
- total_sent = 0
119
- while total_sent < total_length :
120
- try :
121
- sent = sock .send (view [total_sent :])
122
- except BLOCKING_IO_ERRORS :
123
- await asyncio .sleep (0.5 )
124
- sent = 0
125
- total_sent += sent
77
+ if sys .platform != "win32" :
78
+
79
+ async def _async_sendall_ssl (
80
+ sock : Union [socket .socket , _sslConn ], buf : bytes , loop : AbstractEventLoop
81
+ ) -> None :
82
+ view = memoryview (buf )
83
+ fd = sock .fileno ()
84
+ sent = 0
85
+
86
+ def _is_ready (fut : Future ) -> None :
87
+ loop .remove_writer (fd )
88
+ loop .remove_reader (fd )
89
+ if fut .done ():
90
+ return
91
+ fut .set_result (None )
92
+
93
+ while sent < len (buf ):
94
+ try :
95
+ sent += sock .send (view [sent :])
96
+ except BLOCKING_IO_ERRORS as exc :
97
+ fd = sock .fileno ()
98
+ # Check for closed socket.
99
+ if fd == - 1 :
100
+ raise SSLError ("Underlying socket has been closed" ) from None
101
+ if isinstance (exc , BLOCKING_IO_READ_ERROR ):
102
+ fut = loop .create_future ()
103
+ loop .add_reader (fd , _is_ready , fut )
104
+ await fut
105
+ if isinstance (exc , BLOCKING_IO_WRITE_ERROR ):
106
+ fut = loop .create_future ()
107
+ loop .add_writer (fd , _is_ready , fut )
108
+ await fut
109
+ if _HAVE_PYOPENSSL and isinstance (exc , BLOCKING_IO_LOOKUP_ERROR ):
110
+ fut = loop .create_future ()
111
+ loop .add_reader (fd , _is_ready , fut )
112
+ loop .add_writer (fd , _is_ready , fut )
113
+ await fut
114
+ else :
115
+ # The default Windows asyncio event loop does not support loop.add_reader/add_writer:
116
+ # https://docs.python.org/3/library/asyncio-platforms.html#asyncio-platform-support
117
+ async def _async_sendall_ssl (
118
+ sock : Union [socket .socket , _sslConn ], buf : bytes , dummy : AbstractEventLoop
119
+ ) -> None :
120
+ view = memoryview (buf )
121
+ total_length = len (buf )
122
+ total_sent = 0
123
+ while total_sent < total_length :
124
+ try :
125
+ sent = sock .send (view [total_sent :])
126
+ except BLOCKING_IO_ERRORS :
127
+ await asyncio .sleep (0.5 )
128
+ sent = 0
129
+ total_sent += sent
126
130
127
131
128
132
def sendall (sock : Union [socket .socket , _sslConn ], buf : bytes ) -> None :
0 commit comments