Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Slice operation on memoryview causes allocation #7718

Closed
@peterhinch

Description

@peterhinch

The docs lead one to believe that slicing a memoryview should be allocation free, but this seems not to be the case.

This arose in this forum thread. The user has a timer ISR which uses SPI to read first the number of bytes to read, followed by a subsequent SPI read of the payload. I suggested a memoryview, but this does not work as the slice operation causes an allocation.

I replicated the issue with this sample which can be pasted to a Pyboard:

from machine import SPI
from pyb import Timer
import micropython
import time
micropython.alloc_emergency_exception_buf(100)

s = SPI(1)
buf = bytearray(20)
mv = memoryview(buf)
tim = Timer(1)

def cb(t):
    #s.readinto(mv)  # Works
    s.readinto(mv[:5])  # Memory error

tim.init(freq=10, callback=cb)
while True:
    print(buf)
    time.sleep(1)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions