@@ -42,24 +42,26 @@ _getbytevalue(PyObject* arg, int *value)
42
42
}
43
43
44
44
static int
45
- bytearray_getbuffer (PyByteArrayObject * obj , Py_buffer * view , int flags )
45
+ bytearray_getbuffer (PyObject * self , Py_buffer * view , int flags )
46
46
{
47
- void * ptr ;
47
+ PyByteArrayObject * obj = _PyByteArray_CAST ( self ) ;
48
48
if (view == NULL ) {
49
49
PyErr_SetString (PyExc_BufferError ,
50
50
"bytearray_getbuffer: view==NULL argument is obsolete" );
51
51
return -1 ;
52
52
}
53
- ptr = (void * ) PyByteArray_AS_STRING (obj );
53
+
54
+ void * ptr = (void * ) PyByteArray_AS_STRING (obj );
54
55
/* cannot fail if view != NULL and readonly == 0 */
55
56
(void )PyBuffer_FillInfo (view , (PyObject * )obj , ptr , Py_SIZE (obj ), 0 , flags );
56
57
obj -> ob_exports ++ ;
57
58
return 0 ;
58
59
}
59
60
60
61
static void
61
- bytearray_releasebuffer (PyByteArrayObject * obj , Py_buffer * view )
62
+ bytearray_releasebuffer (PyObject * self , Py_buffer * view )
62
63
{
64
+ PyByteArrayObject * obj = _PyByteArray_CAST (self );
63
65
obj -> ob_exports -- ;
64
66
assert (obj -> ob_exports >= 0 );
65
67
}
@@ -286,46 +288,53 @@ PyByteArray_Concat(PyObject *a, PyObject *b)
286
288
/* Functions stuffed into the type object */
287
289
288
290
static Py_ssize_t
289
- bytearray_length (PyByteArrayObject * self )
291
+ bytearray_length (PyObject * op )
290
292
{
293
+ PyByteArrayObject * self = _PyByteArray_CAST (op );
291
294
return Py_SIZE (self );
292
295
}
293
296
294
297
static PyObject *
295
- bytearray_iconcat (PyByteArrayObject * self , PyObject * other )
298
+ bytearray_iconcat (PyObject * op , PyObject * other )
296
299
{
297
- Py_ssize_t size ;
298
- Py_buffer vo ;
300
+ PyByteArrayObject * self = _PyByteArray_CAST (op );
299
301
302
+ Py_buffer vo ;
300
303
if (PyObject_GetBuffer (other , & vo , PyBUF_SIMPLE ) != 0 ) {
301
304
PyErr_Format (PyExc_TypeError , "can't concat %.100s to %.100s" ,
302
305
Py_TYPE (other )-> tp_name , Py_TYPE (self )-> tp_name );
303
306
return NULL ;
304
307
}
305
308
306
- size = Py_SIZE (self );
309
+ Py_ssize_t size = Py_SIZE (self );
307
310
if (size > PY_SSIZE_T_MAX - vo .len ) {
308
311
PyBuffer_Release (& vo );
309
312
return PyErr_NoMemory ();
310
313
}
314
+
311
315
if (PyByteArray_Resize ((PyObject * )self , size + vo .len ) < 0 ) {
312
316
PyBuffer_Release (& vo );
313
317
return NULL ;
314
318
}
319
+
315
320
memcpy (PyByteArray_AS_STRING (self ) + size , vo .buf , vo .len );
316
321
PyBuffer_Release (& vo );
317
322
return Py_NewRef (self );
318
323
}
319
324
320
325
static PyObject *
321
- bytearray_repeat (PyByteArrayObject * self , Py_ssize_t count )
326
+ bytearray_repeat (PyObject * op , Py_ssize_t count )
322
327
{
323
- if (count < 0 )
328
+ PyByteArrayObject * self = _PyByteArray_CAST (op );
329
+ if (count < 0 ) {
324
330
count = 0 ;
331
+ }
325
332
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 ) {
327
334
return PyErr_NoMemory ();
335
+ }
328
336
Py_ssize_t size = mysize * count ;
337
+
329
338
PyByteArrayObject * result = (PyByteArrayObject * )PyByteArray_FromStringAndSize (NULL , size );
330
339
const char * buf = PyByteArray_AS_STRING (self );
331
340
if (result != NULL && size != 0 ) {
@@ -335,20 +344,24 @@ bytearray_repeat(PyByteArrayObject *self, Py_ssize_t count)
335
344
}
336
345
337
346
static PyObject *
338
- bytearray_irepeat (PyByteArrayObject * self , Py_ssize_t count )
347
+ bytearray_irepeat (PyObject * op , Py_ssize_t count )
339
348
{
340
- if (count < 0 )
349
+ PyByteArrayObject * self = _PyByteArray_CAST (op );
350
+ if (count < 0 ) {
341
351
count = 0 ;
352
+ }
342
353
else if (count == 1 ) {
343
354
return Py_NewRef (self );
344
355
}
345
356
346
357
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 ) {
348
359
return PyErr_NoMemory ();
360
+ }
349
361
const Py_ssize_t size = mysize * count ;
350
- if (PyByteArray_Resize ((PyObject * )self , size ) < 0 )
362
+ if (PyByteArray_Resize ((PyObject * )self , size ) < 0 ) {
351
363
return NULL ;
364
+ }
352
365
353
366
char * buf = PyByteArray_AS_STRING (self );
354
367
_PyBytes_Repeat (buf , size , buf , mysize );
@@ -357,8 +370,9 @@ bytearray_irepeat(PyByteArrayObject *self, Py_ssize_t count)
357
370
}
358
371
359
372
static PyObject *
360
- bytearray_getitem (PyByteArrayObject * self , Py_ssize_t i )
373
+ bytearray_getitem (PyObject * op , Py_ssize_t i )
361
374
{
375
+ PyByteArrayObject * self = _PyByteArray_CAST (op );
362
376
if (i < 0 || i >= Py_SIZE (self )) {
363
377
PyErr_SetString (PyExc_IndexError , "bytearray index out of range" );
364
378
return NULL ;
@@ -367,8 +381,9 @@ bytearray_getitem(PyByteArrayObject *self, Py_ssize_t i)
367
381
}
368
382
369
383
static PyObject *
370
- bytearray_subscript (PyByteArrayObject * self , PyObject * index )
384
+ bytearray_subscript (PyObject * op , PyObject * index )
371
385
{
386
+ PyByteArrayObject * self = _PyByteArray_CAST (op );
372
387
if (_PyIndex_Check (index )) {
373
388
Py_ssize_t i = PyNumber_AsSsize_t (index , PyExc_IndexError );
374
389
@@ -559,12 +574,13 @@ bytearray_setslice(PyByteArrayObject *self, Py_ssize_t lo, Py_ssize_t hi,
559
574
}
560
575
561
576
static int
562
- bytearray_setitem (PyByteArrayObject * self , Py_ssize_t i , PyObject * value )
577
+ bytearray_setitem (PyObject * op , Py_ssize_t i , PyObject * value )
563
578
{
564
- int ival = -1 ;
579
+ PyByteArrayObject * self = _PyByteArray_CAST ( op ) ;
565
580
566
581
// GH-91153: We need to do this *before* the size check, in case value has a
567
582
// nasty __index__ method that changes the size of the bytearray:
583
+ int ival = -1 ;
568
584
if (value && !_getbytevalue (value , & ival )) {
569
585
return -1 ;
570
586
}
@@ -588,11 +604,11 @@ bytearray_setitem(PyByteArrayObject *self, Py_ssize_t i, PyObject *value)
588
604
}
589
605
590
606
static int
591
- bytearray_ass_subscript (PyByteArrayObject * self , PyObject * index , PyObject * values )
607
+ bytearray_ass_subscript (PyObject * op , PyObject * index , PyObject * values )
592
608
{
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 );
596
612
597
613
if (_PyIndex_Check (index )) {
598
614
Py_ssize_t i = PyNumber_AsSsize_t (index , PyExc_IndexError );
@@ -645,6 +661,8 @@ bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *valu
645
661
return -1 ;
646
662
}
647
663
664
+ char * bytes ;
665
+ Py_ssize_t needed ;
648
666
if (values == NULL ) {
649
667
bytes = NULL ;
650
668
needed = 0 ;
@@ -661,7 +679,7 @@ bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *valu
661
679
values = PyByteArray_FromObject (values );
662
680
if (values == NULL )
663
681
return -1 ;
664
- err = bytearray_ass_subscript (self , index , values );
682
+ err = bytearray_ass_subscript (( PyObject * ) self , index , values );
665
683
Py_DECREF (values );
666
684
return err ;
667
685
}
@@ -670,10 +688,14 @@ bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *valu
670
688
bytes = PyByteArray_AS_STRING (values );
671
689
needed = Py_SIZE (values );
672
690
}
691
+
673
692
/* Make sure b[5:2] = ... inserts before 5, not before 2. */
674
693
if ((step < 0 && start < stop ) ||
675
694
(step > 0 && start > stop ))
695
+ {
676
696
stop = start ;
697
+ }
698
+
677
699
if (step == 1 ) {
678
700
return bytearray_setslice_linear (self , start , stop , bytes , needed );
679
701
}
@@ -785,7 +807,7 @@ bytearray___init___impl(PyByteArrayObject *self, PyObject *arg,
785
807
if (encoded == NULL )
786
808
return -1 ;
787
809
assert (PyBytes_Check (encoded ));
788
- new = bytearray_iconcat (self , encoded );
810
+ new = bytearray_iconcat (( PyObject * ) self , encoded );
789
811
Py_DECREF (encoded );
790
812
if (new == NULL )
791
813
return -1 ;
@@ -926,8 +948,9 @@ bytearray___init___impl(PyByteArrayObject *self, PyObject *arg,
926
948
/* Mostly copied from string_repr, but without the
927
949
"smart quote" functionality. */
928
950
static PyObject *
929
- bytearray_repr (PyByteArrayObject * self )
951
+ bytearray_repr (PyObject * op )
930
952
{
953
+ PyByteArrayObject * self = _PyByteArray_CAST (op );
931
954
const char * className = _PyType_Name (Py_TYPE (self ));
932
955
const char * quote_prefix = "(b" ;
933
956
const char * quote_postfix = ")" ;
@@ -1021,7 +1044,7 @@ bytearray_str(PyObject *op)
1021
1044
return NULL ;
1022
1045
}
1023
1046
}
1024
- return bytearray_repr (( PyByteArrayObject * ) op );
1047
+ return bytearray_repr (op );
1025
1048
}
1026
1049
1027
1050
static PyObject *
@@ -1080,8 +1103,9 @@ bytearray_richcompare(PyObject *self, PyObject *other, int op)
1080
1103
}
1081
1104
1082
1105
static void
1083
- bytearray_dealloc (PyByteArrayObject * self )
1106
+ bytearray_dealloc (PyObject * op )
1084
1107
{
1108
+ PyByteArrayObject * self = _PyByteArray_CAST (op );
1085
1109
if (self -> ob_exports > 0 ) {
1086
1110
PyErr_SetString (PyExc_SystemError ,
1087
1111
"deallocated bytearray object has exported buffers" );
@@ -1244,7 +1268,9 @@ bytearray_rindex_impl(PyByteArrayObject *self, PyObject *sub,
1244
1268
static int
1245
1269
bytearray_contains (PyObject * self , PyObject * arg )
1246
1270
{
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 );
1248
1274
}
1249
1275
1250
1276
/*[clinic input]
@@ -2262,31 +2288,30 @@ bytearray_sizeof_impl(PyByteArrayObject *self)
2262
2288
}
2263
2289
2264
2290
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 */
2269
2295
0 , /* sq_slice */
2270
- ( ssizeobjargproc ) bytearray_setitem , /* sq_ass_item */
2296
+ bytearray_setitem , /* sq_ass_item */
2271
2297
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 */
2275
2301
};
2276
2302
2277
2303
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 ,
2281
2307
};
2282
2308
2283
2309
static PyBufferProcs bytearray_as_buffer = {
2284
- ( getbufferproc ) bytearray_getbuffer ,
2285
- ( releasebufferproc ) bytearray_releasebuffer ,
2310
+ bytearray_getbuffer ,
2311
+ bytearray_releasebuffer ,
2286
2312
};
2287
2313
2288
- static PyMethodDef
2289
- bytearray_methods [] = {
2314
+ static PyMethodDef bytearray_methods [] = {
2290
2315
{"__alloc__" , (PyCFunction )bytearray_alloc , METH_NOARGS , alloc_doc },
2291
2316
BYTEARRAY_REDUCE_METHODDEF
2292
2317
BYTEARRAY_REDUCE_EX_METHODDEF
@@ -2391,12 +2416,12 @@ PyTypeObject PyByteArray_Type = {
2391
2416
"bytearray" ,
2392
2417
sizeof (PyByteArrayObject ),
2393
2418
0 ,
2394
- ( destructor ) bytearray_dealloc , /* tp_dealloc */
2419
+ bytearray_dealloc , /* tp_dealloc */
2395
2420
0 , /* tp_vectorcall_offset */
2396
2421
0 , /* tp_getattr */
2397
2422
0 , /* tp_setattr */
2398
2423
0 , /* tp_as_async */
2399
- ( reprfunc ) bytearray_repr , /* tp_repr */
2424
+ bytearray_repr , /* tp_repr */
2400
2425
& bytearray_as_number , /* tp_as_number */
2401
2426
& bytearray_as_sequence , /* tp_as_sequence */
2402
2427
& bytearray_as_mapping , /* tp_as_mapping */
@@ -2411,7 +2436,7 @@ PyTypeObject PyByteArray_Type = {
2411
2436
bytearray_doc , /* tp_doc */
2412
2437
0 , /* tp_traverse */
2413
2438
0 , /* tp_clear */
2414
- ( richcmpfunc ) bytearray_richcompare , /* tp_richcompare */
2439
+ bytearray_richcompare , /* tp_richcompare */
2415
2440
0 , /* tp_weaklistoffset */
2416
2441
bytearray_iter , /* tp_iter */
2417
2442
0 , /* tp_iternext */
0 commit comments