← index #2552PR #4371
Likely Duplicate · high · value 0.563
QUERY · ISSUE

Add .offset()/.seek() method for memoryview for inplace "slicing"

openby pfalconopened 2016-10-23updated 2018-12-01
enhancementrfc

Here's an idiom to send a large data buffer thru a short-write stream (e.g., non-blocking socket):

        buffer = memoryview(data).cast('b')
        while buffer:
            try:
                nsent = self._socket_send(buffer, flags)
                buffer = buffer[nsent:]

The last statement still produces bunch of object garbage. If we could do

buffer.offset(nsent)

it would be cool. Suggestions for better name are welcome.

CANDIDATE · PULL REQUEST

py/objarray: Allow to create memoryview with offset/length.

closedby pfalconopened 2018-12-20updated 2022-05-06
py-core

Constructor signature is extended to:

memoryview(buf, offset, size)

This is equivalent to:

m = memoryview(buf)
m[offset:offset + size]

But allocates just 1 memoryview object instea of 2.

Also, .init(buf, offset, size) method added, which allows to reuse
(without allocation) existing memoryview object for new buffer object
(or to update "window" of memoryview into the buffer).

Change-Id: I1f864b908642b256305dc5caebf30662c306b1e0

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied