Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove PyBytesWriter_GetAllocated()
  • Loading branch information
vstinner committed Feb 14, 2025
commit 7cb444b7893ece62e2b977183aa7419fb2d19da7
4 changes: 0 additions & 4 deletions Doc/c-api/bytes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,3 @@ PyBytesWriter
It is the difference between total allocated bytes (bytes allocated by
:c:func:`PyBytesWriter_Create` and :c:func:`PyBytesWriter_Extend`) and the
current position in the buffer.

.. c:function:: Py_ssize_t PyBytesWriter_GetAllocated(PyBytesWriter *writer)

Get the number of allocated bytes.
2 changes: 0 additions & 2 deletions Include/cpython/bytesobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ PyAPI_FUNC(PyObject*) PyBytesWriter_Finish(
PyAPI_FUNC(Py_ssize_t) PyBytesWriter_GetRemaining(
PyBytesWriter *writer,
void *buf);
PyAPI_FUNC(Py_ssize_t) PyBytesWriter_GetAllocated(
PyBytesWriter *writer);
PyAPI_FUNC(void*) PyBytesWriter_Extend(
PyBytesWriter *writer,
void *buf,
Expand Down
27 changes: 10 additions & 17 deletions Objects/bytesobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2833,6 +2833,7 @@ _PyBytes_FromList(PyObject *x)
return NULL;
}

Py_ssize_t extend = 1;
for (Py_ssize_t i = 0; i < PyList_GET_SIZE(x); i++) {
PyObject *item = PyList_GET_ITEM(x, i);
Py_INCREF(item);
Expand All @@ -2849,12 +2850,15 @@ _PyBytes_FromList(PyObject *x)

if (i >= size) {
// The list was extended by a previous PyNumber_AsSsize_t() call
Py_ssize_t extend = PyBytesWriter_GetAllocated(writer) - size + 1;
str = PyBytesWriter_Extend(writer, str, extend);
if (str == NULL) {
goto error;
}
size += extend;

if (extend <= PY_SSIZE_T_MAX / 2) {
extend *= 2;
}
}
*str++ = (char) value;
}
Expand Down Expand Up @@ -2910,6 +2914,7 @@ _PyBytes_FromIterator(PyObject *it, PyObject *x)
}

/* Run the iterator to exhaustion */
Py_ssize_t extend = 1;
for (Py_ssize_t i = 0; ; i++) {
PyObject *item;
Py_ssize_t value;
Expand Down Expand Up @@ -2938,12 +2943,15 @@ _PyBytes_FromIterator(PyObject *it, PyObject *x)
/* Append the byte */
if (i >= size) {
// The list was extended by a previous PyNumber_AsSsize_t() call
Py_ssize_t extend = PyBytesWriter_GetAllocated(writer) - size + 1;
str = PyBytesWriter_Extend(writer, str, extend);
if (str == NULL) {
goto error;
}
size += extend;

if (extend <= PY_SSIZE_T_MAX / 2) {
extend *= 2;
}
}
*str++ = (char) value;
}
Expand Down Expand Up @@ -3643,21 +3651,6 @@ PyBytesWriter_GetRemaining(PyBytesWriter *writer, void *buf)
}


Py_ssize_t
PyBytesWriter_GetAllocated(PyBytesWriter *writer)
{
if (writer->obj == NULL) {
return sizeof(writer->small_buffer);
}
else if (writer->use_bytearray) {
return PyByteArray_GET_SIZE(writer->obj);
}
else {
return PyBytes_GET_SIZE(writer->obj);
}
}


void*
PyBytesWriter_Extend(PyBytesWriter *writer, void *buf, Py_ssize_t extend)
{
Expand Down