Skip to content

Commit d918d4a

Browse files
committed
gh-155742: Use PyBytesWriter in _io _textiowrapper_writeflush()
Replace soft deprecated PyBytes_FromStringAndSize() with PyBytesWriter. Replace PyBytes_AsStringAndSize() with PyBytes_AS_STRING() and PyBytes_GET_SIZE().
1 parent cd98657 commit d918d4a

1 file changed

Lines changed: 11 additions & 8 deletions

File tree

Modules/_io/textio.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1671,17 +1671,17 @@ _textiowrapper_writeflush(textio *self)
16711671
}
16721672
else {
16731673
assert(PyList_Check(pending));
1674-
b = PyBytes_FromStringAndSize(NULL, self->pending_bytes_count);
1675-
if (b == NULL) {
1674+
PyBytesWriter *writer = PyBytesWriter_Create(self->pending_bytes_count);
1675+
if (writer == NULL) {
16761676
return -1;
16771677
}
16781678

1679-
char *buf = PyBytes_AsString(b);
1679+
char *buf = PyBytesWriter_GetData(writer);
16801680
Py_ssize_t pos = 0;
16811681

16821682
for (Py_ssize_t i = 0; i < PyList_GET_SIZE(pending); i++) {
16831683
PyObject *obj = PyList_GET_ITEM(pending, i);
1684-
char *src;
1684+
const char *src;
16851685
Py_ssize_t len;
16861686
if (PyUnicode_Check(obj)) {
16871687
assert(PyUnicode_IS_ASCII(obj));
@@ -1690,15 +1690,18 @@ _textiowrapper_writeflush(textio *self)
16901690
}
16911691
else {
16921692
assert(PyBytes_Check(obj));
1693-
if (PyBytes_AsStringAndSize(obj, &src, &len) < 0) {
1694-
Py_DECREF(b);
1695-
return -1;
1696-
}
1693+
src = PyBytes_AS_STRING(obj);
1694+
len = PyBytes_GET_SIZE(obj);
16971695
}
16981696
memcpy(buf + pos, src, len);
16991697
pos += len;
17001698
}
17011699
assert(pos == self->pending_bytes_count);
1700+
1701+
b = PyBytesWriter_Finish(writer);
1702+
if (b == NULL) {
1703+
return -1;
1704+
}
17021705
}
17031706

17041707
self->pending_bytes_count = 0;

0 commit comments

Comments
 (0)