@@ -462,19 +462,25 @@ buffer_repeat(PyBufferObject *self, Py_ssize_t count)
462
462
}
463
463
464
464
static PyObject *
465
- buffer_item ( PyBufferObject * self , Py_ssize_t idx )
465
+ buffer_item_impl ( void * ptr , Py_ssize_t size , Py_ssize_t idx )
466
466
{
467
- void * ptr ;
468
- Py_ssize_t size ;
469
- if (!get_buf (self , & ptr , & size , ANY_BUFFER ))
470
- return NULL ;
471
467
if ( idx < 0 || idx >= size ) {
472
468
PyErr_SetString (PyExc_IndexError , "buffer index out of range" );
473
469
return NULL ;
474
470
}
475
471
return PyString_FromStringAndSize ((char * )ptr + idx , 1 );
476
472
}
477
473
474
+ static PyObject *
475
+ buffer_item (PyBufferObject * self , Py_ssize_t idx )
476
+ {
477
+ void * ptr ;
478
+ Py_ssize_t size ;
479
+ if (!get_buf (self , & ptr , & size , ANY_BUFFER ))
480
+ return NULL ;
481
+ return buffer_item_impl (ptr , size , idx );
482
+ }
483
+
478
484
static PyObject *
479
485
buffer_slice (PyBufferObject * self , Py_ssize_t left , Py_ssize_t right )
480
486
{
@@ -500,24 +506,27 @@ buffer_subscript(PyBufferObject *self, PyObject *item)
500
506
void * p ;
501
507
Py_ssize_t size ;
502
508
503
- if (!get_buf (self , & p , & size , ANY_BUFFER ))
504
- return NULL ;
505
509
if (PyIndex_Check (item )) {
506
510
Py_ssize_t i = PyNumber_AsSsize_t (item , PyExc_IndexError );
507
511
if (i == -1 && PyErr_Occurred ())
508
512
return NULL ;
509
- if (i < 0 )
513
+ if (!get_buf (self , & p , & size , ANY_BUFFER ))
514
+ return NULL ;
515
+
516
+ if (i < 0 ) {
510
517
i += size ;
511
- return buffer_item (self , i );
518
+ }
519
+ return buffer_item_impl (p , size , i );
512
520
}
513
521
else if (PySlice_Check (item )) {
514
522
Py_ssize_t start , stop , step , slicelength , cur , i ;
515
523
516
- if (PySlice_GetIndicesEx ((PySliceObject * )item , size ,
517
- & start , & stop , & step , & slicelength ) < 0 ) {
524
+ if (_PySlice_Unpack (item , & start , & stop , & step ) < 0 )
525
+ return NULL ;
526
+ if (!get_buf (self , & p , & size , ANY_BUFFER ))
518
527
return NULL ;
519
- }
520
528
529
+ slicelength = _PySlice_AdjustIndices (size , & start , & stop , step );
521
530
if (slicelength <= 0 )
522
531
return PyString_FromStringAndSize ("" , 0 );
523
532
else if (step == 1 )
@@ -550,22 +559,12 @@ buffer_subscript(PyBufferObject *self, PyObject *item)
550
559
}
551
560
552
561
static int
553
- buffer_ass_item ( PyBufferObject * self , Py_ssize_t idx , PyObject * other )
562
+ buffer_ass_item_impl ( void * ptr1 , Py_ssize_t size , Py_ssize_t idx , PyObject * other )
554
563
{
555
564
PyBufferProcs * pb ;
556
- void * ptr1 , * ptr2 ;
557
- Py_ssize_t size ;
565
+ void * ptr2 ;
558
566
Py_ssize_t count ;
559
567
560
- if ( self -> b_readonly ) {
561
- PyErr_SetString (PyExc_TypeError ,
562
- "buffer is read-only" );
563
- return -1 ;
564
- }
565
-
566
- if (!get_buf (self , & ptr1 , & size , ANY_BUFFER ))
567
- return -1 ;
568
-
569
568
if (idx < 0 || idx >= size ) {
570
569
PyErr_SetString (PyExc_IndexError ,
571
570
"buffer assignment index out of range" );
@@ -600,6 +599,23 @@ buffer_ass_item(PyBufferObject *self, Py_ssize_t idx, PyObject *other)
600
599
return 0 ;
601
600
}
602
601
602
+ static int
603
+ buffer_ass_item (PyBufferObject * self , Py_ssize_t idx , PyObject * other )
604
+ {
605
+ void * ptr1 ;
606
+ Py_ssize_t size ;
607
+
608
+ if ( self -> b_readonly ) {
609
+ PyErr_SetString (PyExc_TypeError ,
610
+ "buffer is read-only" );
611
+ return -1 ;
612
+ }
613
+
614
+ if (!get_buf (self , & ptr1 , & size , ANY_BUFFER ))
615
+ return -1 ;
616
+ return buffer_ass_item_impl (ptr1 , size , idx , other );
617
+ }
618
+
603
619
static int
604
620
buffer_ass_slice (PyBufferObject * self , Py_ssize_t left , Py_ssize_t right , PyObject * other )
605
621
{
@@ -687,23 +703,26 @@ buffer_ass_subscript(PyBufferObject *self, PyObject *item, PyObject *value)
687
703
"single-segment buffer object expected" );
688
704
return -1 ;
689
705
}
690
- if (!get_buf (self , & ptr1 , & selfsize , ANY_BUFFER ))
691
- return -1 ;
692
706
if (PyIndex_Check (item )) {
693
707
Py_ssize_t i = PyNumber_AsSsize_t (item , PyExc_IndexError );
694
708
if (i == -1 && PyErr_Occurred ())
695
709
return -1 ;
710
+ if (!get_buf (self , & ptr1 , & selfsize , ANY_BUFFER ))
711
+ return -1 ;
712
+
696
713
if (i < 0 )
697
714
i += selfsize ;
698
- return buffer_ass_item ( self , i , value );
715
+ return buffer_ass_item_impl ( ptr1 , selfsize , i , value );
699
716
}
700
717
else if (PySlice_Check (item )) {
701
718
Py_ssize_t start , stop , step , slicelength ;
702
719
703
- if (PySlice_GetIndicesEx ((PySliceObject * )item , selfsize ,
704
- & start , & stop , & step , & slicelength ) < 0 )
720
+ if (_PySlice_Unpack (item , & start , & stop , & step ) < 0 )
721
+ return -1 ;
722
+ if (!get_buf (self , & ptr1 , & selfsize , ANY_BUFFER ))
705
723
return -1 ;
706
724
725
+ slicelength = _PySlice_AdjustIndices (selfsize , & start , & stop , step );
707
726
if ((othersize = (* pb -> bf_getreadbuffer )(value , 0 , & ptr2 )) < 0 )
708
727
return -1 ;
709
728
0 commit comments