@@ -3978,7 +3978,6 @@ sock_recv(PyObject *self, PyObject *args)
3978
3978
3979
3979
Py_ssize_t recvlen , outlen ;
3980
3980
int flags = 0 ;
3981
- PyObject * buf ;
3982
3981
3983
3982
if (!PyArg_ParseTuple (args , "n|i:recv" , & recvlen , & flags ))
3984
3983
return NULL ;
@@ -3990,25 +3989,21 @@ sock_recv(PyObject *self, PyObject *args)
3990
3989
}
3991
3990
3992
3991
/* 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 ) {
3995
3994
return NULL ;
3995
+ }
3996
3996
3997
3997
/* 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 );
3999
3999
if (outlen < 0 ) {
4000
4000
/* An error occurred, release the string and return an
4001
4001
error. */
4002
- Py_DECREF ( buf );
4002
+ PyBytesWriter_Discard ( writer );
4003
4003
return NULL ;
4004
4004
}
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
- }
4010
4005
4011
- return buf ;
4006
+ return PyBytesWriter_FinishWithSize ( writer , outlen ) ;
4012
4007
}
4013
4008
4014
4009
PyDoc_STRVAR (recv_doc ,
@@ -4164,7 +4159,6 @@ sock_recvfrom(PyObject *self, PyObject *args)
4164
4159
{
4165
4160
PySocketSockObject * s = _PySocketSockObject_CAST (self );
4166
4161
4167
- PyObject * buf = NULL ;
4168
4162
PyObject * addr = NULL ;
4169
4163
PyObject * ret = NULL ;
4170
4164
int flags = 0 ;
@@ -4179,28 +4173,26 @@ sock_recvfrom(PyObject *self, PyObject *args)
4179
4173
return NULL ;
4180
4174
}
4181
4175
4182
- buf = PyBytes_FromStringAndSize (( char * ) 0 , recvlen );
4183
- if (buf == NULL )
4176
+ PyBytesWriter * writer = PyBytesWriter_Create ( recvlen );
4177
+ if (writer == NULL ) {
4184
4178
return NULL ;
4179
+ }
4185
4180
4186
- outlen = sock_recvfrom_guts (s , PyBytes_AS_STRING ( buf ),
4181
+ outlen = sock_recvfrom_guts (s , PyBytesWriter_GetData ( writer ),
4187
4182
recvlen , flags , & addr );
4188
4183
if (outlen < 0 ) {
4184
+ PyBytesWriter_Discard (writer );
4189
4185
goto finally ;
4190
4186
}
4191
4187
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 ;
4198
4191
}
4199
4192
4200
4193
ret = PyTuple_Pack (2 , buf , addr );
4201
4194
4202
4195
finally :
4203
- Py_XDECREF (buf );
4204
4196
Py_XDECREF (addr );
4205
4197
return ret ;
4206
4198
}
0 commit comments