gh-111178: Fix function signatures in bytearrayobject.c (#124940) · python/cpython@aace0dc
@@ -42,24 +42,26 @@ _getbytevalue(PyObject* arg, int *value)
4242}
43434444static int
45-bytearray_getbuffer(PyByteArrayObject *obj, Py_buffer *view, int flags)
45+bytearray_getbuffer(PyObject *self, Py_buffer *view, int flags)
4646{
47-void *ptr;
47+PyByteArrayObject *obj = _PyByteArray_CAST(self);
4848if (view == NULL) {
4949PyErr_SetString(PyExc_BufferError,
5050"bytearray_getbuffer: view==NULL argument is obsolete");
5151return -1;
5252 }
53-ptr = (void *) PyByteArray_AS_STRING(obj);
53+54+void *ptr = (void *) PyByteArray_AS_STRING(obj);
5455/* cannot fail if view != NULL and readonly == 0 */
5556 (void)PyBuffer_FillInfo(view, (PyObject*)obj, ptr, Py_SIZE(obj), 0, flags);
5657obj->ob_exports++;
5758return 0;
5859}
59606061static void
61-bytearray_releasebuffer(PyByteArrayObject *obj, Py_buffer *view)
62+bytearray_releasebuffer(PyObject *self, Py_buffer *view)
6263{
64+PyByteArrayObject *obj = _PyByteArray_CAST(self);
6365obj->ob_exports--;
6466assert(obj->ob_exports >= 0);
6567}
@@ -286,46 +288,53 @@ PyByteArray_Concat(PyObject *a, PyObject *b)
286288/* Functions stuffed into the type object */
287289288290static Py_ssize_t
289-bytearray_length(PyByteArrayObject *self)
291+bytearray_length(PyObject *op)
290292{
293+PyByteArrayObject *self = _PyByteArray_CAST(op);
291294return Py_SIZE(self);
292295}
293296294297static PyObject *
295-bytearray_iconcat(PyByteArrayObject *self, PyObject *other)
298+bytearray_iconcat(PyObject *op, PyObject *other)
296299{
297-Py_ssize_t size;
298-Py_buffer vo;
300+PyByteArrayObject *self = _PyByteArray_CAST(op);
299301302+Py_buffer vo;
300303if (PyObject_GetBuffer(other, &vo, PyBUF_SIMPLE) != 0) {
301304PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
302305Py_TYPE(other)->tp_name, Py_TYPE(self)->tp_name);
303306return NULL;
304307 }
305308306-size = Py_SIZE(self);
309+Py_ssize_t size = Py_SIZE(self);
307310if (size > PY_SSIZE_T_MAX - vo.len) {
308311PyBuffer_Release(&vo);
309312return PyErr_NoMemory();
310313 }
314+311315if (PyByteArray_Resize((PyObject *)self, size + vo.len) < 0) {
312316PyBuffer_Release(&vo);
313317return NULL;
314318 }
319+315320memcpy(PyByteArray_AS_STRING(self) + size, vo.buf, vo.len);
316321PyBuffer_Release(&vo);
317322return Py_NewRef(self);
318323}
319324320325static PyObject *
321-bytearray_repeat(PyByteArrayObject *self, Py_ssize_t count)
326+bytearray_repeat(PyObject *op, Py_ssize_t count)
322327{
323-if (count < 0)
328+PyByteArrayObject *self = _PyByteArray_CAST(op);
329+if (count < 0) {
324330count = 0;
331+ }
325332const Py_ssize_t mysize = Py_SIZE(self);
326-if (count > 0 && mysize > PY_SSIZE_T_MAX / count)
333+if (count > 0 && mysize > PY_SSIZE_T_MAX / count) {
327334return PyErr_NoMemory();
335+ }
328336Py_ssize_t size = mysize * count;
337+329338PyByteArrayObject* result = (PyByteArrayObject *)PyByteArray_FromStringAndSize(NULL, size);
330339const char* buf = PyByteArray_AS_STRING(self);
331340if (result != NULL && size != 0) {
@@ -335,20 +344,24 @@ bytearray_repeat(PyByteArrayObject *self, Py_ssize_t count)
335344}
336345337346static PyObject *
338-bytearray_irepeat(PyByteArrayObject *self, Py_ssize_t count)
347+bytearray_irepeat(PyObject *op, Py_ssize_t count)
339348{
340-if (count < 0)
349+PyByteArrayObject *self = _PyByteArray_CAST(op);
350+if (count < 0) {
341351count = 0;
352+ }
342353else if (count == 1) {
343354return Py_NewRef(self);
344355 }
345356346357const Py_ssize_t mysize = Py_SIZE(self);
347-if (count > 0 && mysize > PY_SSIZE_T_MAX / count)
358+if (count > 0 && mysize > PY_SSIZE_T_MAX / count) {
348359return PyErr_NoMemory();
360+ }
349361const Py_ssize_t size = mysize * count;
350-if (PyByteArray_Resize((PyObject *)self, size) < 0)
362+if (PyByteArray_Resize((PyObject *)self, size) < 0) {
351363return NULL;
364+ }
352365353366char* buf = PyByteArray_AS_STRING(self);
354367_PyBytes_Repeat(buf, size, buf, mysize);
@@ -357,8 +370,9 @@ bytearray_irepeat(PyByteArrayObject *self, Py_ssize_t count)
357370}
358371359372static PyObject *
360-bytearray_getitem(PyByteArrayObject *self, Py_ssize_t i)
373+bytearray_getitem(PyObject *op, Py_ssize_t i)
361374{
375+PyByteArrayObject *self = _PyByteArray_CAST(op);
362376if (i < 0 || i >= Py_SIZE(self)) {
363377PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
364378return NULL;
@@ -367,8 +381,9 @@ bytearray_getitem(PyByteArrayObject *self, Py_ssize_t i)
367381}
368382369383static PyObject *
370-bytearray_subscript(PyByteArrayObject *self, PyObject *index)
384+bytearray_subscript(PyObject *op, PyObject *index)
371385{
386+PyByteArrayObject *self = _PyByteArray_CAST(op);
372387if (_PyIndex_Check(index)) {
373388Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
374389@@ -559,12 +574,13 @@ bytearray_setslice(PyByteArrayObject *self, Py_ssize_t lo, Py_ssize_t hi,
559574}
560575561576static int
562-bytearray_setitem(PyByteArrayObject *self, Py_ssize_t i, PyObject *value)
577+bytearray_setitem(PyObject *op, Py_ssize_t i, PyObject *value)
563578{
564-int ival = -1;
579+PyByteArrayObject *self = _PyByteArray_CAST(op);
565580566581// GH-91153: We need to do this *before* the size check, in case value has a
567582// nasty __index__ method that changes the size of the bytearray:
583+int ival = -1;
568584if (value && !_getbytevalue(value, &ival)) {
569585return -1;
570586 }
@@ -588,11 +604,11 @@ bytearray_setitem(PyByteArrayObject *self, Py_ssize_t i, PyObject *value)
588604}
589605590606static int
591-bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *values)
607+bytearray_ass_subscript(PyObject *op, PyObject *index, PyObject *values)
592608{
593-Py_ssize_t start, stop, step, slicelen, needed;
594-char *buf, *bytes;
595-buf = PyByteArray_AS_STRING(self);
609+PyByteArrayObject *self = _PyByteArray_CAST(op);
610+Py_ssize_t start, stop, step, slicelen;
611+char *buf = PyByteArray_AS_STRING(self);
596612597613if (_PyIndex_Check(index)) {
598614Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
@@ -645,6 +661,8 @@ bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *valu
645661return -1;
646662 }
647663664+char *bytes;
665+Py_ssize_t needed;
648666if (values == NULL) {
649667bytes = NULL;
650668needed = 0;
@@ -661,7 +679,7 @@ bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *valu
661679values = PyByteArray_FromObject(values);
662680if (values == NULL)
663681return -1;
664-err = bytearray_ass_subscript(self, index, values);
682+err = bytearray_ass_subscript((PyObject*)self, index, values);
665683Py_DECREF(values);
666684return err;
667685 }
@@ -670,10 +688,14 @@ bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *valu
670688bytes = PyByteArray_AS_STRING(values);
671689needed = Py_SIZE(values);
672690 }
691+673692/* Make sure b[5:2] = ... inserts before 5, not before 2. */
674693if ((step < 0 && start < stop) ||
675694 (step > 0 && start > stop))
695+ {
676696stop = start;
697+ }
698+677699if (step == 1) {
678700return bytearray_setslice_linear(self, start, stop, bytes, needed);
679701 }
@@ -785,7 +807,7 @@ bytearray___init___impl(PyByteArrayObject *self, PyObject *arg,
785807if (encoded == NULL)
786808return -1;
787809assert(PyBytes_Check(encoded));
788-new = bytearray_iconcat(self, encoded);
810+new = bytearray_iconcat((PyObject*)self, encoded);
789811Py_DECREF(encoded);
790812if (new == NULL)
791813return -1;
@@ -926,8 +948,9 @@ bytearray___init___impl(PyByteArrayObject *self, PyObject *arg,
926948/* Mostly copied from string_repr, but without the
927949 "smart quote" functionality. */
928950static PyObject *
929-bytearray_repr(PyByteArrayObject *self)
951+bytearray_repr(PyObject *op)
930952{
953+PyByteArrayObject *self = _PyByteArray_CAST(op);
931954const char *className = _PyType_Name(Py_TYPE(self));
932955const char *quote_prefix = "(b";
933956const char *quote_postfix = ")";
@@ -1021,7 +1044,7 @@ bytearray_str(PyObject *op)
10211044return NULL;
10221045 }
10231046 }
1024-return bytearray_repr((PyByteArrayObject*)op);
1047+return bytearray_repr(op);
10251048}
1026104910271050static PyObject *
@@ -1080,8 +1103,9 @@ bytearray_richcompare(PyObject *self, PyObject *other, int op)
10801103}
1081110410821105static void
1083-bytearray_dealloc(PyByteArrayObject *self)
1106+bytearray_dealloc(PyObject *op)
10841107{
1108+PyByteArrayObject *self = _PyByteArray_CAST(op);
10851109if (self->ob_exports > 0) {
10861110PyErr_SetString(PyExc_SystemError,
10871111"deallocated bytearray object has exported buffers");
@@ -1244,7 +1268,9 @@ bytearray_rindex_impl(PyByteArrayObject *self, PyObject *sub,
12441268static int
12451269bytearray_contains(PyObject *self, PyObject *arg)
12461270{
1247-return _Py_bytes_contains(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), arg);
1271+return _Py_bytes_contains(PyByteArray_AS_STRING(self),
1272+PyByteArray_GET_SIZE(self),
1273+arg);
12481274}
1249127512501276/*[clinic input]
@@ -2262,31 +2288,30 @@ bytearray_sizeof_impl(PyByteArrayObject *self)
22622288}
2263228922642290static PySequenceMethods bytearray_as_sequence = {
2265-(lenfunc)bytearray_length, /* sq_length */
2266-(binaryfunc)PyByteArray_Concat, /* sq_concat */
2267-(ssizeargfunc)bytearray_repeat, /* sq_repeat */
2268-(ssizeargfunc)bytearray_getitem, /* sq_item */
2291+bytearray_length, /* sq_length */
2292+PyByteArray_Concat, /* sq_concat */
2293+bytearray_repeat, /* sq_repeat */
2294+bytearray_getitem, /* sq_item */
226922950, /* sq_slice */
2270-(ssizeobjargproc)bytearray_setitem, /* sq_ass_item */
2296+bytearray_setitem, /* sq_ass_item */
227122970, /* sq_ass_slice */
2272-(objobjproc)bytearray_contains, /* sq_contains */
2273-(binaryfunc)bytearray_iconcat, /* sq_inplace_concat */
2274-(ssizeargfunc)bytearray_irepeat, /* sq_inplace_repeat */
2298+bytearray_contains, /* sq_contains */
2299+bytearray_iconcat, /* sq_inplace_concat */
2300+bytearray_irepeat, /* sq_inplace_repeat */
22752301};
2276230222772303static PyMappingMethods bytearray_as_mapping = {
2278-(lenfunc)bytearray_length,
2279-(binaryfunc)bytearray_subscript,
2280-(objobjargproc)bytearray_ass_subscript,
2304+bytearray_length,
2305+bytearray_subscript,
2306+bytearray_ass_subscript,
22812307};
2282230822832309static PyBufferProcs bytearray_as_buffer = {
2284-(getbufferproc)bytearray_getbuffer,
2285-(releasebufferproc)bytearray_releasebuffer,
2310+bytearray_getbuffer,
2311+bytearray_releasebuffer,
22862312};
228723132288-static PyMethodDef
2289-bytearray_methods[] = {
2314+static PyMethodDef bytearray_methods[] = {
22902315 {"__alloc__", (PyCFunction)bytearray_alloc, METH_NOARGS, alloc_doc},
22912316BYTEARRAY_REDUCE_METHODDEF
22922317BYTEARRAY_REDUCE_EX_METHODDEF
@@ -2391,12 +2416,12 @@ PyTypeObject PyByteArray_Type = {
23912416"bytearray",
23922417sizeof(PyByteArrayObject),
239324180,
2394-(destructor)bytearray_dealloc, /* tp_dealloc */
2419+bytearray_dealloc, /* tp_dealloc */
239524200, /* tp_vectorcall_offset */
239624210, /* tp_getattr */
239724220, /* tp_setattr */
239824230, /* tp_as_async */
2399-(reprfunc)bytearray_repr, /* tp_repr */
2424+bytearray_repr, /* tp_repr */
24002425&bytearray_as_number, /* tp_as_number */
24012426&bytearray_as_sequence, /* tp_as_sequence */
24022427&bytearray_as_mapping, /* tp_as_mapping */
@@ -2411,7 +2436,7 @@ PyTypeObject PyByteArray_Type = {
24112436bytearray_doc, /* tp_doc */
241224370, /* tp_traverse */
241324380, /* tp_clear */
2414-(richcmpfunc)bytearray_richcompare, /* tp_richcompare */
2439+bytearray_richcompare, /* tp_richcompare */
241524400, /* tp_weaklistoffset */
24162441bytearray_iter, /* tp_iter */
241724420, /* tp_iternext */