[C++-sig] wrapped std::vector slice affecting items in a python list

Jim Bosch talljimbo at gmail.com
Tue Aug 16 19:35:35 CEST 2011

On 08/16/2011 07:32 AM, babak wrote:
> Hi Jim
> Thanks for your response.
> I'm abit surprised that this is the case, I kind of thought that
> indexing_suite was there to bridge this incompatibility. Is it normal then
> for large projects reliant on value based containers to copy data when going
> from python to c++ ?

I don't really know what normal is; my particular area is scientific 
computing, so I actually use numpy arrays in almost all the cases where 
I'm dealing with huge containers.  I do tend to copy data when dealing 
with small containers of more complex objects, and when it's really an 
issue, I make my C++ objects cheap to copy (via copy on write or 
something).  Things are also a lot safer if you only wrap vectors as 
const; it's exposing the mutators to Python that gets really difficult. 
  Finally, if your C++ code works on iterators rather than containers, 
you can avoid copying by using stl_input_iterator to iterate directly 
over the Python containers.

One large project I'm a part of uses vectors of shared_ptr extensively 
on the C++ side to combat this problem (even though they use SWIG, not 
Boost.Python).  If you have control over the C++, that's often the best 
solution, regardless of how you're exposing them to Python.

I would that any large project that uses SWIG with value-based 
containers is probably mostly ignoring the safety issues; unlike 
Boost.Python, SWIG simply doesn't have the facilities to protect you 
from null pointers and dangling references, so its STL wrappers don't do 
anything to prevent such things.  The onus is on the Python user to be safe.

On the Boost.Python side, indexing_suite essentially does the best it 
can to make things safe and full-featured, and while it can't totally 
foolproof your code, it does make it a lot more difficult to segfault 
accidentally.  If you find yourself needing it a lot, you may want to 
looking into indexing suite v2, which I believe is best obtained through 
the Py++ package.


More information about the Cplusplus-sig mailing list