Extending Python with D¶
PyD supports building python modules with D.
Basics¶
A minimal working PyD module looks something like
// A minimal "hello world" Pyd module.
module hello;
import pyd.pyd;
import std.stdio;
void hello() {
writefln("Hello, world!");
}
extern(C) void PydMain() {
def!(hello)();
module_init();
}
This code imports the components of pyd, and provides the initializer function that python will call when it loads your module. It also exposes hello to python.
- Some notes:
- def must be called before module_init. class_wrap must be called after module_init.
- PydMain will catch any thrown D exceptions and safely pass them to Python.
This extension is then built the usual way with distutils, except PyD provides some patches to support D compilers:
from pyd.support import setup, Extension
projName = 'hello'
setup(
name=projName,
version='0.1',
ext_modules=[
Extension(projName, ['hello.d'],
extra_compile_args=['-w'],
build_deimos=True,
d_lump=True
)
],
)
$ python setup.py install
Usage:
import hello
hello.hello()