* sturlamolden, on 13.07.2010 22:03:
On 9 Jul, 17:52, "Alf P. Steinbach /Usenet"<alf.p.steinbach
+use...@gmail.com> wrote:
For an extension module it seems that Python requires each routine to be defined
as 'extern "C"'.
That is strange. PyMethodDef is just a jump table. So why should
'extern "C"' matter?
Formally because they're incompatible function pointer types.
C++98 standard §7.5/1: "Two function types with different language linkages are
distinct types even if they are otherwise identical". Add to that §7.5/4 "A
linkage-specification shall occur only in namespace scope". And add to that
§14-4 "A template, a template explicit specialization, or a class-template
partial specialization shall not have C linkage." This means that formally
correct code that generates callbacks by templating, is ruled out.
In practice, 'extern "C"' matters for the jump tables because for those few
compilers if any where it really matters (not just the compiler emitting a
warning like reportedly Sun CC does), different linkage can imply different
machine code level calling convention. For example, who's responsible for
cleaning up the stack, the order in which arguments are pushed or which
registers they're passed in, and so forth. Ignoring such matters your code gets
into la-la land pretty fast, but, it's a different matter when one /understands/
this and places a requirement on the compiler.
Good luck on re-inventing the wheel (you've probably heared about
Swig, SIP, Boost.Python, PyCXX, scipy.weave and Cython...)
Yes, I know Boost.Python in more detail and I've heard of all the rest except
SIP, but then regarding SIP I really don't like QT (QT makes eminent sense in
the context of Python, they're both essentially dynamically typed, but that
means QT is not very nice as C++, plus there is the ugly preprocessor).
And as you'd guess if you were not in silly ignoramus assertion-mode, I'm not
reinventing the wheel.
Cheers & hth.,
- Alf
--
blog at <url: http://alfps.wordpress.com>
--
http://mail.python.org/mailman/listinfo/python-list