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

Nikolay Mladenov nickm at sitius.com
Fri Oct 31 18:00:20 CET 2003


I had to change function::argument_error because it was throwing
exception (MSVC6.5 built)

$ 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
266c266
<                 str(s[n].basename) + (s[n].lvalue ? " {lvalue}" : "")
---
>                 str(s[n].basename) + (s[n].lvalue ? str(" {lvalue}") : istr(""))



Then I played a bit and end up with this:

$ 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
>                 );



It formats the functions to include the arg names and the default arg
values if available.
Like that:

>>> kwd.Foo(.0)
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<c
har,struct std::char_traits<char>,class _STL::allocator<char> > n="")
>>> kwd.Bar(.0)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
Boost.Python.ArgumentError: Python argument types in
    Bar.__init__(Bar, float)
did not match C++ signature:
    __init__(struct _object *, int a=0, double b=0.0, struct Foo
n=<keywords.Foo
 object at 0x007B73A0>)
>>>


What do you think about it. Is it a useful patch.
I am thinking that something like that can be done for the document
strings
when there are function overloads. It is probably possible to print the
list of the
overloads with the corresponding docstrings instead of just printing one
docstring.


Nikolay Mladenov





More information about the Cplusplus-sig mailing list