[Numpy-discussion] Best way to expose std::vector to be used with numpy

Daniele Nicolodi daniele at grinta.net
Tue Oct 14 10:17:49 EDT 2014

On 14/10/14 14:11, Daniele Nicolodi wrote:
> On 14/10/14 13:39, Nathaniel Smith wrote:
>> If the goal is to have something that works kind of like the new buffer
>> protocol but with a wider variety of python versions, then you might
>> find the old array interface useful:
>> http://docs.scipy.org/doc/numpy/reference/arrays.interface.html
>> I always get confused by the history here but I believe that that's the
>> numpy-only interface that later got cleaned up and generalized to become
>> the new buffer interface. Numpy itself still supports it.
> Hello Nathaniel,
> thanks for the pointer, that's what I need.
> However, reading the documentation you linked, it looks like the new
> buffer protocol is also available in Python 2.6, and I don't have the
> need to support older versions, and this is confirmed by the python
> documentation:
> https://docs.python.org/2.7/c-api/buffer.html

I found one more problem: one of the data types I would like to expose
is a complex float array equivalent to numpy.complex64 dtype. However,
there is not such type format string defined in the python struct module,

Experimenting with numpy it seems like it extends the struct format
string definition with a new modifier 'Z' for complex:

>>> a = np.ones(10, dtype=np.complex64)
>>> m = memoryview(a)
>>> m.format

How "standard" is that?  Should I do the same in my extension?

Thank you.


More information about the NumPy-Discussion mailing list