[C++-sig] Beginner - How to extract a Python Class that inherits from C++ Class
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@host ~/ProjectFolder: cat lib/DemiComponent.hpp #pragma once #include namespace DemiWu { class Component { public: Component(){}; virtual ~Component(){}; virtual void on_create(){}; }; class ComponentWrap : public Component, public boost::python::wrapper { public: void empty(){} virtual void on_create() { this->get_override("on_create")(); } }; void import_component() { using namespace DemiWu; using namespace boost::python; class_("Component") .def("on_create", &Component::on_create, &ComponentWrap::empty); }; }; -- -Second: I exported to Python -- user@host ~/ProjectFolder: cat lib/DemiWu.hpp #pragma once #include #include BOOST_PYTHON_MODULE(DemiWu) { DemiWu::import_component(); } -- cat lib/DemiWu.cpp #include --- -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@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@host ~/ProjectFolder: cat src/DemiWu.cpp #include #include #include #include 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 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.___ Cplusplus-sig mailing list Cplusplus-sig@python.org https://mail.python.org/mailman/listinfo/cplusplus-sig
Re: [C++-sig] Cplusplus-sig Digest, Vol 125, Issue 3
Hello again, I rewrite part of the code, but I didn't find my error. My new code is: cat ../src/DemiWu.cpp #include #include #include #include #include using namespace boost::python; using namespace DemiWu; #if PY_MAJOR_VERSION >= 3 # define INIT_MODULE PyInit_DemiWu extern "C" PyObject* INIT_MODULE(); #else # define INIT_MODULE initDemiwu extern "C" void INIT_MODULE(); #endif int main() { PyImport_AppendInittab((char*)"DemiWu", INIT_MODULE); Py_Initialize(); object main = import("__main__"); object global = main.attr("__dict__"); PySys_SetPath(L"."); global["test"] = import("test"); // How can I check the import? object funcObj = global["test"].attr("test"); std::cout << std::boolalpha; std::cout << funcObj.is_none() << "\n"; // funcObj.is_none() == false object resultObj = funcObj(); std::cout << resultObj.is_none() << "\n"; // resultObj.is_none() == false // I suppose that in "real" code I need to check every time I use attr method. extract exclass(funcObj.attr("__class__").attr("__name__")); if(exclass.check()) std::cout << "test.test.__class__.__name__: " << exclass() << "\n"; // exclass() == "class" extract exclass2(resultObj.attr("__class__").attr("__name__")); if(exclass2.check()) std::cout << "test.test().__class__.__name__: " << exclass2() << "\n"; // exclass2() == "test" extract exbase(funcObj.attr("__base__").attr("__name__")); if(exbase.check()) std::cout << "test.test.__base__.__name__: " << exbase() << "\n"; // exbase() == "Component" // It seems that resultObj is correct, I think that I am using extract<> function wrong. extract ex(resultObj); if(ex.check()){ Component * const b=ex(); b->on_create(); std::cout << "SUCCESS\n"; return 0; } else { std::cout << "FAIL\n"; // And again the program jumps here. return 1; } } And the execution: ./DemiWu false false test.test.__class__.__name__: class test.test().__class__.__name__: test test.test.__base__.__name__: Component FAIL Thank you for your response Carlos___ Cplusplus-sig mailing list Cplusplus-sig@python.org https://mail.python.org/mailman/listinfo/cplusplus-sig
Re: [C++-sig] Cplusplus-sig Digest, Vol 125, Issue 5
Hi, I tried that solutions but they didn't work, furthermore, I thought that polymorphisim only works on pointers. I also tried changing to reference(Component&) and adding a HeldType in the import module: class_*/, boost::noncopyable>("Component", init<>()) but all of this is futile. Thank you Carlos___ Cplusplus-sig mailing list Cplusplus-sig@python.org https://mail.python.org/mailman/listinfo/cplusplus-sig
Re: [C++-sig] Beginner - How to extract a Python Class that inherits from C++ Class
Hi, I find the solution, first of all I need to add the initializer on the C++ class import: class_, boost::noncopyable>("Component", init<>()) // I additionally added a Holder, but is not necessary. Then I add the base class initializer on the python class: class Derived(DemiWu.Component): def __init__(self): DemiWu.Component.__init__(self) I only find a problem, if a python class call the print function it doesn't work. Thanks, Carlos___ Cplusplus-sig mailing list Cplusplus-sig@python.org https://mail.python.org/mailman/listinfo/cplusplus-sig
[C++-sig] Beginner How to debug a python class extracted in C++
Hi, I recently started a Boost Python project, https://mail.python.org/pipermail/cplusplus-sig/2020-May/017585.html but I need to debug my python classes extracted in C++ in the main program. The only referece that I find to this topic is in https://www.boost.org/doc/libs/1_73_0/libs/python/doc/html/faq/how_do_i_debug_my_python_extensi.html How can I solve this? I am only using the gdb debugger. Thank you for your attention, Carlos___ Cplusplus-sig mailing list Cplusplus-sig@python.org https://mail.python.org/mailman/listinfo/cplusplus-sig