[C++-sig] call policies

Ricardo Kirkner ricardokirkner at gmail.com
Tue Nov 8 00:06:19 CET 2005


Hi:

Thank you, this worked... however, pyste is still complaining about my
method (the one that returned a double*) returns a pointer, and has no
policy defined -- although this method now uses a wrapper which does
not return a pointer but a tuple...

On the other hand, I have read that the operator << should be replaced
by the str function. However,

1. pyste isn't doing it.. (might be a thing with pyste)
2. when I replace that operator with .def( str( self ) ), compilation fails with

no matching function for call to
'boost::python::api::object::visit(boost::python::class_<myclass,
boost::python::detail::not_specified,
boost::python::detail::not_specified,
boost::python::detail::not_specified>&) const'

any ideas?

thank you



On 11/7/05, Ralf W. Grosse-Kunstleve <rwgk at yahoo.com> wrote:
> --- Ricardo Kirkner <ricardokirkner at gmail.com> wrote:
>
> > this time I have a question regarding call policies. I have a function
> > that returns a double * which actually is an array of doubles.
> >
> > I am required to provide a policy for this functions, however I cannot
> > find out which one would apply here (from how well I understand each
> > posibility).
> >
> > Can anyone please enlighten me?
>
> I don't think call policies will help you here. More fundamental
> considerations are required.
>
> The Python scripting level is supposed to provide a safe environment.
> According to the Python spirit is should be impossible to provoke a
> segmentation fault from the scripting level, or to access uninitialized
> memory. Of course you can undermine this via C/C++, but Boost.Python
> doesn't support grossly unsafe practices.
>
> In your particular case, you have to tell Python the size of the
> double* array, and you have to take care all elements are initialized.
> There are several alternatives to achieve this. A simple alternative is
> to write a "thin wrapper" which copies the double* array to a new
> Python list of floats. It goes like this (all untested):
>
> your.hpp:
>
>   class yours
>   {
>     public:
>       unsigned
>       number_of_values() const;
>
>       double*
>       values() const;
>   };
>
> Boost.Python extension:
>
>   #include <boost/python.hpp>
>
>   boost::python::list
>   values_as_list(yours const& self)
>   {
>     boost::python::list result;
>     for(unsigned i=0;i<self.number_of_values();i++) {
>       result.append(self.values()[i]);
>     }
>     return result;
>   }
>
>   BOOST_PYTHON_MODULE(your_choice)
>   {
>     using namespace boost::python;
>     class_<yours>("yours")
>       def("values", values_as_list)
>     ;
>   }
>
> Cheers,
>         Ralf
>
>
>
>
> __________________________________
> Yahoo! FareChase: Search multiple travel sites in one click.
> http://farechase.yahoo.com
> _______________________________________________
> C++-sig mailing list
> C++-sig at python.org
> http://mail.python.org/mailman/listinfo/c++-sig
>



More information about the Cplusplus-sig mailing list