[C++-sig] Boost.Python.function.__signatures__

Ralf W. Grosse-Kunstleve rwgk at yahoo.com
Wed Jul 20 18:26:15 CEST 2005

--- David Abrahams <dave at boost-consulting.com> wrote:

> "Ralf W. Grosse-Kunstleve" <rwgk at yahoo.com> writes:
> > I know it needs more work (similar patch for member functions (?), tests on
> all
> > platforms, updated docs). I'll do this if I get positive feedback.
> a. I basically support the idea; haven't looked at your code.

The changes are very simple. In function.hpp I've just added a

  class list;

forward declaration in the boost::python namespace, along with a new
function::signatures() member function declaration.

In function.cpp I've moved your code for generating the list of strings with
the signatures into the new function::signatures() member function, almost
unchanged. I am calling this function from the old place (in
function::argument_error()). Finally I added the new function

    static PyObject* function_get_signatures(PyObject* op, void*)
        function* f = downcast<function>(op);
        return python::incref(f->signatures().ptr());


static PyGetSetDef function_getsetlist[] = {
    {"__signatures__", (getter)function_get_signatures, 0 },
    {NULL} /* Sentinel */

That's all.

> b. Wouldn't you want to get a sequence of sequences, where each
>    element is the type of an argument or return type?

I think it is an unneeded complication. I don't know where the extra level of
detail would be helpful. To get help the strings are just what you want. It is
easy enough to use split("(", 1) and split(",") in Python if more detail is
required, but I am having difficulties imagining a use case.

> c. Should you look for ways to get this integrated into the stuff that
>    Python normally puts on built-in Python functions, i.e. the stuff
>    that help(f) uses?  This is a vague thought.

I thought about this initially and looked in the pydoc and inspect sources. It
wasn't a match because Python doesn't have function overloading. inspect looks
at, e.g., func_defaults and expects exactly one answer (a tuple). However,
Boost.Python has multiple answers, one for each overload.

My plan is: 1. patch Boost.Python to provide the signature information, 2. try
to convince the Python folks to support __signatures__ in pydoc/inspect. I hope
1. is easy, I guess 2. will be much harder. In the meantime I'll give my users
a signatures() built-in that will work the same for regular Python functions
and Boost.Python functions. I need this soon because I'll be teaching at a
school in a four weeks. I don't want everybody to look at the wrapper sources
to figure out the signatures; and of course, as always, there is no time to
write documentation for everything. Easily accessible just-in-time help is the
best option, I believe.


Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 

More information about the Cplusplus-sig mailing list