1

In CPython we can get the argument list of a function by following methods. function name is 'aMethod'

import inspect
inspect.getargspec(aMethod)

or

aMethod.func_code.co_varnames

How can I achieve the same thing for a Boost:Python function? I get the following error when I use these methods in for those.

for the first method TypeError: is not a Python function

for the second method AttributeError: 'aMethod' object has no attribute 'func_code'

2 Answers 2

1

When accessing a Python attribute on a boost::python::object, use the attr member function. For example:

aMethod.func_code.co_varnames

would become

aMethod.attr("func_code").attr("co_varnames")

Here is a complete example.

#include <iostream>
#include <vector>

#include <boost/foreach.hpp>
#include <boost/python.hpp>
#include <boost/python/stl_iterator.hpp>

void print_varnames(boost::python::object fn)
{
  namespace python = boost::python;
  typedef python::stl_input_iterator<std::string> iterator;

  std::vector<std::string> var_names(
    iterator(fn.attr("func_code").attr("co_varnames")),
    iterator());

  BOOST_FOREACH(const std::string& varname, var_names)
    std::cout << varname << std::endl;
}

BOOST_PYTHON_MODULE(example)
{
  def("print_varnames", &print_varnames);
}

Usage:

>>> def test1(a,b,c): pass
... 
>>> def test2(spam, eggs): pass
... 
>>> def test3(): pass
... 
>>> from example import print_varnames
>>> print_varnames(test1)
a
b
c
>>> print_varnames(test2)
spam
eggs
>>> print_varnames(test3)
>>> 
4
  • error C2039: 'stl_input_iterator' : is not a member of 'boost::python'. I get this error while building. I'm using Visual Studio 2010 on Windows 7 32-bit, Python 2.7 and Boost 1.47.
    – maheshakya
    Commented May 19, 2013 at 13:50
  • after importing stl_iterator it works, but gives the same error while getting the argument list using that method.
    – maheshakya
    Commented May 19, 2013 at 15:28
  • Ok. I figured it out. there was an issue in the function in which I used to get arguments. The problem in this works only when Python is is run on command prompt. But anyways, it works now :D. Thnaks
    – maheshakya
    Commented May 19, 2013 at 15:39
  • @maheshakya: I've made the edit since there is no harm in it. As of 2 years ago, the convenience header file on the trunk (r72746) includes stl_iterator. However, it does not appear to be merged into the release branches. Commented May 20, 2013 at 12:48
0

Careful: This approach will print out all variables in a function, not just its parameters. To wit:

def test4(a, b):
    c = a + b

>>> test4.__code__.co_varnames
('a', 'b', 'c')

If you really need just the function parameters, just use the inspect module:

// given f as callable boost::python::object
auto inspect = python::import("inspect");
auto argspec = inspect.attr("getargspec")(f);
// returns args in argspec[0] (and varargs in [1], keywords in [2], and defaults in [3]).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.