Skip to content

Commit ad1a1c5

Browse files
Fix create_arange empty range (#604)
* Fix create_arange crashing when stop == start This is due to len being equal to zero, which made (len - 1) equal to MAX_UINT16 * trailing whitespaces
1 parent afc8e4e commit ad1a1c5

File tree

4 files changed

+12
-5
lines changed

4 files changed

+12
-5
lines changed

code/numpy/create.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ mp_obj_t create_arange(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_arg
159159
}
160160

161161
ndarray_obj_t *ndarray;
162-
if((stop - start)/step < 0) {
162+
if((stop - start)/step <= 0) {
163163
ndarray = ndarray_new_linear_array(0, dtype);
164164
} else {
165165
size_t len = (size_t)(MICROPY_FLOAT_C_FUN(ceil)((stop - start) / step));
@@ -254,14 +254,14 @@ mp_obj_t create_concatenate(size_t n_args, const mp_obj_t *pos_args, mp_map_t *k
254254
size_t *shape = m_new0(size_t, ULAB_MAX_DIMS);
255255
mp_obj_tuple_t *ndarrays = MP_OBJ_TO_PTR(args[0].u_obj);
256256

257-
// first check, whether
257+
// first check, whether
258258

259259
for(uint8_t i = 0; i < ndarrays->len; i++) {
260260
if(!mp_obj_is_type(ndarrays->items[i], &ulab_ndarray_type)) {
261261
mp_raise_ValueError(translate("only ndarrays can be concatenated"));
262262
}
263263
}
264-
264+
265265
// first check, whether the arrays are compatible
266266
ndarray_obj_t *_ndarray = MP_OBJ_TO_PTR(ndarrays->items[0]);
267267
uint8_t dtype = _ndarray->dtype;

code/ulab.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
#include "user/user.h"
3434
#include "utils/utils.h"
3535

36-
#define ULAB_VERSION 6.0.7
36+
#define ULAB_VERSION 6.0.8
3737
#define xstr(s) str(s)
3838
#define str(s) #s
3939

tests/2d/numpy/arange.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
print(np.arange(10, dtype=dtype))
1010
print(np.arange(2, 10, dtype=dtype))
1111
print(np.arange(2, 10, 3, dtype=dtype))
12+
# test empty range
13+
print(np.arange(0, 0, dtype=dtype))
1214

1315
# test for ZeroDivisionError exception
1416
try:
1517
np.arange(0, 10, 0)
1618
except ZeroDivisionError as e:
17-
print('ZeroDivisionError: ', e)
19+
print('ZeroDivisionError: ', e)

tests/2d/numpy/arange.py.exp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=uint8)
22
array([2, 3, 4, 5, 6, 7, 8, 9], dtype=uint8)
33
array([2, 5, 8], dtype=uint8)
4+
array([], dtype=uint8)
45
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=int8)
56
array([2, 3, 4, 5, 6, 7, 8, 9], dtype=int8)
67
array([2, 5, 8], dtype=int8)
8+
array([], dtype=int8)
79
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=uint16)
810
array([2, 3, 4, 5, 6, 7, 8, 9], dtype=uint16)
911
array([2, 5, 8], dtype=uint16)
12+
array([], dtype=uint16)
1013
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=int16)
1114
array([2, 3, 4, 5, 6, 7, 8, 9], dtype=int16)
1215
array([2, 5, 8], dtype=int16)
16+
array([], dtype=int16)
1317
array([0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0], dtype=float64)
1418
array([2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0], dtype=float64)
1519
array([2.0, 5.0, 8.0], dtype=float64)
20+
array([], dtype=float64)
1621
ZeroDivisionError: divide by zero

0 commit comments

Comments
 (0)