[C++-sig] Wrapping std::vector<int> with boost::python::list

Jim Bosch talljimbo at gmail.com
Tue Aug 9 23:37:13 CEST 2011


Just return boost::python::object by value; it's actually a smart 
pointer that carries a PyObject* and uses Python's reference counting, 
and you can implicitly create one from boost::python::list (since list 
derives from object).

Just returning boost::python::list by value might also work, but that 
copy constructor might be overloaded to do deep copies (I'm not sure), 
and you seem to be trying to avoid that.

Jim




On 08/09/2011 02:21 PM, fjanoos wrote:
> Hello,
>
> I'm new to boost::python and was having trouble wrapping a C++ function of
> the form
>   void FindVCs(int vId, vector<int>&  vcs);
>
> Here vcs is allocated in the caller and populated by FindVCs.
>
> Initially, I considered wrapping it something like this:
>
>          boost::python::list* FindVCs_wrap(int vid)
>          {
>                  vector<int>  vcs;
>
>                 FindVCs(vid,vcs);
>                  //wrap and return
>                  boost::python::list* out_list = new boost::python::list;
>                  unsigned int num_elems = vcs.size();
>                  for( unsigned int idx = 0; idx<  num_elems; idx++){
>                          out_list->append(vcs[idx]);
>                  }
>                  return out_list;
>          }
>
> however, this gives me compile time errors
> Error 5 error C2027: use of undefined type
> 'boost::python::detail::specify_a_return_value_policy_to_wrap_functions_returning<T>'
>
> Then I change the return type to
>
> boost::shared_ptr<boost::python::list>  FindVCs_wrap(int vid)
> {
>   ...
>          return boost::shared_ptr<boost::python::list>(out_list);
> }
>
> This compiles fine but then at runtime Python raises:
>
> TypeError: No to_python (by-value) converter found for C++ type: class
> boost::shared_ptr<class boost
> ::python::list>
>
>
> Any ideas of what I am doing wrong ?
>
> thanks,
> -fj
>
>
>
> --
> View this message in context: http://boost.2283326.n4.nabble.com/Wrapping-std-vector-int-with-boost-python-list-tp3731291p3731291.html
> Sent from the Python - c++-sig mailing list archive at Nabble.com.
> _______________________________________________
> Cplusplus-sig mailing list
> Cplusplus-sig at python.org
> http://mail.python.org/mailman/listinfo/cplusplus-sig



More information about the Cplusplus-sig mailing list