python / python/cpython

[ENH] Support F-contiguous `memoryview`s in `cast`

Offen
#91,484 2 Kommentare 0 Reaktionen 2 zugewiesene Personen Auf GitHub ansehen

@jamie2779 arbeitet bereits daran.

Seit 15.8.2025.

interpreter-core type-feature
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
36k
Ø Merge
1 T. 9 Std.
Gemergte PRs (30 T.)
558

Beschreibung

Currently memoryview.cast requires C-contiguous data when going from N-D to 1-D. As a result F-contiguous data is not supported. Please see example below:

>>> mv.c_contiguous
False
>>> mv.f_contiguous
True
>>> mv.cast("B")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: memoryview: casts are restricted to C-contiguous views

However as F-contiguous data is still contiguous and can be viewed in 1-D, it seems possible to support this type of data as well. PickleBuffer's raw method does appear to support both C & F contiguous data. Building off the example above:

>>> from pickle import PickleBuffer
>>> pb = PickleBuffer(mv)
>>> pb.raw()
<memory at 0x1105cec70>

This would be useful when interacting with other libraries that work with this kind of data. That said, don't know if there is other context behind the C-contiguous constraint that I may be missing here.

Linked PRs
  • gh-137803

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.