Opened 12 years ago

Last modified 12 years ago

#4726 new Bugs

Bugs when using named args.

Reported by: Albin Thoren <thorena@…> Owned by: Dave Abrahams
Milestone: To Be Determined Component: python USE GITHUB
Version: Boost 1.44.0 Severity: Problem
Keywords: Cc:

Description

There seem to be issues when naming function arguments. Same thing happens for class_ defs and init<> functions.

Using non-named args f(1,2,3) works fine in all cases below but using named args f(1,2,z=3) works or fails depending on boost python definition.

The first two of the following defs fail when using named args. The first one crashes python and the second raises "did not match C++ signature" exception. The third version works as expected.

#include <boost/python/module.hpp>
#include <boost/python/def.hpp>

int f(int x, int y, int z) { return x*y*z;}

BOOST_PYTHON_MODULE(_pylib)
{
        namespace bp = boost::python;
        // Bug
        bp::def("bug1", &f, ("x", "y", bp::arg("z")));
        bp::def("bug2", &f, ("x", "y", "z"));
        // Works        
        bp::def("works_fine", &f, (bp::arg("x"), bp::arg("y"), bp::arg("z")));
}

Running bug1(x=1, y=2, z=3) crashes python and bug2(1, 2, z=3) gives an ArgumentError

Boost.Python.ArgumentError: Python argument types in
    _pylib.bug2(int, int)
did not match C++ signature:
    bug2(int, int, int)

Summary of errors:

from _pylib import *

works_fine(1,2,3) # OK
bug1(1,2,3) # OK
bug2(1,2,3) # OK

works_fine(1, 2, z=3) # OK
bug1(1, 2, z=3) # OK
bug2(1, 2, z=3) # Error: did not match C++ signature


works_fine(x=1, y=2, z=3) # OK
bug1(x=1, y=2, z=3) # Error: Python crashes and exits abnormally
bug2(x=1, y=2, z=3) # Error: did not match C++ signature

I'm using:

  • boost 1.44. BoostPro Windows pre-built binary for VC++ 2008.
  • Python 2.6
    • sys.version = '2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)]')
  • Microsoft VC++ 2008

Change History (2)

comment:1 by Dave Abrahams, 12 years ago

It's probably a bug that you're able to crash Python, but the two examples you are reporting as bugs don't mean what you think they do:

        bp::def("bug1", &f, ("x", "y", bp::arg("z")));
        bp::def("bug2", &f, ("x", "y", "z"));

In particular, because the comma operator associates from left-to-right, what you've written there is equivalent to:

        bp::def("bug1", &f, bp::arg("z"));
        bp::def("bug2", &f, "z");

comment:2 by Albin Thoren <thorena@…>, 12 years ago

Yes, you're right. I should have seen that.

The crash then only occurs with unintended use of bp::arg. That is, if the number of function arguments does not match the number of bp::arg(s). That is:

bp::def("bug", &f, (bp::arg("y"), bp::arg("z")));

and

bug(anything=1, y=2, z=3)

also crashes python.

I assume any declaration with mismatching num of args should be illegal/undefined behaviour. Would it be possible to add a static assert to verify that the number or function arguments == the number of bp::arg(s)?

Note: See TracTickets for help on using tickets.