Merge branch 'py_send_binary3' of https://github.com/linsomniac/libzt into linsomniac-py_send_binary3
This commit is contained in:
@@ -193,40 +193,16 @@ PyObject * zts_py_recv(int fd, int len, int flags)
|
|||||||
|
|
||||||
int zts_py_send(int fd, PyObject *buf, int flags)
|
int zts_py_send(int fd, PyObject *buf, int flags)
|
||||||
{
|
{
|
||||||
int bytes_sent = ZTS_ERR_OK;
|
Py_buffer output;
|
||||||
char *bytes = NULL;
|
int bytes_sent;
|
||||||
PyObject *encodedStr = NULL;
|
|
||||||
|
|
||||||
// Check for various encodings, or lack thereof
|
if (PyObject_GetBuffer(buf, &output, PyBUF_SIMPLE) != 0) {
|
||||||
|
return 0;
|
||||||
if (PyByteArray_Check(buf)) {
|
|
||||||
bytes = PyByteArray_AsString(buf);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (PyUnicode_Check(buf)) {
|
bytes_sent = zts_send(fd, output.buf, output.len, flags);
|
||||||
encodedStr = PyUnicode_AsEncodedString(buf, "UTF-8", "strict");
|
PyBuffer_Release(&output);
|
||||||
if (!encodedStr) {
|
|
||||||
return ZTS_ERR_ARG;
|
|
||||||
}
|
|
||||||
bytes = PyBytes_AsString(encodedStr);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!bytes) {
|
|
||||||
// No encoding detected
|
|
||||||
bytes = PyBytes_AsString(buf);
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we still don't have a valid pointer to a C-string, fail
|
|
||||||
if (!bytes) {
|
|
||||||
bytes_sent = ZTS_ERR_ARG;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
bytes_sent = zts_send(fd, bytes, strlen(bytes), flags);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (encodedStr) {
|
|
||||||
Py_DECREF(encodedStr);
|
|
||||||
}
|
|
||||||
return bytes_sent;
|
return bytes_sent;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user