[C++-sig] Exposing C Array members to python using boost

Roman Yakovenko roman.yakovenko at gmail.com
Fri Dec 21 23:00:09 CET 2007


On Dec 20, 2007 1:41 AM, Carpman, David <dcarpman at ll.mit.edu> wrote:
> Hello,
>
> I've been reading the archives on the topic of how to expose read/write access of C-arrays to python using boost::python, but what I could find was several years old. Some of the examples reference files that no longer exist, particularly in the indexing/container suite area. I would very much appreciate some wisdom on this subject from this list, if folks would be so kind. :)
>
> >From what I've read, Raoul Gough's indexing suite is needed to convert the arrays to python lists. I see the vector and map examples in the boost distribution (I have tried 1.35-PR1 and svn rev 42152) but nothing having to do with arrays. http://osdir.com/ml/python.c++/2003-11/msg00185.html seems to indicate Raoul had a version of the indexing suite that supported arrays in 2003, but I can't find anything about it. Has anything changed or been done since 2003 for this?

New indexing suite is not integrated with Boost.Python. You can find
it here: http://pygccxml.svn.sourceforge.net/viewvc/pygccxml/pyplusplus_dev/indexing_suite_v2/

> Here is my situation: I'm trying to wrap some legacy C-structures that define a network packet structure that will be written by python and read by some embedded computer. I have no control over the definition of these structures, nor can I add to or change the header files. My hang-up is that some of the members in these structures are defined as C-arrays, for example, int numsamples[32]. I would like to have access in python to the elements of these arrays for reading and writing. Scalar primitive members work fine.

> I would like to avoid having to write C++ wrapper classes for the C-structs containing the C-arrays, as there are many dozens of them.
> I'd be very thankful if someone could help!

Take a look on next code:

http://pygccxml.svn.sourceforge.net/viewvc/pygccxml/pyplusplus_dev/pyplusplus/code_repository/array_1.py?revision=1097&view=markup

search for register_array_1 function.

You can also to try
Py++(http://language-binding.net/pyplusplus/pyplusplus.html), code
generator for Boost.Python library, it handles such cases
automatically.

-- 
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/



More information about the Cplusplus-sig mailing list