[Numpy-discussion] PyBUF_SIMPLE/PyBUF_FORMAT: casts to unsigned bytes

Stefan Krah stefan-usenet at bytereef.org
Fri Sep 2 10:03:56 EDT 2011

Stefan Krah <stefan-usenet at bytereef.org> wrote:
> Dag Sverre Seljebotn <d.s.seljebotn at astro.uio.no> wrote:
> > Under 2), would it make sense to also export the contents of a
> > Fortran-contiguous buffer as a raw byte stream? I was just the other week
> > writing code to serialize an array in Fortran order to a binary stream.
> Probably, since it works now and people might have gotten used to it.

There are two other considerations why a "simple" view should be

1) memoryview.tobytes() converts a Fortran-contiguous buffer to the C layout.
   I would find it odd if simple_view.tobytes() is different from the raw

2) There is an initiative (with very broad support) to add a cast method
   to memoryview. The following casts are allowed:

     - any ND (C-contiguous) -> 1D bytes

     - any 1D bytes -> ND (C-contiguous)

   This is how it looks (in my private repo):

      >>> from _testbuffer import *
      >>> nd = ndarray(list(range(12)), shape=[3,4], format="Q")
      >>> m1 = memoryview(nd)
      >>> m2 = m1.cast('B')
      >>> m3 = m2.cast('Q', shape=[3,4])
      >>> m4 = m2.cast('Q', shape=[2,2,3])

To summarize, I think it will be more consistent if an implicit cast via
PyBUF_SIMPLE also disallows Fortran arrays. Unless there are waves of
protest here... ;)

Stefan Krah

More information about the NumPy-Discussion mailing list