[C++-sig] Re: Pass C++ object to python issue

Vladimir Prus ghost at cs.msu.su
Wed Apr 27 09:32:42 CEST 2005


Martin Dai wrote:

> Hi  everyone,
> 
>     Now  I  have solved the pass variable when the variable is simple
>     type,
> but when I bind the C++ class object into the dictionary,  it compile
> successfully,  it will abort once execute it.

With what error message?

> The below is the c++ source code:
> 
>     if (PyImport_AppendInittab("embedded_hello", initembedded_hello) ==
>     -1)

Please always provide a complete program.

>     main_namespace["AppBase"] = python::object(python::ptr(pBase));
> //abort here
> 
>     python::handle<> result(
>         PyRun_String(
>             "from embedded_hello import *        \n"
>             "print AppBase.i \n"
>         Py_file_input, main_namespace.ptr(), main_namespace.ptr())
>         );

I've tried to reproduce this, and got:

    http://zigzag.cs.msu.su/~ghost/e3.cpp

that works fine. However, when I move the 

    main_namespace["context"] = ptr(b);

above call to PyRun_String (as is done in your example), 

    http://zigzag.cs.msu.su/~ghost/e3f.cpp

I get this:

   TypeError: No Python class registered for C++ class B

which looks quite reasonable. Python has no idea that class 'B' even exists,
but you're trying to add instance of that class. I wonder if it's possible
to automatically register Python class in this case, though.

- Volodya





More information about the Cplusplus-sig mailing list