[C++-sig] Re: function::argument_error / overloads & docstrings

Nikolay Mladenov nickm at sitius.com
Tue Nov 4 02:34:31 CET 2003


If you have the following module:
#include <boost/python.hpp>
#include <string>

struct Foo
{
    Foo(
        int a = 0
        , double b = 0
        , const std::string &n = std::string()
        ) : 
    a_(a)
        , b_(b)
        , n_(n)
    {}

    void set(int a=0, double b=0, const std::string &n=std::string()) 
    {
        a_ = a; 
        b_ = b;
        n_ = n;
    }

    void set(double b) 
    {
        b_ = b;
    }
    void set(int a=0) 
    {
        a_ = a; 
    }
    void set(const std::string &n=std::string()) 
    {
        n_ = n;
    }
 
private:
    int a_;
    double b_;
    std::string n_;
};


using namespace boost::python;
BOOST_PYTHON_MODULE(keywords)
{
   class_<Foo>("Foo" 
       , init<int
              , double
              , const std::string &
              >(
              (  arg("a") = 0
              , arg("b") = 0.0
              , arg("n") = std::string()
              ))
        )
      .def("set"
            , (void (Foo::*)(int , double , const std::string &)
)&Foo::set
            , (arg("self"),arg("a") = 0, arg("b") = 0.0, arg("n") =
std::string()) 
            , "Sets all the fields of self")
      .def("set"
            , (void (Foo::*)(int ) )&Foo::set
            , (arg("self"),arg("a")) 
            , " Sets the integer field of self")
      .def("set"
            , (void (Foo::*)(double ) )&Foo::set
            , (arg("self"),arg("b")) 
            , " Sets the float field of self")
      .def("set"
            , (void (Foo::*)(const std::string &) )&Foo::set
            , (arg("self"), arg("n") ) 
            , "Sets the string field of self" )
      ;

}
#include "module_tail.cpp"
////////////////////////////////////////


with what I currently have (cvs patched by me) I get:


>>> import keywords
>>> keywords.Foo(.1)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
Boost.Python.ArgumentError: Python argument types in
    Foo.__init__(Foo, float)
did not match C++ signature:
    __init__(struct _object *, int a=0, double b=0.0, class
_STL::basic_string<char,struct std::char_traits<char>,class
_STL::allocator<char> > n="")
>>> help(keywords.Foo.set)

Help on method set:

set(...) unbound keywords.Foo method
    set(struct Foo self, class _STL::basic_string<char,struct
std::char_traits<char>,class _STL::allocator<char> > n)
        Sets the string field of self
    set(struct Foo self, double b)
         Sets the float field of self
    set(struct Foo self, int a)
         Sets the integer field of self
    set(struct Foo self, int a=0, double b=0.0, class
_STL::basic_string<char,struct std::char_traits<char>,class
_STL::allocator<char> > n="")
        Sets all the fields of self
(END)
>>>



with the (almost)current cvs :


>>> import keywords
>>> keywords.Foo(.1)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: No to_python (by-value) converter found for C++ type: char
>>> help(keywords.Foo.set)
Help on method set:

set(...) unbound keywords.Foo method
    Sets the string field of self

I was also thinking of modifying the signature element to include
type_info *(instead of just type_info->name())
Then instead of outputing C++ signatures some kind of Python signatures
can be generated.
I think this would be better since the python programmer generally would
not know about the underlying CPP code.
My problem is that I am not exactly sure how should the python signature
look
(since types are not specified for python function parameters):

something like:
    set( [keywords.Foo] self, [int] a=0, [float] b=0.0, [str] n="")
may be?

Thoughts?

Nikolay





More information about the Cplusplus-sig mailing list