[C++-sig] Re: function::argument_error / overloads & docstrings
Nikolay Mladenov
nickm at sitius.com
Thu Nov 6 15:23:26 CET 2003
I is right here:
Nikolay Mladenov wrote:
>
> 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)
> {}
> .....................................
> };
>
> 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()
> ))
> )
> .....................................
> }
> #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="")
>
And the patch is:
$ cvs diff function.cpp
Index: function.cpp
===================================================================
RCS file: /boost/boost/libs/python/src/object/function.cpp,v
retrieving revision 1.36
diff -r1.36 function.cpp
256c256,257
<
---
>
> bool f_has_keywords = f->m_arg_names.ptr()!= Py_None && PyTuple_Size(f->m_arg_names.ptr()) != 0;
263,267c264,281
< }
<
< formal.append(
< str(s[n].basename) + (s[n].lvalue ? " {lvalue}" : "")
< );
---
> }
>
> object arg(s[n].lvalue ? str(" {lvalue}") : str(""));
> if( f_has_keywords ){
> PyObject* kv = PyTuple_GET_ITEM(f->m_arg_names.ptr(), n-1);
> if(kv != Py_None)
> if(PyTuple_Size(kv) == 1)
> arg = " %s"% object(handle<>(incref(kv)));
> else{
> if(PyString_CheckExact(PyTuple_GET_ITEM(kv, 1)))
> arg = " %s=\"%s\""% object(handle<>(incref(kv)));
> else
> arg = " %s=%s"% object(handle<>(incref(kv)));
> }
> }
> formal.append(
> str(s[n].basename) + arg
> );
More information about the Cplusplus-sig
mailing list