Compare commits

...

1 commit

Author SHA1 Message Date
Zoltán Vörös
14d3f657f1 raise exception in arange, if step size is 0 2023-01-22 19:02:00 +01:00
5 changed files with 20 additions and 2 deletions

View file

@ -152,6 +152,12 @@ mp_obj_t create_arange(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_arg
if(args[3].u_obj != mp_const_none) { if(args[3].u_obj != mp_const_none) {
dtype = (uint8_t)mp_obj_get_int(args[3].u_obj); dtype = (uint8_t)mp_obj_get_int(args[3].u_obj);
} }
// bail out, if the range cannot be constructed
if(step == MICROPY_FLOAT_CONST(0.0)) {
mp_raise_msg(&mp_type_ZeroDivisionError, MP_ERROR_TEXT("divide by zero"));
}
ndarray_obj_t *ndarray; ndarray_obj_t *ndarray;
if((stop - start)/step < 0) { if((stop - start)/step < 0) {
ndarray = ndarray_new_linear_array(0, dtype); ndarray = ndarray_new_linear_array(0, dtype);

View file

@ -33,7 +33,7 @@
#include "user/user.h" #include "user/user.h"
#include "utils/utils.h" #include "utils/utils.h"
#define ULAB_VERSION 6.0.5 #define ULAB_VERSION 6.0.6
#define xstr(s) str(s) #define xstr(s) str(s)
#define str(s) #s #define str(s) #s

View file

@ -1,3 +1,8 @@
Sun, 21 Jan 2023
version 6.0.6
raise proper exception in arange
Sun, 21 Jan 2023 Sun, 21 Jan 2023

View file

@ -9,3 +9,9 @@ for dtype in dtypes:
print(np.arange(10, dtype=dtype)) print(np.arange(10, dtype=dtype))
print(np.arange(2, 10, dtype=dtype)) print(np.arange(2, 10, dtype=dtype))
print(np.arange(2, 10, 3, dtype=dtype)) print(np.arange(2, 10, 3, dtype=dtype))
# test for ZeroDivisionError exception
try:
np.arange(0, 10, 0)
except ZeroDivisionError as e:
print('ZeroDivisionError: ', e)

View file

@ -13,3 +13,4 @@ array([2, 5, 8], dtype=int16)
array([0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0], dtype=float64) array([0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0], dtype=float64)
array([2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0], dtype=float64) 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([2.0, 5.0, 8.0], dtype=float64)
ZeroDivisionError: divide by zero