Skip to content

Commit

Permalink
Fix arange crashing when start, stop or step is nan (#605)
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixNumworks committed Apr 27, 2023
1 parent ad1a1c5 commit 8c3e105
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
6 changes: 4 additions & 2 deletions code/numpy/create.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ mp_obj_t create_arange(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_arg
mp_raise_msg(&mp_type_ZeroDivisionError, MP_ERROR_TEXT("divide by zero"));
}

if(isnan(start) || isnan(stop) || isnan(step)) {
mp_raise_ValueError(translate("arange: cannot compute length"));
}

ndarray_obj_t *ndarray;
if((stop - start)/step <= 0) {
ndarray = ndarray_new_linear_array(0, dtype);
Expand Down Expand Up @@ -254,8 +258,6 @@ mp_obj_t create_concatenate(size_t n_args, const mp_obj_t *pos_args, mp_map_t *k
size_t *shape = m_new0(size_t, ULAB_MAX_DIMS);
mp_obj_tuple_t *ndarrays = MP_OBJ_TO_PTR(args[0].u_obj);

// first check, whether

for(uint8_t i = 0; i < ndarrays->len; i++) {
if(!mp_obj_is_type(ndarrays->items[i], &ulab_ndarray_type)) {
mp_raise_ValueError(translate("only ndarrays can be concatenated"));
Expand Down
2 changes: 1 addition & 1 deletion code/ulab.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#include "user/user.h"
#include "utils/utils.h"

#define ULAB_VERSION 6.0.8
#define ULAB_VERSION 6.0.9
#define xstr(s) str(s)
#define str(s) #s

Expand Down
6 changes: 6 additions & 0 deletions tests/2d/numpy/arange.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@
np.arange(0, 10, 0)
except ZeroDivisionError as e:
print('ZeroDivisionError: ', e)

# test for NAN length exception
try:
np.arange(0, np.nan)
except ValueError as e:
print('ValueError: ', e)
1 change: 1 addition & 0 deletions tests/2d/numpy/arange.py.exp
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ array([2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0], dtype=float64)
array([2.0, 5.0, 8.0], dtype=float64)
array([], dtype=float64)
ZeroDivisionError: divide by zero
ValueError: arange: cannot compute length

0 comments on commit 8c3e105

Please sign in to comment.