Skip to content

Commit 8c3e105

Browse files
Fix arange crashing when start, stop or step is nan (#605)
1 parent ad1a1c5 commit 8c3e105

File tree

4 files changed

+12
-3
lines changed

4 files changed

+12
-3
lines changed

code/numpy/create.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ mp_obj_t create_arange(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_arg
158158
mp_raise_msg(&mp_type_ZeroDivisionError, MP_ERROR_TEXT("divide by zero"));
159159
}
160160

161+
if(isnan(start) || isnan(stop) || isnan(step)) {
162+
mp_raise_ValueError(translate("arange: cannot compute length"));
163+
}
164+
161165
ndarray_obj_t *ndarray;
162166
if((stop - start)/step <= 0) {
163167
ndarray = ndarray_new_linear_array(0, dtype);
@@ -254,8 +258,6 @@ mp_obj_t create_concatenate(size_t n_args, const mp_obj_t *pos_args, mp_map_t *k
254258
size_t *shape = m_new0(size_t, ULAB_MAX_DIMS);
255259
mp_obj_tuple_t *ndarrays = MP_OBJ_TO_PTR(args[0].u_obj);
256260

257-
// first check, whether
258-
259261
for(uint8_t i = 0; i < ndarrays->len; i++) {
260262
if(!mp_obj_is_type(ndarrays->items[i], &ulab_ndarray_type)) {
261263
mp_raise_ValueError(translate("only ndarrays can be concatenated"));

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.8
36+
#define ULAB_VERSION 6.0.9
3737
#define xstr(s) str(s)
3838
#define str(s) #s
3939

tests/2d/numpy/arange.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,9 @@
1717
np.arange(0, 10, 0)
1818
except ZeroDivisionError as e:
1919
print('ZeroDivisionError: ', e)
20+
21+
# test for NAN length exception
22+
try:
23+
np.arange(0, np.nan)
24+
except ValueError as e:
25+
print('ValueError: ', e)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ array([2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0], dtype=float64)
1919
array([2.0, 5.0, 8.0], dtype=float64)
2020
array([], dtype=float64)
2121
ZeroDivisionError: divide by zero
22+
ValueError: arange: cannot compute length

0 commit comments

Comments
 (0)