Skip to content

Commit 4221262

Browse files
authored
Merge pull request #338 from v923z/master-buffer
re-introduce ndarray_get_buffer, and buffer protocol
2 parents ce763eb + 06cb1c2 commit 4221262

File tree

6 files changed

+47
-1
lines changed

6 files changed

+47
-1
lines changed

code/ndarray.c

+12
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

+1
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

+2-1
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

+6
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

tests/common/buffer.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
try:
2+
from ulab import numpy as np
3+
except:
4+
import numpy as np
5+
6+
def print_as_buffer(a):
7+
print(len(memoryview(a)), list(memoryview(a)))
8+
print_as_buffer(np.ones(3))
9+
print_as_buffer(np.zeros(3))
10+
print_as_buffer(np.eye(4))
11+
print_as_buffer(np.ones(1, dtype=np.int8))
12+
print_as_buffer(np.ones(2, dtype=np.uint8))
13+
print_as_buffer(np.ones(3, dtype=np.int16))
14+
print_as_buffer(np.ones(4, dtype=np.uint16))
15+
print_as_buffer(np.ones(5, dtype=np.float))
16+
print_as_buffer(np.linspace(0, 1, 9))
17+

tests/common/buffer.py.exp

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
3 [1.0, 1.0, 1.0]
2+
3 [0.0, 0.0, 0.0]
3+
16 [1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0]
4+
1 [1]
5+
2 [1, 1]
6+
3 [1, 1, 1]
7+
4 [1, 1, 1, 1]
8+
5 [1.0, 1.0, 1.0, 1.0, 1.0]
9+
9 [0.0, 0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875, 1.0]

0 commit comments

Comments
 (0)