Git Product home page Git Product logo

pybind-legacy-sdk's Introduction

Exposing a legacy C++ SDK to python with pybind

Background

Suppose you have a legacy C++ SDK that fires events via overridden virtual functions on an interface.

struct EventListener
{
    virtual void OnEvent(int code, const std::string &text) = 0;
};

You register a custom listener through a free standing function provided by the SDK.

void register_listener(EventListener *listener)

The SDK holds a reference to your listener for the life of the program (ignore ownership details for now)

Naive attempt to pybind the SDK over to python

We could pybind the interface and registration routing straight over to python like this

struct PyEventListener : public EventListener {
    void OnEvent(int code, std::string &text)
    {
        py::gil_scoped_acquire acquire;
        PYBIND11_OVERLOAD( ... );
    }
};

void register_pylistener(PyEventListener *listener) {
    register_listener(static_cast<EventListener*>(listener));
}


PYBIND11_MODULE(example, m)
{
    py::class_<EventListener, PyEventListener>(m, "EventListener")
        .def(py::init<>())
        .def("OnEvent", &EventListener::OnEvent);

    m.def("register_listener", &register_pylistener, "Registers a listener with the SDK");
}

On the python side, we can create a listener and register it with the SDK:

import example

class EventListener(example.EventListener):
    def OnEvent(self, code, text):
        print('Py code got an event : %d : %s' % (code, text))

listener  = EventListener()
example.register_listener(listener)
# do something that fires events

Unfortunately, this creates a fragile situation in the python world. The python consumer must be careful to keep the lister alive for as long as the SDK holds a reference to it This code above works. The code below SEGFAULTS:

example.register_listener(EventListener())
# do something that fires events

Solutions

Wrapper class + pybind's keep-alive policy

See the sdk-wrapper-class branch

Use pyobject

This suggestion was discussed in a gitter thread

Use shared_ptr (failure)

See the shared_ptr branch. This fixes the seg fault, but unfortunately, results in slicing off the python part of the event listener. Virtual dispatching to the python side does not work. More details are posted in the gitter thread.

pybind-legacy-sdk's People

Contributors

hhowe29 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.