Skip to content

Commit 6fb6f23

Browse files
committed
re-introduce ndarray_get_buffer, and buffer protocol
1 parent ce763eb commit 6fb6f23

File tree

4 files changed

+21
-1
lines changed

4 files changed

+21
-1
lines changed

code/ndarray.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2026,3 +2026,15 @@ mp_obj_t ndarray_info(mp_obj_t obj_in) {
20262026

20272027
MP_DEFINE_CONST_FUN_OBJ_1(ndarray_info_obj, ndarray_info);
20282028
#endif
2029+
2030+
// (the get_buffer protocol returns 0 for success, 1 for failure)
2031+
mp_int_t ndarray_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
2032+
ndarray_obj_t *self = MP_OBJ_TO_PTR(self_in);
2033+
if(!ndarray_is_dense(self)) {
2034+
return 1;
2035+
}
2036+
bufinfo->len = self->itemsize * self->len;
2037+
bufinfo->buf = self->array;
2038+
bufinfo->typecode = self->dtype;
2039+
return 0;
2040+
}

code/ndarray.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ mp_obj_t ndarray_info(mp_obj_t );
181181
MP_DECLARE_CONST_FUN_OBJ_1(ndarray_info_obj);
182182
#endif
183183

184+
mp_int_t ndarray_get_buffer(mp_obj_t , mp_buffer_info_t *, mp_uint_t );
184185
//void ndarray_attributes(mp_obj_t , qstr , mp_obj_t *);
185186

186187
ndarray_obj_t *ndarray_from_mp_obj(mp_obj_t );

code/ulab.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
#include "user/user.h"
3535

36-
#define ULAB_VERSION 2.4.2
36+
#define ULAB_VERSION 2.4.3
3737
#define xstr(s) str(s)
3838
#define str(s) #s
3939
#define ULAB_VERSION_STRING xstr(ULAB_VERSION) xstr(-) xstr(ULAB_MAX_DIMS) xstr(D)
@@ -105,6 +105,7 @@ const mp_obj_type_t ulab_ndarray_type = {
105105
#if NDARRAY_HAS_BINARY_OPS
106106
.binary_op = ndarray_binary_op,
107107
#endif
108+
.buffer_p = { .get_buffer = ndarray_get_buffer, },
108109
.locals_dict = (mp_obj_dict_t*)&ulab_ndarray_locals_dict,
109110
};
110111

docs/ulab-change-log.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
Sun, 21 Feb 2021
22

3+
version 2.4.3
4+
5+
re-introduce ndarray_get_buffer, and buffer protocol
6+
7+
Sun, 21 Feb 2021
8+
39
version 2.4.2
410

511
fix ndarray_is_dense, eye, ones, full, and zeros for Boolean type

0 commit comments

Comments
 (0)