@@ -4715,6 +4715,8 @@ typedef struct BIIterContiguousObject {
4715
4715
bool reduce ; // optionally reduce slices to integers
4716
4716
} BIIterContiguousObject ;
4717
4717
4718
+
4719
+ // Create a new contiguous slice iterator. Return NULL on error. Steals a reference to PyObject* iter.
4718
4720
static PyObject *
4719
4721
BIIterContiguous_new (BlockIndexObject * bi ,
4720
4722
bool reversed ,
@@ -4728,9 +4730,7 @@ BIIterContiguous_new(BlockIndexObject *bi,
4728
4730
Py_INCREF ((PyObject * )bi );
4729
4731
bii -> bi = bi ;
4730
4732
4731
- Py_INCREF (iter );
4732
- bii -> iter = iter ;
4733
-
4733
+ bii -> iter = iter ; // steals ref
4734
4734
bii -> reversed = reversed ;
4735
4735
4736
4736
bii -> last_block = -1 ;
@@ -4790,9 +4790,8 @@ BIIterContiguous_reversed(BIIterContiguousObject *self)
4790
4790
}
4791
4791
PyObject * biiter = BIIterContiguous_new (self -> bi ,
4792
4792
reversed ,
4793
- self -> iter ,
4793
+ iter , // steals ref
4794
4794
self -> reduce );
4795
- Py_DECREF (iter );
4796
4795
return biiter ;
4797
4796
}
4798
4797
@@ -4832,6 +4831,9 @@ BIIterContiguous_iternext(BIIterContiguousObject *self)
4832
4831
}
4833
4832
// no more pairs, return previous slice_start, flag for end on next call
4834
4833
self -> next_block = -2 ;
4834
+ if (self -> last_block == -1 ) { // iter produced no values, terminate
4835
+ break ;
4836
+ }
4835
4837
return AK_build_pair_ssize_t_slice ( // steals ref
4836
4838
self -> last_block ,
4837
4839
AK_build_slice_inclusive (slice_start ,
@@ -5488,14 +5490,11 @@ BlockIndex_iter_contiguous(BlockIndexObject *self, PyObject *args, PyObject *kwa
5488
5490
)) {
5489
5491
return NULL ;
5490
5492
}
5491
-
5492
- // might need to store enum type for branching
5493
5493
PyObject * iter = BIIterSelector_new (self , selector , 0 , BIIS_UNKNOWN , ascending );
5494
5494
if (iter == NULL ) {
5495
5495
return NULL ; // exception set
5496
5496
}
5497
- PyObject * biiter = BIIterContiguous_new (self , 0 , iter , reduce ); // might be NULL, will incref iter
5498
- Py_DECREF (iter );
5497
+ PyObject * biiter = BIIterContiguous_new (self , 0 , iter , reduce ); // might be NULL, steals iter ref
5499
5498
return biiter ;
5500
5499
}
5501
5500
0 commit comments