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

Geoffrey Furnish furnish at actel.com
Wed Dec 1 18:08:44 CET 1999

skaller writes:
 > David Beazley wrote:
 > The conversion of pointers to ints is not supported in C++:

Right.  Pointer values must be held in objects of pointer type.  And
anticipating the following, I'll go ahead and state that pointer
values must be held in objects of pointer type which are of the same
type as the pointer in question (or which are accessible through valid 
pointer conversions).

 > For those using C Python sources with C++, there are two
 > gotchas.
 > 	1)  Enumerations are NOT integers in C++
 > 		[Luckily, Guido doesn't use them much]

Just to amplify, you can always make an int from an enum if you need
to.  A common case where this issue comes up, is in situations like
the following:

enum { This, That };

vector<int>::iterator it = find( v.begin(), v.end(), That );

which will not work (will not compile), but which can be repaired by

vector<int>::iterator it = find( v.begin(), v.end(), int(That) );

 > 	2) The very core of Python contains a serious
 > 		breach of C++ rules: it casts functions
 > 		pointers, which is NOT allowed AT ALL.
 > [I'm responsible for that rule!]

Thank you!

 > Luckily, this actually works on most C++ compilers.

I disagree with this.  It does not work on /any/ "C++" compilers.
There are however, on the market, many compilers which support any of
a family of as yet undocumented languages, which seem all to be
similar to "C++" in many ways, but which evidently support various
"extension" constructs that are illegal in "C++", such as casting

The right thing to do, is to avoid efforts to cast function pointers.

For the Python/C++ interface, this basically means you need a seperate 
extension type object for each C++ object you wish to register as a
Python extension.

Making this machinery work easily and maintainably seems to be
challenging.  I don't personally have much hope for doing it, except
through heavy use of template mechanisms, which have been the source
of much consternation on this sig.

Geoffrey Furnish            Actel Corporation        furnish at actel.com
Senior Staff Engineer      955 East Arques Ave       voice: 408-522-7528
Placement & Routing     Sunnyvale, CA   94086-4533   fax:   408-328-2303

"... because only those who write the code truly control the project."
						      -- Jamie Zawinski

More information about the Cplusplus-sig mailing list