diff options
| author | Damien George <damien@micropython.org> | 2021-05-18 00:23:13 +1000 |
|---|---|---|
| committer | Damien George <damien@micropython.org> | 2021-05-18 10:18:56 +1000 |
| commit | 6d2680fa36696fd0b2fd7a595f4a26153cd84e07 (patch) | |
| tree | f706f1f523bd5c0b135e66fb1aa4b7a48920096b | |
| parent | 1446107b4d24915143f8212c2f649a0e628735cc (diff) | |
py/objarray: Fix constructing a memoryview from a memoryview.
Fixes issue #7261.
Signed-off-by: Damien George <damien@micropython.org>
| -rw-r--r-- | py/objarray.c | 8 | ||||
| -rw-r--r-- | tests/basics/memoryview_gc.py | 10 |
2 files changed, 18 insertions, 0 deletions
diff --git a/py/objarray.c b/py/objarray.c index be85674f8..16a4d4aac 100644 --- a/py/objarray.c +++ b/py/objarray.c @@ -220,6 +220,14 @@ STATIC mp_obj_t memoryview_make_new(const mp_obj_type_t *type_in, size_t n_args, bufinfo.len / mp_binary_get_size('@', bufinfo.typecode, NULL), bufinfo.buf)); + // If the input object is a memoryview then need to point the items of the + // new memoryview to the start of the buffer so the GC can trace it. + if (mp_obj_get_type(args[0]) == &mp_type_memoryview) { + mp_obj_array_t *other = MP_OBJ_TO_PTR(args[0]); + self->memview_offset = other->memview_offset; + self->items = other->items; + } + // test if the object can be written to if (mp_get_buffer(args[0], &bufinfo, MP_BUFFER_RW)) { self->typecode |= MP_OBJ_ARRAY_TYPECODE_FLAG_RW; // indicate writable buffer diff --git a/tests/basics/memoryview_gc.py b/tests/basics/memoryview_gc.py index d366cbbb1..5cd6124ad 100644 --- a/tests/basics/memoryview_gc.py +++ b/tests/basics/memoryview_gc.py @@ -21,3 +21,13 @@ for i in range(100000): # check that the memoryview is still what we want print(list(m)) + +# check that creating a memoryview of a memoryview retains the underlying data +m = None +gc.collect() # cleanup from previous test +m = memoryview(memoryview(bytearray(i for i in range(50)))[5:-5]) +print(sum(m), list(m[:10])) +gc.collect() +for i in range(10): + list(range(10)) # allocate memory to overwrite any reclaimed heap +print(sum(m), list(m[:10])) |
