Skip to content

Commit

Permalink
fix error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
v923z committed Jan 9, 2024
1 parent 79ab792 commit 9ac0cad
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 70 deletions.
16 changes: 8 additions & 8 deletions code/numpy/random/random.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ mp_obj_t random_generator_make_new(const mp_obj_type_t *type, size_t n_args, siz

if(args[0] == mp_const_none) {
#ifndef MICROPY_PY_RANDOM_SEED_INIT_FUNC
mp_raise_ValueError(translate("no default seed"));
mp_raise_ValueError(MP_ERROR_TEXT("no default seed"));
#endif
random_generator_obj_t *generator = m_new_obj(random_generator_obj_t);
generator->base.type = &random_generator_type;
Expand All @@ -90,7 +90,7 @@ mp_obj_t random_generator_make_new(const mp_obj_type_t *type, size_t n_args, siz
}
return mp_obj_new_tuple(seeds->len, items);
} else {
mp_raise_TypeError(translate("argument must be None, an integer or a tuple of integers"));
mp_raise_TypeError(MP_ERROR_TEXT("argument must be None, an integer or a tuple of integers"));
}
// we should never end up here
return mp_const_none;
Expand Down Expand Up @@ -142,7 +142,7 @@ static mp_obj_t random_random(size_t n_args, const mp_obj_t *pos_args, mp_map_t
} else if(mp_obj_is_type(size, &mp_type_tuple)) {
mp_obj_tuple_t *_shape = MP_OBJ_TO_PTR(size);
if(_shape->len > ULAB_MAX_DIMS) {
mp_raise_ValueError(translate("maximum number of dimensions is " MP_STRINGIFY(ULAB_MAX_DIMS)));
mp_raise_ValueError(MP_ERROR_TEXT("maximum number of dimensions is " MP_STRINGIFY(ULAB_MAX_DIMS)));
}
ndim = _shape->len;
for(size_t i = 0; i < ULAB_MAX_DIMS; i++) {
Expand All @@ -153,29 +153,29 @@ static mp_obj_t random_random(size_t n_args, const mp_obj_t *pos_args, mp_map_t
}
}
} else { // input type not supported
mp_raise_TypeError(translate("shape must be None, and integer or a tuple of integers"));
mp_raise_TypeError(MP_ERROR_TEXT("shape must be None, and integer or a tuple of integers"));
}
}

if(out != mp_const_none) {
if(!mp_obj_is_type(out, &ulab_ndarray_type)) {
mp_raise_TypeError(translate("out has wrong type"));
mp_raise_TypeError(MP_ERROR_TEXT("out has wrong type"));
}

ndarray = MP_OBJ_TO_PTR(out);

if(ndarray->dtype != NDARRAY_FLOAT) {
mp_raise_TypeError(translate("output array has wrong type"));
mp_raise_TypeError(MP_ERROR_TEXT("output array has wrong type"));
}
if(size != mp_const_none) {
for(uint8_t i = 0; i < ULAB_MAX_DIMS; i++) {
if(ndarray->shape[i] != shape[i]) {
mp_raise_ValueError(translate("size must match out.shape when used together"));
mp_raise_ValueError(MP_ERROR_TEXT("size must match out.shape when used together"));
}
}
}
if(!ndarray_is_dense(ndarray)) {
mp_raise_ValueError(translate("output array must be contiguous"));
mp_raise_ValueError(MP_ERROR_TEXT("output array must be contiguous"));
}
} else { // out == None
if(size != mp_const_none) {
Expand Down
1 change: 0 additions & 1 deletion code/numpy/random/random.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
* Copyright (c) 2024 Zoltán Vörös
*/

#include "../../ulab.h"
#include "../../ndarray.h"

#ifndef _NUMPY_RANDOM_
Expand Down
61 changes: 0 additions & 61 deletions code/ulab_tools.c
Original file line number Diff line number Diff line change
Expand Up @@ -274,64 +274,3 @@ bool ulab_tools_mp_obj_is_scalar(mp_obj_t obj) {
}
#endif
}

#if 0
#if ULAB_NUMPY_HAS_RANDOM_MODULE
ndarray_obj_t *ulab_tools_create_out(mp_obj_t shape, mp_obj_t out, uint8_t dtype, bool check_dense) {
// raise various exceptions, if the supplied output is not compatible with
// the requested shape or the output of a particular function

// if no out object is supplied, there is nothing to inspect
if(out == mp_const_none) {
return;
}

if(mp_obj_is_int(shape)) {
size_t len = (size_t)mp_obj_get_int(shape);
return ndarray_new_linear_array(len, dtype);
} else if(mp_obj_is_type(size, &mp_type_tuple)) {
mp_obj_tuple_t *shape = MP_OBJ_TO_PTR(shape);
if(shape->len > ULAB_MAX_DIMS) {
mp_raise_ValueError(translate("maximum number of dimensions is " MP_STRINGIFY(ULAB_MAX_DIMS)));
}
if(out != mp_const_none) {
if(!mp_obj_is_type(out, &ulab_ndarray_type)) {
mp_raise_TypeError(translate("out must be an ndarray"));
}
ndarray_obj_t *ndarray = MP_OBJ_TO_PTR(out);
if(ndarray->dtype != dtype) {
mp_raise_TypeError(translate("incompatible output dtype"));
}
// some functions require a dense output array
if(check_dense && !ndarray_is_dense(ndarray)) {
mp_raise_ValueError(translate("supplied output array must be contiguous"));
}


// check if the requested shape and the output shape are compatible
bool shape_is_compatible = ndarray->ndim == shape->len;
for(uint8_t i = 0; i < ndarray->ndim; i++) {
if(ndarray->shape[ULAB_MAX_DIMS - ndarray->ndim + i] != mp_obj_get_int(shape->items[i])) {
shape_is_compatible = false;
break;
}
}
if(!shape_is_compatible) {
mp_raise_ValueError(translate("size must match out shape when used together"));
}

}


return ndarray_new_ndarray_from_tuple(shape, dtype);
} else { // input type not supported
mp_raise_TypeError(translate("shape must be None, and integer or a tuple of integers"));
}


if(mp_obj_is_type(shape, &mp_tuple_type)) {

}
}
#endif /* ULAB_NUMPY_HAS_RANDOM_MODULE */
#endif

0 comments on commit 9ac0cad

Please sign in to comment.