Search code examples
pythonboostpython-2.7boost-pythoncpython

Getting argument list in a Boost:Python function


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'


Solution

  • 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)
    >>>