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

Adrian Eyre a.eyre at optichrome.com
Wed Dec 1 15:21:49 CET 1999


> I haven't particularly followed the discussion, but I just want to
> point out that doing what you suggest is extremely dangerous. There is
> no guarantee that longs are the same size as pointers.

There's no guarantee of most types in C++, only that a long is at least
1 byte. It's up to the programmer to make their C++ code platform
independent by using #ifdefs and typedefs, but I thought that much was
obvious. Besides which, the criteria IIRC was to run in on a PC on Linux,
for which the stated code will work.

> There is also nothing to stop the Python code from modifying the integer,
> causing the C code to dereference arbitrary memory locations.

Also true. However, if providing the Python programmer is aware of how the
extension works, and then chooses to modify the pointer, or whatever, then
they Deserve To Lose (tm).

> It shouldn't be possible for Python code to cause core dumps. You should
> use PyCObjects instead.

It shouldn't, no, but I present you with the following code:

#!/usr/bin/env python

l = []
m = []
l.append(m)
m.append(l)
print l == m

It was just a reponse to get the code to work. I mentioned that there were
better ways of doing it. Although interestingly enough, code almost
identical
to what I posted appears in Mark Lutz's Programming Python book, as a valid
way of achieve an extension type/class.

--------------------------------------------
Adrian Eyre <mailto:a.eyre at optichrome.com>
Optichrome Computer Solutions Ltd
Maybury Road, Woking, Surrey, GU21 5HX, UK
Tel: +44 1483 740 233  Fax: +44 1483 760 644
http://www.optichrome.com
--------------------------------------------





More information about the Cplusplus-sig mailing list