pybind / pybind/pybind11

cannot exchange objects from python <-> C++. The object is from a third library

Open
#4,358 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
18k
Forks
2.3k
Avg merge
5d 17h
Merged PRs (30d)
10

Description

Discussed in https://github.com/pybind/pybind11/discussions/4357

Originally posted by saroj31 November 23, 2022
I have a very basic question. I say basic because I am new to pybind11 and this is my first attempt to use the library to make python bindings for my C++ code. So here I am using a C++ class which internally uses a Realsense class. When I try to contruct a constructor for it then it gives me a TypeError.
Can someone explain me what I am doing wrong in PYbind11 persepctive? Definitely I can see that there is a type mismatch here but Still I beleive this is a general problem which many of you would have faced. I want to solve this puzzle. Read all documentation but the cutom type caster example also uses an internal pybnd11 template internally. I don't think mine is a similar thing. I want to somhow pass the info from C++ side to python side and vice versa. If you can direct me to some right examples or documentation then it will be awesome.

My C++ Class in Devicemanager.h/cpp :

include <librealsense2/rs.hpp>
class DeviceManager {

	public:
		explicit DeviceManager(rs2::context& ctx);
		td::size_t getNumOfConnectedDevices();
}

My Binder.cpp code

#include <pybind11/pybind11.h>
#include "rsWrap/DeviceManager.h"
#include <librealsense2/rs.hpp>
#include <librealsense2/hpp/rs_context.hpp>


#define STRINGIFY(x) #x
#define MACRO_STRINGIFY(x) STRINGIFY(x)

int add(int i, int j) {
    return i + j;
}

namespace py = pybind11;
using namespace RealSenseWraLib;

PYBIND11_MODULE(qco_py_wrapper, m) {


    py::class_<DeviceManager>(m, "DeviceManager")
            .def(py::init<rs2::context &>())
            .def("getNumOfConnectedDevices", &DeviceManager::getNumOfConnectedDevices);

    m.def("add", &add, R"pbdoc(
            Add two numbers
            Some other explanation about the add function.
        )pbdoc");

}

The python Code that I want to make work is:

import pyrealsense2 as rs
import qco_py_wrapper as qco

dmanager =  qco.DeviceManager(rs.context());
print(dmanager)

Error That I get is this:

Traceback (most recent call last):
  File " *<my Directory>*/build/python_wrapper/test.py", line 5, in <module>
    dmanager =  qco.DeviceManager(rs.context());
TypeError: __init__(): incompatible constructor arguments. The following argument types are supported:
    1. qco_py_wrapper.DeviceManager(arg0: rs2::context)

Invoked with: <pyrealsense2.pyrealsense2.context object at 0x7f3cc3c16d30>

When I do a help on qco_py_wrapper then the following appears:

Help(qco_py_wrapper):
`Help on module qco_py_wrapper:

NAME
    qco_py_wrapper

CLASSES
    pybind11_builtins.pybind11_object(builtins.object)
        DeviceManager
    
    class DeviceManager(pybind11_builtins.pybind11_object)
     |  Method resolution order:
     |      DeviceManager
     |      pybind11_builtins.pybind11_object
     |      builtins.object
     |  
     |  Methods defined here:
     |  
     |  __init__(...)
     |      __init__(self: qco_py_wrapper.DeviceManager, arg0: rs2::context) -> None
     |  
     |  getNumOfConnectedDevices(...)
     |      getNumOfConnectedDevices(self: qco_py_wrapper.DeviceManager) -> int
     |  
     |  ----------------------------------------------------------------------
     |  Static methods inherited from pybind11_builtins.pybind11_object:
     |  
     |  __new__(*args, **kwargs) from pybind11_builtins.pybind11_type
     |      Create and return a new object.  See help(type) for accurate signature.

`

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the DeviceManager.h/cpp constructor and Binder.cpp binding shown in the issue, then compare the rs.context object from pyrealsense2 with the exposed rs2::context type. Review the linked discussion and relevant pybind11 type-caster documentation; the work is complete when the required cross-module object exchange is clearly supported or its limitation is documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
api
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.