Skip to content

Supporting strides in memoryview.cast #85398

Open

Description

@jakirkham
mannequin
BPO 41226
Nosy @vedgar, @jakirkham

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2020-07-07.02:01:27.687>
labels = ['type-feature', 'library', '3.9', '3.10', '3.11']
title = 'Supporting `strides` in `memoryview.cast`'
updated_at = <Date 2022-01-13.19:54:52.107>
user = 'https://github.com/jakirkham'

bugs.python.org fields:

activity = <Date 2022-01-13.19:54:52.107>
actor = 'jakirkham'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2020-07-07.02:01:27.687>
creator = 'jakirkham'
dependencies = []
files = []
hgrepos = []
issue_num = 41226
keywords = []
message_count = 3.0
messages = ['373202', '402944', '410515']
nosy_count = 2.0
nosy_names = ['veky', 'jakirkham']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue41226'
versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']

Activity

  1. jakirkham commented on Jul 7, 2020

    jakirkhammannequin
    MannequinAuthor

    Currently one can reshape a memoryview using .cast(...) like so...

    In [1]: m = memoryview(b"abcdef")
    
    In [2]: m2 = m.cast("B", (2, 3))
    

    However it is not currently possible to specify the strides when reshaping the memoryview. This would be useful if the memoryview should be F-order or otherwise strided. To that end, syntax like this would be useful...

    In [1]: m = memoryview(b"abcdef")
    
    In [2]: m2 = m.cast("B", (2, 3), (1, 2))
    
  2. added
    3.11only security fixes
    stdlibStandard Library Python modules in the Lib/ directory
    type-featureA feature request or enhancement
    on Sep 27, 2021
  3. vedgar commented on Sep 30, 2021

    vedgarmannequin
    Mannequin

    I surely don't understand what the third argument means. What's (1, 2) there, and what stride does it produce?

  4. jakirkham commented on Jan 13, 2022

    jakirkhammannequin
    MannequinAuthor

    The 2nd argument is the strides. IOW it is just specifying how to traverse the buffer in memory to visit each of the dimensions.

    For the first example where strides is not specified, Python makes them C-ordered. IOW m2.strides would be (3, 1). Effectively this is represented like this:

    [[ b"a", b"c", b"e"],
     [ b"b", b"d', b"f"]]
    

    For the second case where strides are overridden (so m2.strides would be (1, 2)), we get something like this:

    [[b"a", b"b", b"c"],
     [b"d", b"e", b"f"]]
    

    In either case the 1 here has specified which dimension is fastest to traverse along. IOW that content is adjacent in memory.

    Should add the reason it is 1 is that for uint8_t (or format "B"), this is that type's size. If we had a different format, this would be the size of that format.

    HTH

  5. transferred this issue fromon Apr 10, 2022
  6. serhiy-storchaka commented on Jul 13, 2026

    @serhiy-storchaka
    Member

    The motivating example here — m.cast("B", (2, 3), (1, 2)) — is exactly an F-contiguous view, which is being added as memoryview.cast(..., order='F') in gh-78959 (PR gh-153663):

    >>> m.cast("B", (2, 3), order='F').strides
    (1, 2)

    Supporting arbitrary strides is a much bigger and riskier feature: cast currently guarantees the view exactly covers the buffer (product(shape) * itemsize == len), whereas free strides can alias or read outside the buffer — essentially numpy's as_strided, which is notoriously unsafe.

    @jakirkham, do you have a concrete use case for arbitrary strides that isn't covered by order='F'?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.10 (EOL)end of life3.11only security fixes3.9 (EOL)end of lifependingThe issue will be closed if no feedback is providedstdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions