[C++-sig] Beginner - How to extract a Python Class that inherits from C++ Class

Carlos Duran carlos_wurt at zohomail.eu
Tue May 12 06:11:36 EDT 2020


Hi,



I was being researching the Boost.Python library, this archive and stackoverflow but I couldn't find the correct answer. 



I tried the following:
-First: I created the C++ Class.
--------------------------------------------------------------------------

user at host ~/ProjectFolder: cat lib/DemiComponent.hpp 

#pragma once

#include <boost/python.hpp>



namespace DemiWu

{

    class Component

    {

        public:

        Component(){};

        virtual ~Component(){};

        virtual void on_create(){};

    };



    class ComponentWrap : public Component, public boost::python::wrapper<Component>

    {

        public:

        void empty(){}

        virtual void on_create()

        {

            this->get_override("on_create")();

        }

    };



    void import_component()

    {

        using namespace DemiWu;

         using namespace boost::python;

        class_<ComponentWrap, boost::noncopyable>("Component")

            .def("on_create", &Component::on_create, &ComponentWrap::empty);



    };

};

--------------------------------------------------------------------------



-Second: I exported to Python

--------------------------------------------------------------------------

user at host ~/ProjectFolder: cat lib/DemiWu.hpp 

#pragma once

#include <boost/python.hpp>

#include <DemiComponent.hpp>



BOOST_PYTHON_MODULE(DemiWu)

{

    DemiWu::import_component();

}

--------------------------------------------------------------------------

 cat lib/DemiWu.cpp 

#include <DemiWu.hpp>

---------------------------



-Third: I used Meson to compile, but I think that is not important. The module works on python3 interpreter.

--------------------------------------------------------------------------



-Fourth: I wrote a Python Class that inherit form the C++ Class DemiWu::Component.

--------------------------------------------------------------------------

user at host ~/ProjectFolder: cat build/test.py 

import DemiWu



class test(DemiWu.Component):

    def __init__(self):

        self.x = 10;

        self.y = 20;



    def on_create(self):

        print("(",self.x, ",",self.y,")", sep="")

        self.x = self.x + 1

        self.y = self.y + 1

--------------------------------------------------------------------------



Fifth: I write a program that extracts the test Python Class.

--------------------------------------------------------------------------

user at host ~/ProjectFolder: cat src/DemiWu.cpp 

#include <boost/python.hpp>

#include <DemiComponent.hpp>

#include <DemiWu.hpp>

#include <iostream>

using namespace boost::python;

using namespace DemiWu;



int main()

{

    PyImport_AppendInittab("DemiWu", PyInit_DemiWu);

    Py_Initialize();

    object main = import("__main__");

        object global = main.attr("__dict__");

    PySys_SetPath(L".");

    global["test"] = import("test");

    object obj = eval("test.test()", global);

    extract<Component*> ex(obj);

    if(ex.check()){

         Component* b=ex(); 

         b->on_create();  

         std::cout << "SUCCESS\n";

         return 0;

    } else {

        std::cout << "FAIL\n";

        return 1;

    }

}

--------------------------------------------------------------------------



Sixth: Compile the program successfully.

--------------------------------------------------------------------------



Seventh: Execute and... the output is always "FAIL".

--------------------------------------------------------------------------



How can I fix it? What did I miss?



Thank you for your attention,



Carlos



PS: Sorry for the format of the mail, but I can't express on English well.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20200512/ef615230/attachment.html>


More information about the Cplusplus-sig mailing list