Wrapping std::vector<int> with boost::python::list

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Wrapping std::vector<int> with boost::python::list

fjanoos
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

Reply | Threaded
Open this post in threaded view
|

Re: Wrapping std::vector<int> with boost::python::list

Stefan Seefeld-2
On 08/09/2011 05:17 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.

Note that this should work, but requires you to instruct Python that
'vcs' is an "inout" value, so it gets passed into the function
by-reference, not by-value (the default). Please read the tutorial (and
reference) sections on call policies:
http://www.boost.org/doc/libs/1_46_0/libs/python/doc/tutorial/doc/html/python/functions.html#python.call_policies

>
> 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>'

This is a little trick (you could call it a kludge) to flag a user error
using metaprogramming: the last line above should be spoken out aloud:
Boost.Python noticed an ambiguous situation, and instead of guessing
what (return) value ownership semantics you intended, it requests you to
specify it (return value policies are very similar to call policies, so
the same docs above apply).


>
>
> 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>

Python list objects are themselves shared pointers, so Python wouldn't
know what to do with a shared_ptr<python::list>.

Regards,
        Stefan

--

      ...ich hab' noch einen Koffer in Berlin...

_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig
Reply | Threaded
Open this post in threaded view
|

Re: Wrapping std::vector<int> with boost::python::list

Jim Bosch-2
In reply to this post by fjanoos
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
> [hidden email]
> http://mail.python.org/mailman/listinfo/cplusplus-sig

_______________________________________________
Cplusplus-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/cplusplus-sig