Merge branch 'linsomniac-py_recv_bytes'
This commit is contained in:
@@ -135,15 +135,30 @@ int zts_py_connect(int fd, int family, int type, PyObject *addr_obj)
|
|||||||
|
|
||||||
PyObject * zts_py_recv(int fd, int len, int flags)
|
PyObject * zts_py_recv(int fd, int len, int flags)
|
||||||
{
|
{
|
||||||
PyObject *t;
|
PyObject *t, *buf;
|
||||||
char buf[4096];
|
int bytes_read;
|
||||||
int err = zts_recv(fd, buf, len, flags);
|
|
||||||
if (err < 0) {
|
buf = PyBytes_FromStringAndSize((char *) 0, len);
|
||||||
|
if (buf == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
|
||||||
|
bytes_read = zts_recv(fd, PyBytes_AS_STRING(buf), len, flags);
|
||||||
t = PyTuple_New(2);
|
t = PyTuple_New(2);
|
||||||
PyTuple_SetItem(t, 0, PyLong_FromLong(err));
|
PyTuple_SetItem(t, 0, PyLong_FromLong(bytes_read));
|
||||||
PyTuple_SetItem(t, 1, PyUnicode_FromString(buf));
|
|
||||||
|
if (bytes_read < 0) {
|
||||||
|
Py_DECREF(buf);
|
||||||
|
Py_INCREF(Py_None);
|
||||||
|
PyTuple_SetItem(t, 1, Py_None);
|
||||||
|
Py_INCREF(t);
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (bytes_read != len) {
|
||||||
|
_PyBytes_Resize(&buf, bytes_read);
|
||||||
|
}
|
||||||
|
|
||||||
|
PyTuple_SetItem(t, 1, buf);
|
||||||
Py_INCREF(t);
|
Py_INCREF(t);
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user