Skip to content

Commit 446587c

Browse files
authored
gh-129813, PEP 782: Use PyBytesWriter in _ssl (#138929)
Replace PyBytes_FromStringAndSize(NULL, size) and _PyBytes_Resize() with the new public PyBytesWriter API.
1 parent b0a8073 commit 446587c

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

Modules/_ssl.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2891,7 +2891,7 @@ _ssl__SSLSocket_read_impl(PySSLSocket *self, Py_ssize_t len,
28912891
int group_right_1, Py_buffer *buffer)
28922892
/*[clinic end generated code: output=49b16e6406023734 input=80ed30436df01a71]*/
28932893
{
2894-
PyObject *dest = NULL;
2894+
PyBytesWriter *writer = NULL;
28952895
char *mem;
28962896
size_t count = 0;
28972897
int retval;
@@ -2918,14 +2918,16 @@ _ssl__SSLSocket_read_impl(PySSLSocket *self, Py_ssize_t len,
29182918
}
29192919

29202920
if (!group_right_1) {
2921-
dest = PyBytes_FromStringAndSize(NULL, len);
2922-
if (dest == NULL)
2923-
goto error;
29242921
if (len == 0) {
29252922
Py_XDECREF(sock);
2926-
return dest;
2923+
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
2924+
}
2925+
2926+
writer = PyBytesWriter_Create(len);
2927+
if (writer == NULL) {
2928+
goto error;
29272929
}
2928-
mem = PyBytes_AS_STRING(dest);
2930+
mem = PyBytesWriter_GetData(writer);
29292931
}
29302932
else {
29312933
mem = buffer->buf;
@@ -3003,8 +3005,7 @@ _ssl__SSLSocket_read_impl(PySSLSocket *self, Py_ssize_t len,
30033005
done:
30043006
Py_XDECREF(sock);
30053007
if (!group_right_1) {
3006-
_PyBytes_Resize(&dest, count);
3007-
return dest;
3008+
return PyBytesWriter_FinishWithSize(writer, count);
30083009
}
30093010
else {
30103011
return PyLong_FromSize_t(count);
@@ -3013,8 +3014,9 @@ _ssl__SSLSocket_read_impl(PySSLSocket *self, Py_ssize_t len,
30133014
error:
30143015
PySSL_ChainExceptions(self);
30153016
Py_XDECREF(sock);
3016-
if (!group_right_1)
3017-
Py_XDECREF(dest);
3017+
if (!group_right_1) {
3018+
PyBytesWriter_Discard(writer);
3019+
}
30183020
return NULL;
30193021
}
30203022

0 commit comments

Comments
 (0)