[Tutor] Re: Embeding Python... (Please help !)

Derrick 'dman' Hudson dman@dman.ddts.net
Fri Jan 3 18:01:01 2003


--G4iJoqBmSsgzjUCe
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Fri, Jan 03, 2003 at 12:39:26PM -0500, Sebastien Auclair wrote:
| Our problem is so trivial, it's crazy that we can't find anything on the =
net
| that would help us.
|=20
| We are trying to use the function PyArg_Parse and others in order to simp=
ly
| get a correctly casted pointer to a Python Class instance.

In C or C++ the type will be known as "PyObject".

| That python class is in fact a subclass of a C++ class defining an
| interface. Our python file knows this interface class through SIP binding=
s.
|=20
| There are some examples of how we can get simple C type values (strings,
| int, float...) from Python types but nothing on pointers to class... We
| tried to use the O& option but it doesn't work.

| PyObject * v1 =3D PyObject_CallObject(func1,NULL);
|=20
| interface * interf =3D (interface*) v1;  // OF COURSE THIS DOESN'T WORK B=
UT
| YOU GET THE IDEA....

The problem here is you want to throw python away.  The python object
isn't _really_ a implementation of the interface.  It is just a
PyObject.  It just so happens that the contents of the PyObject make
it behave (in python) as an implementation of the interface.  You need
to use the python API to interact with and extract the lower-level
data from the PyObject.  For example, the interafce defines a method
named "update" you would call it with
    PyObject_CallMethod(v1, "update", NULL); // it returns Py_None

Now you want to use the "getValue" method on the interface to get the
(integer) value from the class instance.
    PyObject* presult =3D PyObject_CallObject(v1, "getValue", NULL);
    int result =3D (int) PyInt_AsLong( presult ) ;
    Py_DEREF(presult) ; presult =3D NULL ;


This might seem like it is overkill and too much effort -- after all,
you have the interface declared in C++, but since the object itself is
a python object all of its life cycle must be managed by python.  The
python interpreter is needed to enforce the Python, not C++, semantics
of the object.  The C (or C++) code for managing python objects will
always be much longer and more tedious than the equivalent python code
because the C (C++) compiler doesn't provide the same runtime services
for C (C++) objects that the python interpreter provides for python
objects..


NOTE: In this example code and in the code you posted there is no
      error checking.  If a python C-level function returns NULL that
      means an exception has occured.
      http://python.org/doc/current/api/exceptions.html

HTH,
-D

--=20
Windows, hmmm, does it come with a GUI interface that works or just
pretty blue screens?
=20
http://dman.ddts.net/~dman/

--G4iJoqBmSsgzjUCe
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iEYEARECAAYFAj4WFfMACgkQO8l8XBKTpRR07gCfTZ1QD0r9kweyRyJlxijJjJoU
FZsAnRUOVyrxwrw1kRB5MGSvefG/Rng6
=Yc8O
-----END PGP SIGNATURE-----

--G4iJoqBmSsgzjUCe--