Exposing buffer interface for non-extension types?

Carl Banks pavlovevidence at gmail.com
Tue Jul 20 20:28:12 EDT 2010


On Jul 20, 3:09 pm, Ken Watford <kwatford+pyt... at gmail.com> wrote:
> Is there any way to expose the PEP 3118 buffer interface for objects
> that aren't extension types?
>
> Currently, I can expose the NumPy array interface (using either
> __array_interface__ or __array_struct__) for any class, extension or
> otherwise. But I can't find any reference to python-side interfacing
> for PEP 3118. SWIG makes an extension module for your wrapped code,
> but not extension *types*, so the classes it produces are pure-python
> with methods added in from the extension module.
>
> The NumPy array interface works fine for now (especially since NumPy
> is the only thing I need to consume these objects), but the
> documentation claims that it's being deprecated in favor of PEP 3118,
> so I thought it might be relevant to bring this up.

Can you tell let us know what you want to use it for?  We could offer
better help.

Numpy is generally how I get at buffers in Python 2.x.  For instance
if I have an object m that supports buffer protocol (m could a string,
mmap object, Python array, etc.), then the following will create an
array view of the same buffer:

numpy.ndarray((10,10),type=numpy.float32,buffer=m)

As far as I know this procedure won't be too different under PEP 3118;
if anything it's simplified in Python 3 since it can discover type and
shape information itself.  (You'll have to check with the numpy people
on that.)


Carl Banks



More information about the Python-list mailing list