[C++-sig] [boost python] : how to pass a tuple of lists from python to C++

Jakob van Santen jvansanten at gmail.com
Tue Feb 11 08:56:58 EST 2020


> On Feb 11, 2020, at 13:24, HOUSSEN Franck <fghoussen at gmail.com> wrote:
> 
> Finally able to reproduce the "real" problem with a "dummy" example : seems that, at python side, when you use "np.append" C++ get screwed data ?!... (note that with or without "np.append" all is OK at python side).
> Can somebody help here ? Known problem ? Possible workaround or fix ? Should I open a bug for that ? (if yes where)

This is a side-effect of how np.append() broadcasts its arguments to find a common type. You've asked it to append a Python float (which is secretly a double) to an array of np.float32. The only safe way to do that is to cast both to numpy.float64, which np.append() happily does for you. Before you reinterpret_cast the data pointer of an ndarray to a given type, you need to check whether the dtype is compatible (and also, strictly speaking, whether the flags are CARRAY or CARRAY_RO).

There's a whole lot more background information in the Numpy C API docs: https://docs.scipy.org/doc/numpy/reference/c-api.html

Cheers,
Jakob


More information about the Cplusplus-sig mailing list