Python calling D
Russel Winder
russel at winder.org.uk
Tue Feb 4 02:35:13 PST 2014
On Tue, 2014-02-04 at 07:13 +0000, Artem Tarasov wrote:
> On Sunday, 2 February 2014 at 15:31:30 UTC, Russel Winder wrote:
> > result is:
> >
> > |> LD_LIBRARY_PATH=. python execute.py
> > Segmentation fault
>
> You should call Runtime.initialize() prior to calling any other D
> functions.
Hummm… obvious once pointed out :-) Thanks for doing exactly that.
The question is how to get this run. I tried a module initializer:
static this() {
Runtime.initialize();
}
but this module never actually gets loaded per se, so that is never
going to work. So this means calling a C linkage function to do the
initialization in every entry point:
import core.runtime: Runtime;
import std.stdio: writeln;
auto initialized = false;
extern (C) void initializeIfNotAlreadyDone() {
if (!initialized) {
Runtime.initialize();
initialized = true;
}
}
extern(C) {
void sayHello() {
initializeIfNotAlreadyDone();
writeln("Hello World.");
}
}
works a treat:
scons: Building targets ...
dmd -I. -fPIC -c -fPIC -ofhelloWorld.o helloWorld.d
dmd -shared -defaultlib=libphobos2.so -oflib_helloWorld.so helloWorld.o -fPIC -L-L.
LD_LIBRARY_PATH=. python execute.py
Hello World.
scons: done building targets.
So D extensions to CPython are possible without PyD!
Now to continue with PyD anyway so as to compare and contrast.
--
Russel.
=============================================================================
Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder at ekiga.net
41 Buckmaster Road m: +44 7770 465 077 xmpp: russel at winder.org.uk
London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
More information about the Digitalmars-d-learn
mailing list