Skip to content

Commit 86c93a5

Browse files
authored
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().
1 parent e66424a commit 86c93a5

5 files changed

Lines changed: 16 additions & 13 deletions

File tree

Modules/_io/bytesio.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ peek_bytes_lock_held(bytesio *self, Py_ssize_t size)
486486
is beyond the size of self->buf. Assert above validates size is always in
487487
bounds. When self->pos is out of bounds calling code sets size to 0. */
488488
if (size == 0) {
489-
return PyBytes_FromStringAndSize(NULL, 0);
489+
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
490490
}
491491

492492
output = PyBytes_AS_STRING(self->buf) + self->pos;
@@ -1109,7 +1109,7 @@ bytesio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
11091109
/* tp_alloc initializes all the fields to zero. So we don't have to
11101110
initialize them here. */
11111111

1112-
self->buf = PyBytes_FromStringAndSize(NULL, 0);
1112+
self->buf = Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
11131113
if (self->buf == NULL) {
11141114
Py_DECREF(self);
11151115
return PyErr_NoMemory();

Modules/_io/winconsoleio.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,7 @@ _io__WindowsConsoleIO_readall_impl(winconsoleio *self)
942942
if (len == 0 && _buflen(self) == 0) {
943943
/* when the result starts with ^Z we return an empty buffer */
944944
PyMem_Free(buf);
945-
return PyBytes_FromStringAndSize(NULL, 0);
945+
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
946946
}
947947

948948
if (len) {

Modules/_sqlite/blob.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ subscript_slice(pysqlite_Blob *self, PyObject *item)
447447
}
448448

449449
if (len == 0) {
450-
return PyBytes_FromStringAndSize(NULL, 0);
450+
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
451451
}
452452

453453
if (step == 1) {

Objects/object.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3464,7 +3464,7 @@ _Py_GetConstant_Init(void)
34643464
constants[Py_CONSTANT_ZERO] = _PyLong_GetZero();
34653465
constants[Py_CONSTANT_ONE] = _PyLong_GetOne();
34663466
constants[Py_CONSTANT_EMPTY_STR] = PyUnicode_New(0, 0);
3467-
constants[Py_CONSTANT_EMPTY_BYTES] = PyBytes_FromStringAndSize(NULL, 0);
3467+
constants[Py_CONSTANT_EMPTY_BYTES] = PyBytes_FromStringAndSize("", 0);
34683468
constants[Py_CONSTANT_EMPTY_TUPLE] = PyTuple_New(0);
34693469
#ifndef NDEBUG
34703470
for (size_t i=0; i < Py_ARRAY_LENGTH(constants); i++) {

Python/pystrhex.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,15 +168,16 @@ _Py_strhex_impl(const char* argbuf, Py_ssize_t arglen,
168168
abs_bytes_per_sep = 0;
169169
}
170170

171-
PyObject *retval;
171+
PyObject *retval = NULL;
172+
PyBytesWriter *bytes_writer = NULL;
172173
Py_UCS1 *retbuf;
173174
if (return_bytes) {
174175
/* If _PyBytes_FromSize() were public we could avoid malloc+copy. */
175-
retval = PyBytes_FromStringAndSize(NULL, resultlen);
176-
if (!retval) {
176+
bytes_writer = PyBytesWriter_Create(resultlen);
177+
if (!bytes_writer) {
177178
return NULL;
178179
}
179-
retbuf = (Py_UCS1 *)PyBytes_AS_STRING(retval);
180+
retbuf = PyBytesWriter_GetData(bytes_writer);
180181
}
181182
else {
182183
retval = PyUnicode_New(resultlen, 127);
@@ -244,13 +245,15 @@ _Py_strhex_impl(const char* argbuf, Py_ssize_t arglen,
244245
}
245246
}
246247

248+
if (return_bytes) {
249+
return PyBytesWriter_Finish(bytes_writer);
250+
}
251+
else {
247252
#ifdef Py_DEBUG
248-
if (!return_bytes) {
249253
assert(_PyUnicode_CheckConsistency(retval, 1));
250-
}
251254
#endif
252-
253-
return retval;
255+
return retval;
256+
}
254257
}
255258

256259
PyObject * _Py_strhex(const char* argbuf, Py_ssize_t arglen)

0 commit comments

Comments
 (0)