-
Notifications
You must be signed in to change notification settings - Fork 123
[WIP] Fix ndarray constructor #305
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
CallumJHays
wants to merge
3
commits into
v923z:master
Choose a base branch
from
CallumJHays:fix-ndarray-constructor
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -777,7 +777,7 @@ STATIC uint8_t ndarray_init_helper(size_t n_args, const mp_obj_t *pos_args, mp_m | |
return _dtype; | ||
} | ||
|
||
STATIC mp_obj_t ndarray_make_new_core(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args, mp_map_t *kw_args) { | ||
mp_obj_t ndarray_array_constructor(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) { | ||
uint8_t dtype = ndarray_init_helper(n_args, args, kw_args); | ||
|
||
if(MP_OBJ_IS_TYPE(args[0], &ulab_ndarray_type)) { | ||
|
@@ -912,12 +912,38 @@ STATIC mp_obj_t ndarray_make_new_core(const mp_obj_type_t *type, size_t n_args, | |
return MP_OBJ_FROM_PTR(self); | ||
} | ||
|
||
mp_obj_t ndarray_array_constructor(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { | ||
// array constructor for ndarray, equivalent to numpy.array(...) | ||
return ndarray_make_new_core(&ulab_ndarray_type, n_args, kw_args->used, pos_args, kw_args); | ||
} | ||
MP_DEFINE_CONST_FUN_OBJ_KW(ndarray_array_constructor_obj, 1, ndarray_array_constructor); | ||
|
||
STATIC mp_obj_t ndarray_make_new_core(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args, mp_map_t *kw_args) { | ||
(void) type; | ||
mp_arg_check_num(n_args, n_kw, 1, 2, true); | ||
uint8_t dtype = ndarray_init_helper(n_args, args, kw_args); | ||
|
||
mp_obj_t mp_shape = args[0]; | ||
mp_obj_t mp_ndim_maybe = mp_obj_len_maybe(mp_shape); | ||
// single-number shapes "x" are interpreted the same as "(x,)" | ||
mp_int_t ndim; | ||
size_t shape[ULAB_MAX_DIMS]; | ||
|
||
if (mp_ndim_maybe == MP_OBJ_NULL) { | ||
ndim = 1; | ||
shape[ULAB_MAX_DIMS - 1] = MP_OBJ_SMALL_INT_VALUE(mp_shape); | ||
} else { | ||
mp_obj_get_int_maybe(mp_ndim_maybe, &ndim); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here you have to bail out, if the tuple is longer then ULAB_MAX_DIMS. The loop below is safe, but the user would still be left wondering, why the array is not what they expected. |
||
|
||
mp_obj_iter_buf_t iter_buf; | ||
mp_obj_t mp_shape_iter = mp_getiter(mp_shape, &iter_buf); | ||
|
||
for (uint8_t i = ULAB_MAX_DIMS - ndim; i < ULAB_MAX_DIMS; i++) { | ||
shape[i] = MP_OBJ_SMALL_INT_VALUE(mp_iternext(mp_shape_iter)); | ||
} | ||
} | ||
|
||
return MP_OBJ_FROM_PTR( | ||
ndarray_new_dense_ndarray(ndim, shape, dtype) | ||
); | ||
} | ||
|
||
#ifdef CIRCUITPY | ||
mp_obj_t ndarray_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) { | ||
(void) type; | ||
|
@@ -926,7 +952,6 @@ mp_obj_t ndarray_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj | |
if (kw_args != 0) { | ||
n_kw = kw_args->used; | ||
} | ||
mp_map_init_fixed_table(kw_args, n_kw, args + n_args); | ||
return ndarray_make_new_core(type, n_args, n_kw, args, kw_args); | ||
} | ||
#else | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will fail with
ndarray(-12.3)
. You can make it safe by explicitly checking for the input type:micropython-ulab/code/ulab_create.c
Line 29 in ab964b9