boost python method calls with reference arguments
Asked Answered
B

3

6

While trying to do a simple call by reference from python into a C++ class method:

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

It is possible to compile the boost wrapper code:

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

However when executing the functionality in python an error occurs:

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 C++ 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.

Bronwyn answered 5/12, 2012 at 16:58 Comment(0)
R
6

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
Requital answered 5/12, 2012 at 19:4 Comment(0)
G
1

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"
Gratian answered 23/5, 2018 at 18:25 Comment(0)
P
-1

In getValDoesNothing you're passing in an int. In getVal you're passing a reference to an int.

I believe this is the source of your problem.

Proclaim answered 5/12, 2012 at 17:11 Comment(1)
I know I'm passing in a reference. My question is how do I do that?Tarweed

© 2022 - 2024 — McMap. All rights reserved.