[C++-sig] [c++-sig] How does C++ cooperate with python array?

Roman Yakovenko roman.yakovenko at gmail.com
Mon Jun 4 20:12:53 CEST 2007


On 6/4/07, 甜瓜 <littlesweetmelon at gmail.com> wrote:
> I'm sure it is a newbie question. ^_^
> In my python program, it will generate a very big array stored in
> array.array.

It seems that this module doesn't provide C API. Is it possible to get
access from C++ to the underlying array?

> The data processing is time consuming. So I want to move
> it into C++, and return the result array back to python.

A new array or you want to reuse the array instance you pass as argument.

> Could you give me a related example by using Boost.Python library? It
> may like this:
> ============
> # python code
> ar = array(...)
> ret = func(ar)  # call a Boost.Python exported funtion which accepts a
> array.array parameter.

You will not find example that does exactly what you want.
Boost.Python tutorials is a good place to start. You can always right
something like this:

boost::python::object func( boost::python::object ){
    ...
}


def( "func", &::func );


> ...
>
> # C++ code
> array func(array& ar)  // just simple array.array, not numpy.array

What do you mean simple array "int*" or instance of array.array type?

> {
>    ...
>    //return array
> }
>
> BOOST_PYTHON_MODULE() {...}
>
> ============
> Another concern is about the memory management. If I create a new
> array in C++ part, who will take charge of memory release?

It depends. If you will write the right code than Boost.Python,
otherwise you( your users )

> And, is
> there any implicit data-copying on the way that the array is passed
> between C++ and python?

You don't have to copy the whole array, but you should understand that
you do construct new Python object every time you access an item
within the array

>Due to the size of the array is very large,
> any unnecessary copy will lead to poor performance.

> Is there anything I should pay attention to when using Boost.Python?

Well it is possible to create clean solution, that will not force you
copy arrays.

May be you should consider to expose std::vector< your type > (
http://boost.org/libs/python/doc/v2/indexing.html#vector_indexing_suite
)
and implement all interface using it. Thus you will have good
performance and memory management.

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



More information about the Cplusplus-sig mailing list