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

HOUSSEN Franck fghoussen at gmail.com
Tue Feb 11 14:05:08 EST 2020


OK, I understand this is a type related problem. I found a workaround
(dummyTest3.py - init numpy arrays with list seems to work).

But, I unfortunately do not get how to change the code to get dummyTest2.py
to work ?!... Should I read reinterpret_cast'ed data sizeof(double) by
sizeof(double) : seems cryptic (?!), what's the natural way to do that ?

Also a question raises : if the size of the data type you want to pass from
python to C++ is smaller than sizeof(double), python will always allocate
MORE memory than needed ? Why is that ? In case the size is large, the
extra memory allocated may be large and trigger problems like slowdown
(swapping) or out-of-memory, no ?... Why is this like so ?

Can somebody help to fix the dummy code : this would be the best
explanation !... :D

Franck

Le mar. 11 févr. 2020 à 14:57, Jakob van Santen <jvansanten at gmail.com> a
écrit :

>
> > 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
> _______________________________________________
> Cplusplus-sig mailing list
> Cplusplus-sig at python.org
> https://mail.python.org/mailman/listinfo/cplusplus-sig
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20200211/9c7b2e6e/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: dummy.cpp
Type: text/x-c++src
Size: 1460 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20200211/9c7b2e6e/attachment.cpp>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: dummyTest3.py
Type: text/x-python
Size: 324 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20200211/9c7b2e6e/attachment.py>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: dummyTest1.py
Type: text/x-python
Size: 300 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20200211/9c7b2e6e/attachment-0001.py>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Makefile
Type: application/octet-stream
Size: 100 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20200211/9c7b2e6e/attachment.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: dummyTest2.py
Type: text/x-python
Size: 561 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20200211/9c7b2e6e/attachment-0002.py>


More information about the Cplusplus-sig mailing list