mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
gh-129813, PEP 782: Use PyBytesWriter in socket and mmap (#138831)
Replace PyBytes_FromStringAndSize(NULL, size) and _PyBytes_Resize() with the new public PyBytesWriter API.
This commit is contained in:
parent
06b7891f12
commit
bb743b684b
2 changed files with 14 additions and 13 deletions
|
|
@ -3467,7 +3467,6 @@ sock_getsockopt(PyObject *self, PyObject *args)
|
|||
int level;
|
||||
int optname;
|
||||
int res;
|
||||
PyObject *buf;
|
||||
socklen_t buflen = 0;
|
||||
int flag = 0;
|
||||
socklen_t flagsize;
|
||||
|
|
@ -3512,17 +3511,17 @@ sock_getsockopt(PyObject *self, PyObject *args)
|
|||
"getsockopt buflen out of range");
|
||||
return NULL;
|
||||
}
|
||||
buf = PyBytes_FromStringAndSize((char *)NULL, buflen);
|
||||
if (buf == NULL)
|
||||
PyBytesWriter *writer = PyBytesWriter_Create(buflen);
|
||||
if (writer == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
res = getsockopt(get_sock_fd(s), level, optname,
|
||||
(void *)PyBytes_AS_STRING(buf), &buflen);
|
||||
PyBytesWriter_GetData(writer), &buflen);
|
||||
if (res < 0) {
|
||||
Py_DECREF(buf);
|
||||
PyBytesWriter_Discard(writer);
|
||||
return s->errorhandler();
|
||||
}
|
||||
_PyBytes_Resize(&buf, buflen);
|
||||
return buf;
|
||||
return PyBytesWriter_FinishWithSize(writer, buflen);
|
||||
}
|
||||
|
||||
PyDoc_STRVAR(getsockopt_doc,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue