https://github.com/python/cpython/commit/d557d642d4272073bce05f3ce0bce28dd90288dd
commit: d557d642d4272073bce05f3ce0bce28dd90288dd
branch: main
author: Victor Stinner <[email protected]>
committer: vstinner <[email protected]>
date: 2026-09-02T12:48:00+02:00
summary:
gh-155742: Use PyBytesWriter in io _textiowrapper_writeflush() (#155743)
Replace soft deprecated PyBytes_FromStringAndSize() with
PyBytesWriter.
Replace PyBytes_AsStringAndSize() with PyBytes_AS_STRING() and
PyBytes_GET_SIZE().
files:
M Modules/_io/textio.c
diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c
index 5630f0309d98cff..a744a885932cdf5 100644
--- a/Modules/_io/textio.c
+++ b/Modules/_io/textio.c
@@ -1671,17 +1671,17 @@ _textiowrapper_writeflush(textio *self)
}
else {
assert(PyList_Check(pending));
- b = PyBytes_FromStringAndSize(NULL, self->pending_bytes_count);
- if (b == NULL) {
+ PyBytesWriter *writer =
PyBytesWriter_Create(self->pending_bytes_count);
+ if (writer == NULL) {
return -1;
}
- char *buf = PyBytes_AsString(b);
+ char *buf = PyBytesWriter_GetData(writer);
Py_ssize_t pos = 0;
for (Py_ssize_t i = 0; i < PyList_GET_SIZE(pending); i++) {
PyObject *obj = PyList_GET_ITEM(pending, i);
- char *src;
+ const char *src;
Py_ssize_t len;
if (PyUnicode_Check(obj)) {
assert(PyUnicode_IS_ASCII(obj));
@@ -1690,15 +1690,18 @@ _textiowrapper_writeflush(textio *self)
}
else {
assert(PyBytes_Check(obj));
- if (PyBytes_AsStringAndSize(obj, &src, &len) < 0) {
- Py_DECREF(b);
- return -1;
- }
+ src = PyBytes_AS_STRING(obj);
+ len = PyBytes_GET_SIZE(obj);
}
memcpy(buf + pos, src, len);
pos += len;
}
assert(pos == self->pending_bytes_count);
+
+ b = PyBytesWriter_Finish(writer);
+ if (b == NULL) {
+ return -1;
+ }
}
self->pending_bytes_count = 0;
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]