Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost python method calls with reference arguments

I'm trying to do a simple call by reference from python into a C++ class method.

My C++ code looks like:

class Foo {
protected:
  int _internalVal;
public:
  Foo() : _internalVal(5){}
  void getVal(int& val_io) {val_io = _internalVal;}
  void getValDoesNothing(int val_io) {val_io = _internalVal;}
}

My boost wrapper code which compiles fine is:

BOOST_PYTHON_MODULE(libBar) {
  boost::python::class_<Foo>("Foo")
    .def("getVal", &Foo::getVal)
    .def("getValDoesNothing", &Foo::getValDoesNothing);
}

However when I make the following python calls:

In [1]: import libBar

In [2]: f = libBar.Foo()

In [3]: f
Out[3]: <libBar.Foo at 0x2b483c0>

In [4]: val = int()

In [5]: #next command is just to check function signature type

In [6]: f.getValDoesNothing(val)

In [7]: f.getVal(val)
---------------------------------------------------------------------------
ArgumentError                             Traceback (most recent call last)
<ipython-input-5-531e4cea97c2> in <module>()
----> 1 f.getVal(val)

ArgumentError: Python argument types in
    Foo.getVal(Foo, int)
did not match C++ signature:
    getVal(Foo {lvalue}, int {lvalue})

I'm working with a library I don't control so changing getVal to return the value isn't an option.

Is there any way to make the last Python command work?

I'll even take a fix that doesn't change the Python variable but still allows the function call.

like image 1000
Ramón J Romero y Vigil Avatar asked Dec 05 '12 16:12

Ramón J Romero y Vigil


2 Answers

What you are trying to achieve is not valid in Python. Integers are immutable, so you can't simple call a function and hope it's going to change its content.

Since you are working with a library you don't control and changing getVal to return the value isn't an option, you can create an wrapper like that:

int getVal(Foo &foo) {
    int val_io;
    foo.getVal(val_io);
    return val_io;
};

BOOST_PYTHON_MODULE(libBar) {
    boost::python::def("getVal", getVal);
    ...
}

and then use in this way:

In [1]: import libBar

In [2]: f = libBar.Foo()

In [3]: f
Out[3]: <libBar.Foo at 0x2b483c0

In [3]: libBar.getVal(f)
Out[3]: 5
like image 51
Hugo Corrá Avatar answered Sep 28 '22 01:09

Hugo Corrá


I use a numpy array with a single element to accomplish what you are trying. Here is the class that I wrap (in my case, implemented with an int for MsgBuf) in the file ipc-messenger.h:

template <class MsgBuf>
class IPCMessenger {
public:
    IPCMessenger(const char* name);
    virtual ~IPCMessenger();
    virtual int Notify(const MsgBuf& msg, unsigned int priority = 0);
    int Receive(MsgBuf& msg, unsigned int* priority = nullptr);
    int Terminate();
private:
    boost::interprocess::message_queue _mq; /// boost IPC Message Queue.
    std::string _queue_name;                /// application defined queue name
};

Here is the wrapper class in ipc-messenger_py.cpp

#include <boost/python.hpp>
#include "ipc-messenger.h"
#include <boost/python/numpy.hpp>

using namespace boost::python;
using namespace std;
namespace np = boost::python::numpy;

class IPCMessengerPy : public IPCMessenger<int> {
public:
    IPCMessengerPy(const char* name) : IPCMessenger(name) {}
    ~IPCMessengerPy() {}
    int Receive(np::ndarray & arr) {
        int ret = 0;
        int* p = (long *)arr.get_data();
        return IPCMessenger::Receive(*p);
    }
    int Notify(np::ndarray& arr) {
        int p = (long *)arr.get_data();
        return IPCMessenger::Notify(*p);
    }
};

BOOST_PYTHON_MODULE(ipcmessengerpy)
{
    Py_Initialize();
    np::initialize();

    class_<IPCMessengerPy, boost::noncopyable>("ipcmessenger", init<const char*>())
        .def( "receive", &IPCMessengerPy::Receive )
        .def( "send", &IPCMessengerPy::Notify )
        ;
}

This can be called from python as follows:

import ipcmessengerpy
import numpy as np
import time

# run this script in background, and then run ipc-master.py

q = ipcmessengerpy.ipcmessenger("QueueName")
buf = np.zeros([1], dtype = np.int32)

for c in range(0, 10):
    q.receive(buf)
    print "Received ", buf[0], " on q"
like image 35
DAmann Avatar answered Sep 28 '22 00:09

DAmann