Hello,
I suggest to avoid eval, in main()
instead, after "import test" in main():
funcObj = global["test"].attr("test") // assuming your global["test"] is a
successfully imported module
resultObj = funcObj()
then check resultObj:
as well as extract-and-check, you can query repr/str, and is_none(), and
check attributes __class__ and __class__.__name__
in boost-python say someObject.attr("attributeNameHere")
Best
Lewis
Bayforest Technologies Limited
48 Dover St, Mayfair
London W1S 4FF
work1: +44 203 968 5167
email: [email protected]
web: https://bayforest.ai/
On Tue, May 12, 2020 at 5:00 PM <[email protected]> wrote:
> Send Cplusplus-sig mailing list submissions to
> [email protected]
>
> To subscribe or unsubscribe via the World Wide Web, visit
> https://mail.python.org/mailman/listinfo/cplusplus-sig
> or, via email, send a message with subject or body 'help' to
> [email protected]
>
> You can reach the person managing the list at
> [email protected]
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Cplusplus-sig digest..."
>
>
> Today's Topics:
>
> 1. Beginner - How to extract a Python Class that inherits from
> C++ Class (Carlos Duran)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Tue, 12 May 2020 12:11:36 +0200
> From: Carlos Duran <[email protected]>
> To: "cplusplus-sig" <[email protected]>
> Subject: [C++-sig] Beginner - How to extract a Python Class that
> inherits from C++ Class
> Message-ID:
> <[email protected]>
> Content-Type: text/plain; charset="utf-8"
>
> 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 <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@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@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 <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-0001.html
> >
>
> ------------------------------
>
> Subject: Digest Footer
>
> _______________________________________________
> Cplusplus-sig mailing list
> [email protected]
> https://mail.python.org/mailman/listinfo/cplusplus-sig
>
>
> ------------------------------
>
> End of Cplusplus-sig Digest, Vol 125, Issue 3
> *********************************************
>
--
Bayforest Capital Limited ( Bayforest®) is an Appointed Representative of
G10 Capital Limited. G10 Capital Limited is authorised and regulated by the
Financial Conduct Authority, registration number 648953.
The
information
contained in this transmission may contain privileged and
confidential
information. It is intended only for the
use of the person(s)
named above. If you
are not the intended recipient, you are hereby
notified that any review,
dissemination, distribution or duplication of
this communication is strictly
prohibited. This communication is for
information purposes only and should not be regarded as an offer to sell or
as
a solicitation of an offer to buy any financial product, an official
confirmation
of any transaction, or as an official statement of Bayforest
Capital Limited
and Bayforest Technologies Limited. If
you are not the
intended recipient, please contact the sender by replying to
this e-mail
and destroy all copies of this e-mail (and any attachment(s)) from
your
system. To reply to our e-mail
administrator directly, please send an
e-mail to [email protected] <mailto:[email protected]>.
_______________________________________________
Cplusplus-sig mailing list
[email protected]
https://mail.python.org/mailman/listinfo/cplusplus-sig