1

Using Boost::Python, the normal mechanism for wrapping functions works correctly with C++ functions returning void. Unfortunately, the normal mechanism also has limitations, specifically with regards to the function arity it supports. So I need to use boost::python::raw_function to wrap my function, but it doesn't compile when my function returns void. Here's a simple test case:

#include <boost/python.hpp>
#include <boost/python/raw_function.hpp>

void entry_point(boost::python::tuple args, boost::python::dict kwargs) {  }

BOOST_PYTHON_MODULE(module)
{
  boost::python::def("entry_point", boost::python::raw_function(&entry_point));
}

Which gives the error:

/usr/local/include/boost/python/raw_function.hpp: In member function ‘PyObject* boost::python::detail::raw_dispatcher::operator()(PyObject*, PyObject*) [with F = void (*)(boost::python::tuple, boost::python::dict)]’:

/usr/local/include/boost/python/object/py_function.hpp:94: instantiated from ‘PyObject* boost::python::objects::full_py_function_impl::operator()(PyObject*, PyObject*) [with Caller = boost::python::detail::raw_dispatcher, Sig = boost::mpl::vector1]’

void.cpp:8: instantiated from here

/usr/local/include/boost/python/raw_function.hpp:36: error: invalid use of void expression

For the moment, I can work around this by having my function return a dummy value, but that's somewhat unsatisfying. Have other people run into this problem?

2
  • You can get around the arity issues with the normal method by #define-ing the BOOST_PYTHON_MAX_ARITY macro. Commented Nov 8, 2010 at 19:32
  • Thanks for pointing this out. I'm doing this now instead of raw_function, it turns out to be faster on my machine. Commented Nov 29, 2010 at 5:57

1 Answer 1

2

I think this is the way that raw_function() works. It expects your function to return a Python object.

In Python the closest thing you will get to a function returning void is a function returning None. I think that approach would be best (and not even that ugly) in your case:

#include <boost/python.hpp>
#include <boost/python/raw_function.hpp>

using namespace boost::python;

namespace
{
  object entry_point(tuple args, dict kwargs) 
  {  
    return object();
  } 
}

BOOST_PYTHON_MODULE(foo)
{
  def("entry_point", raw_function(&entry_point));
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this is exactly what I was hoping to see.

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.