Skip to content

Commit d6a6fe2

Browse files
authored
gh-129813, PEP 782: Use PyBytesWriter in ssl.MemoryBIO (#139113)
Replace PyBytes_FromStringAndSize(NULL, size) and _PyBytes_Resize() with the new public PyBytesWriter API.
1 parent 89ff88b commit d6a6fe2

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

Modules/_ssl.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5848,30 +5848,29 @@ _ssl_MemoryBIO_read_impl(PySSLMemoryBIO *self, int len)
58485848
/*[clinic end generated code: output=a657aa1e79cd01b3 input=21046f2d7dac3a90]*/
58495849
{
58505850
int avail, nbytes;
5851-
PyObject *result;
58525851

58535852
avail = (int)Py_MIN(BIO_ctrl_pending(self->bio), INT_MAX);
58545853
if ((len < 0) || (len > avail))
58555854
len = avail;
58565855

5857-
result = PyBytes_FromStringAndSize(NULL, len);
5858-
if ((result == NULL) || (len == 0))
5859-
return result;
5856+
if (len == 0) {
5857+
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
5858+
}
5859+
5860+
PyBytesWriter *writer = PyBytesWriter_Create(len);
5861+
if (writer == NULL) {
5862+
return NULL;
5863+
}
58605864

5861-
nbytes = BIO_read(self->bio, PyBytes_AS_STRING(result), len);
5865+
nbytes = BIO_read(self->bio, PyBytesWriter_GetData(writer), len);
58625866
if (nbytes < 0) {
58635867
_sslmodulestate *state = get_state_mbio(self);
5864-
Py_DECREF(result);
5868+
PyBytesWriter_Discard(writer);
58655869
_setSSLError(state, NULL, 0, __FILE__, __LINE__);
58665870
return NULL;
58675871
}
58685872

5869-
/* There should never be any short reads but check anyway. */
5870-
if (nbytes < len) {
5871-
_PyBytes_Resize(&result, nbytes);
5872-
}
5873-
5874-
return result;
5873+
return PyBytesWriter_FinishWithSize(writer, nbytes);
58755874
}
58765875

58775876
/*[clinic input]

0 commit comments

Comments
 (0)