-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #408 from v923z/attr-prop
implement ndarray properties
- Loading branch information
Showing
32 changed files
with
197 additions
and
481 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,83 @@ | ||
|
||
/* | ||
* This file is part of the micropython-ulab project, | ||
* | ||
* https://github.com/v923z/micropython-ulab | ||
* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2021 Zoltán Vörös | ||
* | ||
*/ | ||
|
||
#include <math.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include "py/obj.h" | ||
#include "py/runtime.h" | ||
|
||
#include "ulab.h" | ||
#include "ndarray.h" | ||
|
||
#ifndef CIRCUITPY | ||
|
||
// a somewhat hackish implementation of property getters/setters; | ||
// this functions is hooked into the attr member of ndarray | ||
|
||
STATIC void call_local_method(mp_obj_t obj, qstr attr, mp_obj_t *dest) { | ||
const mp_obj_type_t *type = mp_obj_get_type(obj); | ||
while (type->locals_dict != NULL) { | ||
assert(type->locals_dict->base.type == &mp_type_dict); // MicroPython restriction, for now | ||
mp_map_t *locals_map = &type->locals_dict->map; | ||
mp_map_elem_t *elem = mp_map_lookup(locals_map, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP); | ||
if (elem != NULL) { | ||
mp_convert_member_lookup(obj, type, elem->value, dest); | ||
break; | ||
} | ||
if (type->parent == NULL) { | ||
break; | ||
} | ||
type = type->parent; | ||
} | ||
} | ||
|
||
|
||
void ndarray_properties_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) { | ||
if (dest[0] == MP_OBJ_NULL) { | ||
switch(attr) { | ||
#if NDARRAY_HAS_DTYPE | ||
case MP_QSTR_dtype: | ||
dest[0] = ndarray_dtype(self_in); | ||
break; | ||
#endif | ||
#if NDARRAY_HAS_ITEMSIZE | ||
case MP_QSTR_itemsize: | ||
dest[0] = ndarray_itemsize(self_in); | ||
break; | ||
#endif | ||
#if NDARRAY_HAS_SHAPE | ||
case MP_QSTR_shape: | ||
dest[0] = ndarray_shape(self_in); | ||
break; | ||
#endif | ||
#if NDARRAY_HAS_SIZE | ||
case MP_QSTR_size: | ||
dest[0] = ndarray_size(self_in); | ||
break; | ||
#endif | ||
#if NDARRAY_HAS_STRIDES | ||
case MP_QSTR_strides: | ||
dest[0] = ndarray_strides(self_in); | ||
break; | ||
#endif | ||
default: | ||
call_local_method(self_in, attr, dest); | ||
break; | ||
} | ||
} else { | ||
return; | ||
} | ||
} | ||
|
||
#endif /* CIRCUITPY */ |
This file contains 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 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 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 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 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.