Skip to content

Commit a076a43

Browse files
authored
Merge pull request #141 from static-frame/132/bi-register
Set exceptions on memory errors; small optimization to `register`
2 parents b995588 + 73be5fd commit a076a43

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/_arraykit.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ AK_ImmutableFilter(PyArrayObject *a)
101101
return a;
102102
}
103103

104+
// Returns NULL on error.
104105
PyArray_Descr*
105106
AK_ResolveDTypes(PyArray_Descr *d1, PyArray_Descr *d2)
106107
{
@@ -5159,6 +5160,7 @@ AK_BI_BIR_new(BlockIndexObject* bi) {
51595160
BlockIndexRecord* bir = (BlockIndexRecord*)PyMem_Malloc(
51605161
sizeof(BlockIndexRecord) * bi->bir_capacity);
51615162
if (bir == NULL) {
5163+
PyErr_SetNone(PyExc_MemoryError);
51625164
return -1;
51635165
}
51645166
bi->bir = bir;
@@ -5176,6 +5178,7 @@ AK_BI_BIR_resize(BlockIndexObject* bi, Py_ssize_t increment) {
51765178
bi->bir = PyMem_Realloc(bi->bir,
51775179
sizeof(BlockIndexRecord) * capacity);
51785180
if (bi->bir == NULL) {
5181+
PyErr_SetNone(PyExc_MemoryError);
51795182
return -1;
51805183
}
51815184
bi->bir_capacity = capacity;
@@ -5318,6 +5321,9 @@ BlockIndex_register(BlockIndexObject *self, PyObject *value) {
53185321
}
53195322
else if (!PyDataType_ISOBJECT(self->dtype)) { // if object cannot resolve further
53205323
PyArray_Descr* dtr = AK_ResolveDTypes(self->dtype, dt); // new ref
5324+
if (dtr == NULL) {
5325+
return NULL;
5326+
}
53215327
Py_DECREF((PyObject*)self->dtype);
53225328
self->dtype = dtr;
53235329
}
@@ -5329,10 +5335,12 @@ BlockIndex_register(BlockIndexObject *self, PyObject *value) {
53295335
BlockIndexRecord* bir = self->bir;
53305336
Py_ssize_t bc = self->block_count;
53315337

5338+
Py_ssize_t birc = self->bir_count;
53325339
for (Py_ssize_t i = 0; i < increment; i++) {
5333-
bir[self->bir_count] = (BlockIndexRecord){bc, i};
5334-
self->bir_count++;
5340+
bir[birc] = (BlockIndexRecord){bc, i};
5341+
birc++;
53355342
}
5343+
self->bir_count = birc;
53365344
self->block_count++;
53375345
Py_RETURN_TRUE;
53385346
}

test/test_block_index.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,14 @@ def test_block_index_register_h(self) -> None:
145145
self.assertEqual(bi1.dtype, np.dtype(object))
146146

147147

148+
def test_block_index_register_i(self) -> None:
149+
bi1 = BlockIndex()
150+
# NOTE: this value in one context returned an unset exception; I think I have now covered those cases but cannot reproduce the failure; testing the full size is too slow, so reducing here as a placeholder
151+
size = 2_147_483_649 // 100
152+
post = bi1.register(np.array(()).reshape(0, size))
153+
self.assertEqual(bi1.shape, (0, size))
154+
155+
148156
#---------------------------------------------------------------------------
149157

150158
def test_block_index_to_bytes_a(self) -> None:

0 commit comments

Comments
 (0)