gh-111178: Fix function signatures in bytearrayobject.c (#124940) · python/cpython@aace0dc

@@ -42,24 +42,26 @@ _getbytevalue(PyObject* arg, int *value)

4242

}

43434444

static 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);

4848

if (view == NULL) {

4949

PyErr_SetString(PyExc_BufferError,

5050

"bytearray_getbuffer: view==NULL argument is obsolete");

5151

return -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);

5657

obj->ob_exports++;

5758

return 0;

5859

}

59606061

static void

61-

bytearray_releasebuffer(PyByteArrayObject *obj, Py_buffer *view)

62+

bytearray_releasebuffer(PyObject *self, Py_buffer *view)

6263

{

64+

PyByteArrayObject *obj = _PyByteArray_CAST(self);

6365

obj->ob_exports--;

6466

assert(obj->ob_exports >= 0);

6567

}

@@ -286,46 +288,53 @@ PyByteArray_Concat(PyObject *a, PyObject *b)

286288

/* Functions stuffed into the type object */

287289288290

static Py_ssize_t

289-

bytearray_length(PyByteArrayObject *self)

291+

bytearray_length(PyObject *op)

290292

{

293+

PyByteArrayObject *self = _PyByteArray_CAST(op);

291294

return Py_SIZE(self);

292295

}

293296294297

static 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;

300303

if (PyObject_GetBuffer(other, &vo, PyBUF_SIMPLE) != 0) {

301304

PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",

302305

Py_TYPE(other)->tp_name, Py_TYPE(self)->tp_name);

303306

return NULL;

304307

}

305308306-

size = Py_SIZE(self);

309+

Py_ssize_t size = Py_SIZE(self);

307310

if (size > PY_SSIZE_T_MAX - vo.len) {

308311

PyBuffer_Release(&vo);

309312

return PyErr_NoMemory();

310313

}

314+311315

if (PyByteArray_Resize((PyObject *)self, size + vo.len) < 0) {

312316

PyBuffer_Release(&vo);

313317

return NULL;

314318

}

319+315320

memcpy(PyByteArray_AS_STRING(self) + size, vo.buf, vo.len);

316321

PyBuffer_Release(&vo);

317322

return Py_NewRef(self);

318323

}

319324320325

static 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) {

324330

count = 0;

331+

}

325332

const 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) {

327334

return PyErr_NoMemory();

335+

}

328336

Py_ssize_t size = mysize * count;

337+329338

PyByteArrayObject* result = (PyByteArrayObject *)PyByteArray_FromStringAndSize(NULL, size);

330339

const char* buf = PyByteArray_AS_STRING(self);

331340

if (result != NULL && size != 0) {

@@ -335,20 +344,24 @@ bytearray_repeat(PyByteArrayObject *self, Py_ssize_t count)

335344

}

336345337346

static 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) {

341351

count = 0;

352+

}

342353

else if (count == 1) {

343354

return Py_NewRef(self);

344355

}

345356346357

const 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) {

348359

return PyErr_NoMemory();

360+

}

349361

const Py_ssize_t size = mysize * count;

350-

if (PyByteArray_Resize((PyObject *)self, size) < 0)

362+

if (PyByteArray_Resize((PyObject *)self, size) < 0) {

351363

return NULL;

364+

}

352365353366

char* 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

}

358371359372

static 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);

362376

if (i < 0 || i >= Py_SIZE(self)) {

363377

PyErr_SetString(PyExc_IndexError, "bytearray index out of range");

364378

return NULL;

@@ -367,8 +381,9 @@ bytearray_getitem(PyByteArrayObject *self, Py_ssize_t i)

367381

}

368382369383

static PyObject *

370-

bytearray_subscript(PyByteArrayObject *self, PyObject *index)

384+

bytearray_subscript(PyObject *op, PyObject *index)

371385

{

386+

PyByteArrayObject *self = _PyByteArray_CAST(op);

372387

if (_PyIndex_Check(index)) {

373388

Py_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

}

560575561576

static 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;

568584

if (value && !_getbytevalue(value, &ival)) {

569585

return -1;

570586

}

@@ -588,11 +604,11 @@ bytearray_setitem(PyByteArrayObject *self, Py_ssize_t i, PyObject *value)

588604

}

589605590606

static 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);

596612597613

if (_PyIndex_Check(index)) {

598614

Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);

@@ -645,6 +661,8 @@ bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *valu

645661

return -1;

646662

}

647663664+

char *bytes;

665+

Py_ssize_t needed;

648666

if (values == NULL) {

649667

bytes = NULL;

650668

needed = 0;

@@ -661,7 +679,7 @@ bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *valu

661679

values = PyByteArray_FromObject(values);

662680

if (values == NULL)

663681

return -1;

664-

err = bytearray_ass_subscript(self, index, values);

682+

err = bytearray_ass_subscript((PyObject*)self, index, values);

665683

Py_DECREF(values);

666684

return err;

667685

}

@@ -670,10 +688,14 @@ bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *valu

670688

bytes = PyByteArray_AS_STRING(values);

671689

needed = Py_SIZE(values);

672690

}

691+673692

/* Make sure b[5:2] = ... inserts before 5, not before 2. */

674693

if ((step < 0 && start < stop) ||

675694

(step > 0 && start > stop))

695+

{

676696

stop = start;

697+

}

698+677699

if (step == 1) {

678700

return bytearray_setslice_linear(self, start, stop, bytes, needed);

679701

}

@@ -785,7 +807,7 @@ bytearray___init___impl(PyByteArrayObject *self, PyObject *arg,

785807

if (encoded == NULL)

786808

return -1;

787809

assert(PyBytes_Check(encoded));

788-

new = bytearray_iconcat(self, encoded);

810+

new = bytearray_iconcat((PyObject*)self, encoded);

789811

Py_DECREF(encoded);

790812

if (new == NULL)

791813

return -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. */

928950

static PyObject *

929-

bytearray_repr(PyByteArrayObject *self)

951+

bytearray_repr(PyObject *op)

930952

{

953+

PyByteArrayObject *self = _PyByteArray_CAST(op);

931954

const char *className = _PyType_Name(Py_TYPE(self));

932955

const char *quote_prefix = "(b";

933956

const char *quote_postfix = ")";

@@ -1021,7 +1044,7 @@ bytearray_str(PyObject *op)

10211044

return NULL;

10221045

}

10231046

}

1024-

return bytearray_repr((PyByteArrayObject*)op);

1047+

return bytearray_repr(op);

10251048

}

1026104910271050

static PyObject *

@@ -1080,8 +1103,9 @@ bytearray_richcompare(PyObject *self, PyObject *other, int op)

10801103

}

1081110410821105

static void

1083-

bytearray_dealloc(PyByteArrayObject *self)

1106+

bytearray_dealloc(PyObject *op)

10841107

{

1108+

PyByteArrayObject *self = _PyByteArray_CAST(op);

10851109

if (self->ob_exports > 0) {

10861110

PyErr_SetString(PyExc_SystemError,

10871111

"deallocated bytearray object has exported buffers");

@@ -1244,7 +1268,9 @@ bytearray_rindex_impl(PyByteArrayObject *self, PyObject *sub,

12441268

static int

12451269

bytearray_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

}

2263228922642290

static 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 */

22692295

0, /* sq_slice */

2270-

(ssizeobjargproc)bytearray_setitem, /* sq_ass_item */

2296+

bytearray_setitem, /* sq_ass_item */

22712297

0, /* 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

};

2276230222772303

static 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

};

2282230822832309

static 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},

22912316

BYTEARRAY_REDUCE_METHODDEF

22922317

BYTEARRAY_REDUCE_EX_METHODDEF

@@ -2391,12 +2416,12 @@ PyTypeObject PyByteArray_Type = {

23912416

"bytearray",

23922417

sizeof(PyByteArrayObject),

23932418

0,

2394-

(destructor)bytearray_dealloc, /* tp_dealloc */

2419+

bytearray_dealloc, /* tp_dealloc */

23952420

0, /* tp_vectorcall_offset */

23962421

0, /* tp_getattr */

23972422

0, /* tp_setattr */

23982423

0, /* 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 = {

24112436

bytearray_doc, /* tp_doc */

24122437

0, /* tp_traverse */

24132438

0, /* tp_clear */

2414-

(richcmpfunc)bytearray_richcompare, /* tp_richcompare */

2439+

bytearray_richcompare, /* tp_richcompare */

24152440

0, /* tp_weaklistoffset */

24162441

bytearray_iter, /* tp_iter */

24172442

0, /* tp_iternext */