Skip to content

Commit 4e27d13

Browse files
committed
Include PyBytesObject in __alloc__ of bytearray.
1 parent 7c6e8a8 commit 4e27d13

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

Objects/bytearrayobject.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2504,7 +2504,11 @@ static PyObject *
25042504
bytearray_alloc(PyObject *op, PyObject *Py_UNUSED(ignored))
25052505
{
25062506
PyByteArrayObject *self = _PyByteArray_CAST(op);
2507-
return PyLong_FromSsize_t(FT_ATOMIC_LOAD_SSIZE_RELAXED(self->ob_alloc));
2507+
Py_ssize_t alloc = FT_ATOMIC_LOAD_SSIZE_RELAXED(self->ob_alloc);
2508+
if (alloc > 0) {
2509+
alloc += sizeof(PyBytesObject);
2510+
}
2511+
return PyLong_FromSsize_t(alloc);
25082512
}
25092513

25102514
/*[clinic input]
@@ -2700,8 +2704,13 @@ static PyObject *
27002704
bytearray_sizeof_impl(PyByteArrayObject *self)
27012705
/*[clinic end generated code: output=738abdd17951c427 input=e27320fd98a4bc5a]*/
27022706
{
2703-
size_t res = _PyObject_SIZE(Py_TYPE(self));
2704-
res += (size_t)FT_ATOMIC_LOAD_SSIZE_RELAXED(self->ob_alloc) * sizeof(char);
2707+
Py_ssize_t res = _PyObject_SIZE(Py_TYPE(self));
2708+
Py_ssize_t alloc = FT_ATOMIC_LOAD_SSIZE_RELAXED(self->ob_alloc) * sizeof(char);
2709+
if (alloc > 0) {
2710+
res += sizeof(PyBytesObject);
2711+
res += alloc * sizeof(char);
2712+
}
2713+
27052714
return PyLong_FromSize_t(res);
27062715
}
27072716

0 commit comments

Comments
 (0)