Skip to content

Memoryview for column-major (f_contiguous) arrays from bytes impossible to achieve #78959

Closed

Description

@lgautier
mannequin
BPO 34778
Nosy @ncoghlan, @pitrou, @skrah, @mattip

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 = 'https://github.com/skrah'
closed_at = None
created_at = <Date 2018-09-23.17:51:38.368>
labels = ['extension-modules', '3.8', 'type-feature', 'library', '3.7']
title = 'Memoryview for column-major (f_contiguous) arrays from bytes impossible to achieve'
updated_at = <Date 2021-10-03.13:34:19.879>
user = 'https://bugs.python.org/lgautier'

bugs.python.org fields:

activity = <Date 2021-10-03.13:34:19.879>
actor = 'lgautier'
assignee = 'skrah'
closed = False
closed_date = None
closer = None
components = ['Extension Modules', 'Library (Lib)']
creation = <Date 2018-09-23.17:51:38.368>
creator = 'lgautier'
dependencies = []
files = []
hgrepos = []
issue_num = 34778
keywords = []
message_count = 16.0
messages = ['326167', '326276', '326660', '326684', '326689', '332743', '332751', '332766', '332805', '334519', '334520', '334622', '334742', '334783', '350933', '403086']
nosy_count = 5.0
nosy_names = ['ncoghlan', 'pitrou', 'lgautier', 'skrah', 'mattip']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue34778'
versions = ['Python 3.4', 'Python 3.5', 'Python 3.6', 'Python 3.7', 'Python 3.8']

Linked PRs

Activity

  1. lgautier commented on Sep 23, 2018

    lgautiermannequin
    MannequinAuthor

    The buffer protocol is accounting for the row-major or column-major arrays, and that information is shown in the attributes, c_contiguous and f_contiguous respectively, of a memoryview object.

    Using the method cast allows one to specify a shape but does not allow
    to specify whether row or column major:

    # column-major 3x2 array of bytes that was serialized
    b = bytearray([1,2,3,4,5,6])
    
    mv = memoryview(b)
    mv_b = mv.cast('b', shape=(3,2))
    

    The result object is believed to be row-major and little can be done
    to correct it:

    >>> mv_int.c_contiguous
    True
    >>> mv_int.c_contiguous = False
    AttributeError: attribute 'c_contiguous' of 'memoryview' objects is not writable
    
  2. mattip commented on Sep 24, 2018

    @mattip
    Contributor

    This could be done via a shape kwarg to cast

  3. lgautier commented on Sep 28, 2018

    lgautiermannequin
    MannequinAuthor

    @mattip : do you mean that it can currently be achieved by calling cast with a specific shape parameter ? If the case, how so ?

  4. mattip commented on Sep 29, 2018

    @mattip
    Contributor

    Sorry, I meant a "strides" keyword. "shape" is already a valid keyword

  5. lgautier commented on Sep 29, 2018

    lgautiermannequin
    MannequinAuthor

    Wouldn't a contiguity argument ('C' or 'F') be simpler ?

    (Independently, an argument strides is likely also missing from "cast").

    Do you know what are the next possible steps here ? Bring this to the python-dev list ? Submit a patch ?

  6. added
    stdlibStandard Library Python modules in the Lib/ directory
    and removed
    interpreter-core(Objects, Python, Grammar, and Parser dirs)
    on Sep 29, 2018
  7. lgautier commented on Dec 30, 2018

    lgautiermannequin
    MannequinAuthor

    Bump.

    What are the next steps here ?

  8. skrah commented on Dec 30, 2018

    skrahmannequin
    Mannequin

    memoryview.cast() was originally meant to be a faster version of tobytes(), which always converts to C-contiguous.

    The 'shape' keyword was added because it is odd if you can cast from ND-C to 1D-Bytes but not back.

    I'm not sure if we should introduce that feature, just pointing out that the original decision to exclude non 'C' views was deliberate.

  9. lgautier commented on Dec 30, 2018

    lgautiermannequin
    MannequinAuthor

    Wait. Isn't a memoryview memerely a Python object for a buffer inferface, whatever its valid attributes or flags might be ?

    The perceived oddness that lead to the addition of the keyword 'shape' was a good initial instinct that something was off, but this is an incomplete workaround .

    If the rationale was to follow what tobytes is doing, this delegates the justification for excluding non 'C' views it. Then I do not understand the rationale behind memoryview.tobytes's exclusive relationshop to C-contiguous arrays. A memmoryview is a window on a memory region (a Python buffer), and one would expect tobytes to just return bytes for it (in whatever bytes/strides) the memoryview is originally in.

  10. 6 remaining items

  11. self-assigned this
    on Feb 5, 2019
  12. lgautier commented on Aug 31, 2019

    lgautiermannequin
    MannequinAuthor

    Bump.

    Is there anything I could do to move this forward (as in write and submit a patch for review) ?

  13. lgautier commented on Oct 3, 2021

    lgautiermannequin
    MannequinAuthor

    Bump.

    I am still stumbling on that issue. I have a workaround by importing numpy but that's a rather large dependency for that one little thing.

    I see that the related issue https://bugs.python.org/issue35845 opened later has later discussion entries.

  14. transferred this issue fromon Apr 10, 2022
  15. mattip commented on May 13, 2022

    @mattip
    Contributor

    Note the bpo issue 35845 is now #80026 on github

  16. serhiy-storchaka commented on Jul 13, 2026

    @serhiy-storchaka
    Member

    Opened #153663.

    It adds a keyword-only order parameter to memoryview.cast(); with order='F' you get a zero-copy Fortran-contiguous (column-major) view of a flat buffer, mirroring memoryview.tobytes(order=...).

  17. added a commit that references this issue on Aug 3, 2026
  18. added
    3.16new features, bugs and security fixes
    and removed on Aug 3, 2026
  19. added a commit that references this issue on Aug 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

3.16new features, bugs and security fixesextension-modulesC modules in the Modules dirstdlibStandard 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