[Python-3000] Python/C++ question

Talin talin at acm.org
Thu Aug 10 01:13:05 CEST 2006


A while back someone proposed switching to C++ as the implementation 
language for CPython, and the response was that this would make ABI 
compatibility too difficult, since the different C++ compilers don't 
have a common way to represent things like vtables and such.

However, I was thinking - if you remove all of the ABI-breaking features 
of C++, such as virtual functions, name mangling, RTTI, exceptions, and 
so on, its still a pretty nice language compared to C - you still have 
things like namespaces, constructors/destructors (especially nice for 
stack-local objects), overloadable type conversion, automatic 
upcasting/downcasting, references, plus you don't have to keep repeating 
the word 'struct' everywhere.

Think how much cleaner the Python source would be if just one C++ 
feature - namespaces - could be used. Imagine being able to put all of 
your enumeration values in their own namespace, instead of mixing them 
in with all the other global symbols.

Think of the gazillions of cast operators you could get rid of if you 
could assign from PyString* to PyObject*, without having to explicitly 
cast between pointer types.

My question is, however - would this even work? That is, if you wrapped 
all the source files in 'extern "C"', turned off the exception and RTTI 
compiler switches, suppressed the use of the C++ runtime libs and 
forbade use of the word 'virtual', would that effectively avoid the ABI 
compatibility issues? Would you be able to produce, on all supported 
platforms, a binary executable that was interoperable with ones produced 
by straight C?

I actually have a personal motivation in asking this - it has been so 
many years since I've written in C, that I've actually *forgotten how*. 
Despite the fact that my very first C program, written in 1982, was a C 
compiler, today I find writing C programs a considerable challenge, 
because I don't remember exactly where the dividing line between C and 
C++ is - and I will either end up accidentally using a C++-specific 
language feature, or worse, I'll unconsciously avoid a valid C language 
feature because I don't remember whether it's C++ specific or not. (For 
example, I don't remember whether its valid to define an enumeration 
within a struct, which is something that I do all the time in C++.)

-- Talin


More information about the Python-3000 mailing list