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

HOUSSEN Franck fghoussen at gmail.com
Tue Feb 11 16:13:02 EST 2020


Works great ! Thanks ! Code attached.

Franck

>> make; python dummyTest1.py; python dummyTest2.py; python dummyTest3.py

g++ -I/usr/include/python2.7 -o dummy.so -shared -fPIC dummy.cpp
-lboost_python -lboost_numpy

[1 2 3]
[1.2 2.3 3.4]
[4.5 5.6 6.7]
ptrInt[0] = 1, 0x555a42aede80
ptrInt[1] = 2, 0x555a42aede84
ptrInt[2] = 3, 0x555a42aede88
ptrFloat[0] = 1.2, 0x555a429c4f60
ptrFloat[1] = 2.3, 0x555a429c4f64
ptrFloat[2] = 3.4, 0x555a429c4f68
ptrDouble[0] = 4.5, 0x555a429c4f80
ptrDouble[1] = 5.6, 0x555a429c4f88
ptrDouble[2] = 6.7, 0x555a429c4f90

[1 2 3]
[1.2 2.3 3.4]
[4.5 5.6 6.7]
ptrInt[0] = 1, 0x55dbaa536470
ptrInt[1] = 2, 0x55dbaa536474
ptrInt[2] = 3, 0x55dbaa536478
ptrFloat[0] = 1.2, 0x55dbaa4b9bb0
ptrFloat[1] = 2.3, 0x55dbaa4b9bb4
ptrFloat[2] = 3.4, 0x55dbaa4b9bb8
ptrDouble[0] = 4.5, 0x55dbaa4b9bd0
ptrDouble[1] = 5.6, 0x55dbaa4b9bd8
ptrDouble[2] = 6.7, 0x55dbaa4b9be0

[1 2 3]
[1.2 2.3 3.4]
[4.5 5.6 6.7]
ptrInt[0] = 1, 0x563725f958f0
ptrInt[1] = 2, 0x563725f958f4
ptrInt[2] = 3, 0x563725f958f8
ptrFloat[0] = 1.2, 0x563725e87a10
ptrFloat[1] = 2.3, 0x563725e87a14
ptrFloat[2] = 3.4, 0x563725e87a18
ptrDouble[0] = 4.5, 0x563725fc0de0
ptrDouble[1] = 5.6, 0x563725fc0de8
ptrDouble[2] = 6.7, 0x563725fc0df0

Le mar. 11 févr. 2020 à 20:12, stefan <stefan at seefeld.name> a écrit :

>
> On 2020-02-11 2:05 p.m., HOUSSEN Franck wrote:
>
> 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 ?
>
> Can't you change your Python code so instead of
>
>   np.append(lsFloat, 1.2)
>
> you would call
>
>   np.append(lsFloat, np.float32(1.2))
>
> to make sure the casting happens before the broadcasting.
>
> (Note: I haven't actually tried that; it just seems the natural fix to the
> issue ;-) )
> [image: Stefan]
>
> --
>
>       ...ich hab' noch einen Koffer in Berlin...
>
>
> _______________________________________________
> 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/63f7cf96/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.png
Type: image/png
Size: 1478 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20200211/63f7cf96/attachment-0001.png>
-------------- 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/63f7cf96/attachment-0003.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/63f7cf96/attachment-0004.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/63f7cf96/attachment-0001.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: dummyTest2.py
Type: text/x-python
Size: 663 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20200211/63f7cf96/attachment-0005.py>
-------------- 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/63f7cf96/attachment-0001.cpp>


More information about the Cplusplus-sig mailing list