Skip to content

Commit 4263bc3

Browse files
authored
gh-129813, PEP 782: Use PyBytesWriter in _socket (#139097)
Replace PyBytes_FromStringAndSize(NULL, size) and _PyBytes_Resize() with the new public PyBytesWriter API.
1 parent 3b83257 commit 4263bc3

File tree

1 file changed

+14
-22
lines changed

1 file changed

+14
-22
lines changed

Modules/socketmodule.c

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3978,7 +3978,6 @@ sock_recv(PyObject *self, PyObject *args)
39783978

39793979
Py_ssize_t recvlen, outlen;
39803980
int flags = 0;
3981-
PyObject *buf;
39823981

39833982
if (!PyArg_ParseTuple(args, "n|i:recv", &recvlen, &flags))
39843983
return NULL;
@@ -3990,25 +3989,21 @@ sock_recv(PyObject *self, PyObject *args)
39903989
}
39913990

39923991
/* Allocate a new string. */
3993-
buf = PyBytes_FromStringAndSize((char *) 0, recvlen);
3994-
if (buf == NULL)
3992+
PyBytesWriter *writer = PyBytesWriter_Create(recvlen);
3993+
if (writer == NULL) {
39953994
return NULL;
3995+
}
39963996

39973997
/* Call the guts */
3998-
outlen = sock_recv_guts(s, PyBytes_AS_STRING(buf), recvlen, flags);
3998+
outlen = sock_recv_guts(s, PyBytesWriter_GetData(writer), recvlen, flags);
39993999
if (outlen < 0) {
40004000
/* An error occurred, release the string and return an
40014001
error. */
4002-
Py_DECREF(buf);
4002+
PyBytesWriter_Discard(writer);
40034003
return NULL;
40044004
}
4005-
if (outlen != recvlen) {
4006-
/* We did not read as many bytes as we anticipated, resize the
4007-
string if possible and be successful. */
4008-
_PyBytes_Resize(&buf, outlen);
4009-
}
40104005

4011-
return buf;
4006+
return PyBytesWriter_FinishWithSize(writer, outlen);
40124007
}
40134008

40144009
PyDoc_STRVAR(recv_doc,
@@ -4164,7 +4159,6 @@ sock_recvfrom(PyObject *self, PyObject *args)
41644159
{
41654160
PySocketSockObject *s = _PySocketSockObject_CAST(self);
41664161

4167-
PyObject *buf = NULL;
41684162
PyObject *addr = NULL;
41694163
PyObject *ret = NULL;
41704164
int flags = 0;
@@ -4179,28 +4173,26 @@ sock_recvfrom(PyObject *self, PyObject *args)
41794173
return NULL;
41804174
}
41814175

4182-
buf = PyBytes_FromStringAndSize((char *) 0, recvlen);
4183-
if (buf == NULL)
4176+
PyBytesWriter *writer = PyBytesWriter_Create(recvlen);
4177+
if (writer == NULL) {
41844178
return NULL;
4179+
}
41854180

4186-
outlen = sock_recvfrom_guts(s, PyBytes_AS_STRING(buf),
4181+
outlen = sock_recvfrom_guts(s, PyBytesWriter_GetData(writer),
41874182
recvlen, flags, &addr);
41884183
if (outlen < 0) {
4184+
PyBytesWriter_Discard(writer);
41894185
goto finally;
41904186
}
41914187

4192-
if (outlen != recvlen) {
4193-
/* We did not read as many bytes as we anticipated, resize the
4194-
string if possible and be successful. */
4195-
if (_PyBytes_Resize(&buf, outlen) < 0)
4196-
/* Oopsy, not so successful after all. */
4197-
goto finally;
4188+
PyObject *buf = PyBytesWriter_FinishWithSize(writer, outlen);
4189+
if (buf == NULL) {
4190+
goto finally;
41984191
}
41994192

42004193
ret = PyTuple_Pack(2, buf, addr);
42014194

42024195
finally:
4203-
Py_XDECREF(buf);
42044196
Py_XDECREF(addr);
42054197
return ret;
42064198
}

0 commit comments

Comments
 (0)