Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]: Pass by reference does copy #4417

Open
3 tasks done
ManuelSchneid3r opened this issue Dec 23, 2022 · 2 comments
Open
3 tasks done

[BUG]: Pass by reference does copy #4417

ManuelSchneid3r opened this issue Dec 23, 2022 · 2 comments
Labels
triage New bug, unverified

Comments

@ManuelSchneid3r
Copy link
Contributor

ManuelSchneid3r commented Dec 23, 2022

Required prerequisites

What version (or hash if on master) of pybind11 are you using?

2.10.2

Problem description

The docs state that passing arguments as reference and pointer should work, but actually, while pointers works well, references dont. They end up beeing copied, which is a blocker if the type is not copy constructible.

class PyPlugin
{
public:
    virtual void takeThisAndModifyR(StandardItem &item) {};
    virtual void takeThisAndModifyP(StandardItem *item) {};
};
class PyPluginTrampoline : PyPlugin
{
public:
    using PyPlugin::PyPlugin;  // Inherit the constructors
    void takeThisAndModifyR(StandardItem &item) override  { PYBIND11_OVERRIDE(void, PyPlugin, takeThisAndModifyR, item); }
    void takeThisAndModifyP(StandardItem *item) override { PYBIND11_OVERRIDE(void, PyPlugin, takeThisAndModifyP, item); }
};

Then in a pyhton plugin I do:

class Plugin(Plugin):
  …
    def takeThisAndModifyR(self, item):
        item.id = "takeThisAndModifyR";

    def takeThisAndModifyP(self, item):
        item.id = "takeThisAndModifyP";
…

In the app i do

auto item = StandardItem("id");
INFO << item.id();
p->takeThisAndModifyP(&item);
INFO << item.id();
p->takeThisAndModifyR(item);
INFO << item.id();

Finally this prints:

10:47:43 [info:python] id
10:47:43 [info:python] takeThisAndModifyP
10:47:43 [warn:albert] [17ms] Failed loading 'find': return_value_policy = copy, but type albert::StandardItem is non-copyable!

Of course I exposed the item class. Left it out for readability. Obviously pybind does not like references. Is this a bug or a known limitation and I should rather look for a workaround?

Related:

Reproducible example code

No response

Is this a regression? Put the last known working version here if it is.

Not a regression

@ManuelSchneid3r ManuelSchneid3r added the triage New bug, unverified label Dec 23, 2022
@Iximiel
Copy link

Iximiel commented May 26, 2023

Hello, I have a problem that i think it is related to the topic of this issue:

I want to have a two way communication to and from python and what I come out is using a helper class in C++ with its python interface that should do that.

So I tried to pass by reference to a python function loaded in the embedded interpreter, and what python sees its a copy of my C++ object, so calling methods in python that should modify it do not affect the C++ instatiated object.

   mylib::SimpleIO mytest;
   auto fn = py_module.attr(funcName);
   //This will NOT modify the data in mytest
   fn(mytest, setValue).cast<py::tuple>();

But if i cast the pointer of the C++ object with py::cast(&myobject) i can pass it (the object, not the casted pointer) to the python function and the methods in python affect my C++ object

   mylib::SimpleIO mytest;
   auto fn = py_module.attr(funcName);
   auto obj = py::cast(&mytest);
   //This will modify the data in mytest
   fn(mytest, setValue).cast<py::tuple>();

I know I'm not clear with words: I have set up a small gist with the example of this. I'm using pybind v2.10.4, all the details are within the CMakeLists.txt in the gist

My question is: is this a wanted behaviour?

@ManuelSchneid3r
Copy link
Contributor Author

People having to get stuff done could redirect to a function casting the reference to a pointer. works for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
triage New bug, unverified
Projects
None yet
Development

No branches or pull requests

2 participants