[C++-SIG] Python calling C++ issues

Mark Hammond mhammond at skippinet.com.au
Tue Nov 23 08:34:46 CET 1999


The problem is that Python thinks it is giving you an "Object *", but
you are passing a "Spam *".  For the same object, an "Object *" is off
by the size of the virtual function pointer - an "Object *" doesnt
have a vfptr, but a "Spam *" does.

Change your code to:

static PyObject*
ext_Spam_Display(PyObject* self, PyObject* args)
{

	Object *obSpam;
	if (!PyArg_ParseTuple(args, "O", &obSpam))
		return NULL;

	Spam* spam = (Spam *)obSpam;
	Spam_Display(spam);

	return Py_BuildValue("");
}

Check in the debugger - the "Object *" and "Spam *" will be different,
even though they are the same object!

Mark.

-----Original Message-----
From: c++-sig-admin at python.org [mailto:c++-sig-admin at python.org]On
Behalf Of Ephi Dror
Sent: Tuesday, 23 November 1999 5:50
To: c++-sig at python.org
Subject: [C++-SIG] Python calling C++ issues



Hi All,

We are exploring the possibility of using Python with conjunction with
C++.

I recently posted the "Beginner Question" and would like to thank all
of you
for the great discussion and support we have received.

We have managed to make  great progress however we run into couple of
very
strange problems which I would like to bring up here and share them
with the group.
Any help in solving these issues will be highly appreciated and
beneficiary for all.
We also think that the attached example could be good starting point
for beginners to understand the concept of calling C++ objects from
Python.

The example contains a simple C++ class with two data members and we
have a simple python script which uses the class.

The two problems are:
1. The value of the first data member in the class is ok in the
constructor,
but in all the rest of the member functions it added 2 to the first
data
member.
2. When we call the destructor, It is executing the destructor, but
we get
"Segmentation fault" after it finished to execute the python script
that
calls the functions that call c++ functions.

Here is general information on:
1. We use Linux version 6.1 from redhat.
2. We use gcc version egcs-2.91.66 from redhat.
3. we use Python version 1.5.2 compiled with same gcc from redhat.
4. The class files are: spam.cpp and spam.h .
5. The files that include the c functions that wrap the class
functions are :  c_spam.cpp and c_spam.h .
6. The file that has the module extension is : ext_spam.cpp.
7. The file that includes the Python script that call the c++ is run.

We suspect that the problems might be one or combination of the
following issues:
2. Makefile bug (C++ flags and/or shared lib flags are missing).
3. Incompatibility between the way python was built and the version of
GCC/C++ we use for the example.

Thanks a lot in advance for your cooperation ,

Cheers,
Ephi.





More information about the Cplusplus-sig mailing list