Skip to content

Commit 29722a5

Browse files
committed
isolated segfault, updated casting
1 parent bfccbaf commit 29722a5

File tree

2 files changed

+48
-13
lines changed

2 files changed

+48
-13
lines changed

src/_arraykit.c

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4254,7 +4254,7 @@ BIIter_new(BlockIndexObject *bi, int8_t reversed) {
42544254
if (!bii) {
42554255
return NULL;
42564256
}
4257-
Py_INCREF(bi);
4257+
Py_INCREF((PyObject*)bi);
42584258
bii->bi = bi;
42594259
bii->reversed = reversed;
42604260
bii->pos = 0;
@@ -4263,7 +4263,7 @@ BIIter_new(BlockIndexObject *bi, int8_t reversed) {
42634263

42644264
static void
42654265
BIIter_dealloc(BIIterObject *self) {
4266-
Py_DECREF(self->bi);
4266+
Py_DECREF((PyObject*)self->bi);
42674267
Py_TYPE(self)->tp_free((PyObject *)self);
42684268
}
42694269

@@ -4355,7 +4355,7 @@ typedef struct BIIterSeqObject {
43554355

43564356
static void
43574357
BIIterSeq_dealloc(BIIterSeqObject *self) {
4358-
Py_DECREF(self->bi);
4358+
Py_DECREF((PyObject*)self->bi);
43594359
Py_DECREF(self->selector);
43604360
Py_TYPE(self)->tp_free((PyObject *)self);
43614361
}
@@ -4491,7 +4491,7 @@ typedef struct BIIterSliceObject {
44914491

44924492
static void
44934493
BIIterSlice_dealloc(BIIterSliceObject *self) {
4494-
Py_DECREF(self->bi);
4494+
Py_DECREF((PyObject*)self->bi);
44954495
Py_DECREF(self->selector);
44964496
Py_TYPE(self)->tp_free((PyObject *)self);
44974497
}
@@ -4567,7 +4567,7 @@ typedef struct BIIterBooleanObject {
45674567

45684568
static void
45694569
BIIterBoolean_dealloc(BIIterBooleanObject *self) {
4570-
Py_DECREF(self->bi);
4570+
Py_DECREF((PyObject*)self->bi);
45714571
Py_DECREF(self->selector);
45724572
Py_TYPE(self)->tp_free((PyObject *)self);
45734573
}
@@ -4669,10 +4669,11 @@ BIIterContiguous_new(BlockIndexObject *bi,
46694669
if (!bii) {
46704670
return NULL;
46714671
}
4672-
Py_INCREF(bi);
4672+
Py_INCREF((PyObject*)bi);
46734673
bii->bi = bi;
46744674
Py_INCREF(iter);
46754675
bii->iter = iter;
4676+
46764677
bii->reversed = reversed;
46774678

46784679
bii->last_block = -1;
@@ -4681,14 +4682,17 @@ BIIterContiguous_new(BlockIndexObject *bi,
46814682
bii->next_column = -1;
46824683
bii->reduce = reduce;
46834684

4685+
AK_DEBUG_MSG_OBJ("complete init", Py_None);
46844686
return (PyObject *)bii;
46854687
}
46864688

46874689
static void
46884690
BIIterContiguous_dealloc(BIIterContiguousObject *self) {
4689-
Py_DECREF(self->bi);
4691+
AK_DEBUG_MSG_OBJ("start dealloc", Py_None);
4692+
Py_DECREF((PyObject*)self->bi);
46904693
Py_DECREF(self->iter);
46914694
Py_TYPE(self)->tp_free((PyObject *)self);
4695+
AK_DEBUG_MSG_OBJ("end dealloc", Py_None);
46924696
}
46934697

46944698
static BIIterContiguousObject *
@@ -4829,7 +4833,7 @@ BIIterSelector_new(BlockIndexObject *bi,
48294833
int8_t ascending) {
48304834

48314835
int8_t is_array = 0;
4832-
int8_t incref_selector = 1; // incref borrowed selector; but if a new ref is made, do not
4836+
bool incref_selector = true; // incref borrowed selector; but if a new ref is made, do not
48334837

48344838
Py_ssize_t len = -1;
48354839
Py_ssize_t pos = 0;
@@ -4881,7 +4885,7 @@ BIIterSelector_new(BlockIndexObject *bi,
48814885
if (PyArray_Sort((PyArrayObject*)selector, 0, NPY_QUICKSORT)) {
48824886
return NULL; // returns -1 on error
48834887
}; // new ref
4884-
incref_selector = 0;
4888+
incref_selector = false;
48854889
}
48864890
}
48874891
else if (PySlice_Check(selector)) {
@@ -4896,7 +4900,7 @@ BIIterSelector_new(BlockIndexObject *bi,
48964900
if (ascending) {
48974901
// NOTE: we are abandoning the borrowed reference
48984902
selector = AK_slice_to_ascending_slice(selector, bi->bir_count); // new ref
4899-
incref_selector = 0;
4903+
incref_selector = false;
49004904
}
49014905

49024906
if (PySlice_Unpack(selector, &pos, &stop, &step)) {
@@ -4930,7 +4934,7 @@ BIIterSelector_new(BlockIndexObject *bi,
49304934
return NULL;
49314935
}
49324936
Py_DECREF(post); // just a None
4933-
incref_selector = 0;
4937+
incref_selector = false;
49344938
}
49354939
}
49364940
else {
@@ -4979,7 +4983,7 @@ BIIterSelector_new(BlockIndexObject *bi,
49794983
case BIIS_UNKNOWN:
49804984
goto error; // should not get here!
49814985
}
4982-
Py_INCREF(bi);
4986+
Py_INCREF((PyObject*)bi);
49834987
if (incref_selector) {
49844988
Py_INCREF(selector);
49854989
}
@@ -5428,7 +5432,10 @@ BlockIndex_iter_contiguous(BlockIndexObject *self, PyObject *args, PyObject *kwa
54285432

54295433
// might need to store enum type for branching
54305434
PyObject* iter = BIIterSelector_new(self, selector, 0, BIIS_UNKNOWN, ascending);
5431-
PyObject* biiter = BIIterContiguous_new(self, 0, iter, reduce); // will incref iter
5435+
if (iter == NULL) {
5436+
return NULL; // exception set
5437+
}
5438+
PyObject* biiter = BIIterContiguous_new(self, 0, iter, reduce); // might be NULL, will incref iter
54325439
Py_DECREF(iter);
54335440

54345441
return biiter;

test/test_block_index.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,3 +705,31 @@ def test_block_index_iter_contiguous_e2(self) -> None:
705705
list(bi1.iter_contiguous(np.array([6, 0, 7]), ascending=True, reduce=True)),
706706
[(0, 0), (6, 0), (7, 0)]
707707
)
708+
709+
710+
711+
def test_block_index_iter_contiguous_f(self) -> None:
712+
bi1 = BlockIndex()
713+
bi1.register(np.arange(6).reshape(2,3))
714+
bi1.register(np.arange(6).reshape(2,3))
715+
bi1.register(np.arange(4).reshape(2,2))
716+
717+
key = np.array([2, 3, 5])
718+
719+
def gen():
720+
yield from bi1.iter_contiguous(key)
721+
post2 = list(gen())
722+
723+
post1 = list(bi1.iter_contiguous(key))
724+
self.assertEqual(post1, post2)
725+
726+
727+
728+
def test_block_index_iter_contiguous_g(self) -> None:
729+
bi1 = BlockIndex()
730+
bi1.register(np.arange(6).reshape(2,3))
731+
bi1.register(np.arange(6).reshape(2,3))
732+
bi1.register(np.arange(4).reshape(2,2))
733+
734+
with self.assertRaises(TypeError):
735+
_ = list(bi1.iter_contiguous('a'))

0 commit comments

Comments
 (0)