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

Martin Dai blast2001 at 21cn.com
Thu Apr 28 03:47:35 CEST 2005


Hi  Volodya,

    Thanks for your great help.  I have tried those two files(those are very
visual  :).
But  just as you mentioned:      is  it  possible  to  register  the
corresponding  Python class  for  custom C++  class   by   an  explicit
way
before  Invoke  PyRun_String?

Best Regards,
Martin


"Vladimir Prus" <ghost at cs.msu.su> ???? news:d4neu4$qas$1 at sea.gmane.org...
> 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