@@ -179,40 +179,45 @@ cdef class UVStream(UVBaseTransport):
179
179
180
180
cdef inline _try_write(self , object data):
181
181
cdef:
182
- ssize_t written
183
- int fd
182
+ int written
183
+ bint used_buf = 0
184
184
Py_buffer py_buf
185
-
186
- fd = self ._fileno()
185
+ uv.uv_buf_t uv_buf
187
186
188
187
if PyBytes_CheckExact(data):
189
- written = system.send(fd, PyBytes_AS_STRING(data),
190
- Py_SIZE(data), 0 )
191
-
188
+ uv_buf.base = PyBytes_AS_STRING(data)
189
+ uv_buf.len = Py_SIZE(data)
192
190
elif PyByteArray_CheckExact(data):
193
- written = system.send(fd, PyByteArray_AS_STRING(data),
194
- Py_SIZE(data), 0 )
195
-
191
+ uv_buf.base = PyByteArray_AS_STRING(data)
192
+ uv_buf.len = Py_SIZE(data)
196
193
else :
197
194
PyObject_GetBuffer(data, & py_buf, PyBUF_SIMPLE)
198
- written = system.send(fd, < char * > py_buf.buf, py_buf.len, 0 )
195
+ used_buf = 1
196
+ uv_buf.base = < char * > py_buf.buf
197
+ uv_buf.len = py_buf.len
198
+
199
+ written = uv.uv_try_write(
200
+ < uv.uv_stream_t* > self ._handle,
201
+ & uv_buf, 1 )
202
+
203
+ if used_buf:
199
204
PyBuffer_Release(& py_buf)
200
205
201
206
if written < 0 :
202
- if errno.errno not in (system.EINTR, system.EAGAIN,
203
- system.EWOULDBLOCK, system.EINPROGRESS,
204
- system.EALREADY,
205
- # If it's a wrong type of FD - let libuv
206
- # handle it, ignore the error:
207
- system.ENOTSOCK, system.EBADF,
208
- system.ENOSYS):
209
- exc = convert_error(- errno.errno)
207
+ if written == uv.UV_EAGAIN:
208
+ return - 1
209
+ else :
210
+ exc = convert_error(written)
210
211
self ._fatal_error(exc, True )
212
+ return
211
213
212
214
IF DEBUG:
213
215
if written > 0 :
214
216
self ._loop._debug_stream_write_tries += 1
215
217
218
+ if written == uv_buf.len:
219
+ return 0
220
+
216
221
return written
217
222
218
223
cdef inline _write(self , object data):
@@ -224,9 +229,16 @@ cdef class UVStream(UVBaseTransport):
224
229
# Try to write without polling only when there is
225
230
# no data in write buffers.
226
231
sent = self ._try_write(data)
232
+ if sent == 0 :
233
+ # All data was successfully written.
234
+ return
227
235
if sent > 0 :
228
- if sent == len (data):
229
- return
236
+ IF DEBUG:
237
+ if sent == len (data):
238
+ raise RuntimeError (
239
+ ' _try_write sent all data and returned non-zero' )
240
+ if not isinstance (data, memoryview):
241
+ data = memoryview(data)
230
242
data = data[sent:]
231
243
232
244
ctx = _StreamWriteContext.new(self , data)
0 commit comments