Bryan Catanzaro
Bryan Catanzaro

Reputation: 269

Boost::Python raw_function returning void

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?

Upvotes: 1

Views: 1496

Answers (1)

Arlaharen
Arlaharen

Reputation: 3145

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));
}

Upvotes: 2

Related Questions