https://github.com/python/cpython/commit/86c93a5b843eeec288b1557369f6f08745ae92ad
commit: 86c93a5b843eeec288b1557369f6f08745ae92ad
branch: main
author: Victor Stinner <[email protected]>
committer: vstinner <[email protected]>
date: 2026-09-18T14:09:09+02:00
summary:
gh-155742: Use PyBytesWriter in _Py_strhex_impl() (#157734)
Replace soft deprecated PyBytes_FromStringAndSize() with
PyBytesWriter.
Replace also PyBytes_FromStringAndSize(NULL, 0) with
Py_GetConstant(Py_CONSTANT_EMPTY_BYTES) in other functions.
Replace also PyBytes_FromStringAndSize(NULL, 0) with
PyBytes_FromStringAndSize("", 0) in _Py_GetConstant_Init().
files:
M Modules/_io/bytesio.c
M Modules/_io/winconsoleio.c
M Modules/_sqlite/blob.c
M Objects/object.c
M Python/pystrhex.c
diff --git a/Modules/_io/bytesio.c b/Modules/_io/bytesio.c
index b487d6d7beca93f..b7c1555c2637dd5 100644
--- a/Modules/_io/bytesio.c
+++ b/Modules/_io/bytesio.c
@@ -486,7 +486,7 @@ peek_bytes_lock_held(bytesio *self, Py_ssize_t size)
is beyond the size of self->buf. Assert above validates size is always
in
bounds. When self->pos is out of bounds calling code sets size to 0. */
if (size == 0) {
- return PyBytes_FromStringAndSize(NULL, 0);
+ return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
}
output = PyBytes_AS_STRING(self->buf) + self->pos;
@@ -1109,7 +1109,7 @@ bytesio_new(PyTypeObject *type, PyObject *args, PyObject
*kwds)
/* tp_alloc initializes all the fields to zero. So we don't have to
initialize them here. */
- self->buf = PyBytes_FromStringAndSize(NULL, 0);
+ self->buf = Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
if (self->buf == NULL) {
Py_DECREF(self);
return PyErr_NoMemory();
diff --git a/Modules/_io/winconsoleio.c b/Modules/_io/winconsoleio.c
index bc375e3dfe7de81..11e29227d9276e9 100644
--- a/Modules/_io/winconsoleio.c
+++ b/Modules/_io/winconsoleio.c
@@ -942,7 +942,7 @@ _io__WindowsConsoleIO_readall_impl(winconsoleio *self)
if (len == 0 && _buflen(self) == 0) {
/* when the result starts with ^Z we return an empty buffer */
PyMem_Free(buf);
- return PyBytes_FromStringAndSize(NULL, 0);
+ return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
}
if (len) {
diff --git a/Modules/_sqlite/blob.c b/Modules/_sqlite/blob.c
index 43cee9e0f308df7..ae318ca19fa0b8a 100644
--- a/Modules/_sqlite/blob.c
+++ b/Modules/_sqlite/blob.c
@@ -447,7 +447,7 @@ subscript_slice(pysqlite_Blob *self, PyObject *item)
}
if (len == 0) {
- return PyBytes_FromStringAndSize(NULL, 0);
+ return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
}
if (step == 1) {
diff --git a/Objects/object.c b/Objects/object.c
index e3f29b71301695e..971ac1b7a686693 100644
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -3464,7 +3464,7 @@ _Py_GetConstant_Init(void)
constants[Py_CONSTANT_ZERO] = _PyLong_GetZero();
constants[Py_CONSTANT_ONE] = _PyLong_GetOne();
constants[Py_CONSTANT_EMPTY_STR] = PyUnicode_New(0, 0);
- constants[Py_CONSTANT_EMPTY_BYTES] = PyBytes_FromStringAndSize(NULL, 0);
+ constants[Py_CONSTANT_EMPTY_BYTES] = PyBytes_FromStringAndSize("", 0);
constants[Py_CONSTANT_EMPTY_TUPLE] = PyTuple_New(0);
#ifndef NDEBUG
for (size_t i=0; i < Py_ARRAY_LENGTH(constants); i++) {
diff --git a/Python/pystrhex.c b/Python/pystrhex.c
index 8fb1fa36f85e739..ff6ee830fea7abc 100644
--- a/Python/pystrhex.c
+++ b/Python/pystrhex.c
@@ -168,15 +168,16 @@ _Py_strhex_impl(const char* argbuf, Py_ssize_t arglen,
abs_bytes_per_sep = 0;
}
- PyObject *retval;
+ PyObject *retval = NULL;
+ PyBytesWriter *bytes_writer = NULL;
Py_UCS1 *retbuf;
if (return_bytes) {
/* If _PyBytes_FromSize() were public we could avoid malloc+copy. */
- retval = PyBytes_FromStringAndSize(NULL, resultlen);
- if (!retval) {
+ bytes_writer = PyBytesWriter_Create(resultlen);
+ if (!bytes_writer) {
return NULL;
}
- retbuf = (Py_UCS1 *)PyBytes_AS_STRING(retval);
+ retbuf = PyBytesWriter_GetData(bytes_writer);
}
else {
retval = PyUnicode_New(resultlen, 127);
@@ -244,13 +245,15 @@ _Py_strhex_impl(const char* argbuf, Py_ssize_t arglen,
}
}
+ if (return_bytes) {
+ return PyBytesWriter_Finish(bytes_writer);
+ }
+ else {
#ifdef Py_DEBUG
- if (!return_bytes) {
assert(_PyUnicode_CheckConsistency(retval, 1));
- }
#endif
-
- return retval;
+ return retval;
+ }
}
PyObject * _Py_strhex(const char* argbuf, Py_ssize_t arglen)
_______________________________________________
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]