[C++-sig] Cplusplus-sig Digest, Vol 125, Issue 3

Carlos Duran carlos_wurt at zohomail.eu
Wed May 13 11:50:16 EDT 2020


Hello again,



I rewrite part of the code, but I didn't find my error.

My new code is:

cat ../src/DemiWu.cpp 

#include <boost/python.hpp>



#include <DemiComponent.hpp>

#include <DemiWu.hpp>

#include <iostream>

#include <string>



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<std::string> exclass(funcObj.attr("__class__").attr("__name__"));

      if(exclass.check())

            std::cout << "test.test.__class__.__name__: " << exclass() << "\n"; // exclass() == "class"

      extract<std::string> exclass2(resultObj.attr("__class__").attr("__name__"));

      if(exclass2.check())

            std::cout << "test.test().__class__.__name__: " << exclass2() << "\n"; // exclass2() == "test"

      extract<std::string> 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<Component*> 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20200513/e8d19d13/attachment.html>


More information about the Cplusplus-sig mailing list