Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
10 lines (8 sloc) 4.37 KB
---
layout: post
title: "Embedding Python in D"
date: 2009-01-01
comments: false
---
<div class='post'>
<div style="float:right; margin: 0pt 0pt 10px 10px; "><img style="cursor: pointer; width: 200px; height: 200px;" src="http://abandonedwig.info/blog/uploaded_images/python-758704.png" alt="" border="0" /><div style="font-size: small; width:100%; text-align:center;">Oooooooooh.<br/>It's some snakes.<br/>It's some snakes.</div></div><br />A while back I began spending some time acquainting myself with D. For those who don't know, D is a relatively new <a href="http://en.wikipedia.org/wiki/System_programming_language">system programming language</a> with all kinds of interesting features like very flexible garbage collection, mixins and an easy-to-use yet powerful template system. D was created by <a href="http://en.wikipedia.org/wiki/Walter_Bright">Walter Bright</a>, who also created the first native C++ compiler and <a href="http://en.wikipedia.org/wiki/Classic_Empire_%28computer_game%29">Empire</a> (of all things).<br /><br />Anyhow, one of the things that I really like about D is that it is maintains link compatibility with C <sup>1</sup>. This means that D code can make calls into libraries like those from GTK in the same way that C++ code can. This mitigates the need or the desire to rewrite useful tools. Many times simple binding layers can be written to expose C-style APIs in D-style ways.<br /><br />In my case, I'm writing an application which uses the GTK bindings for D, <a href="http://www.dsource.org/projects/gtkd">GtkD</a>, and embeds a Python interpreter. Creating the D header from Python.h turns out to be pretty easy with <a href="http://www.dsource.org/projects/bcd/">bcd</a>, a tool which tries to automate most of the process <sup>2</sup>.<br /><br />The first step is to run bcd against the header you want to use. For me this looked like:<br /><pre>./bcdgen /usr/include/python2.5/Python.h Python -C -A</pre>In this case "-C" means "C-mode" (as opposed to C++-mode) and "-A" tells bcdgen to continue working on all includes recursively (since Python.h is really a meta-header). Unfortunately this gives me:<br /><pre>In file included from /usr/include/stdio.h:906,<br /> from /usr/include/python2.5/Python.h:32:<br />/usr/include/bits/stdio2.h: In function 'int sprintf(char*, const char*, ...)':<br />/usr/include/bits/stdio2.h:35: error: '__builtin_va_arg_pack' was not declared in this scope<br />/usr/include/bits/stdio2.h: In function 'int snprintf(char*, size_t, const char*, ...)':<br />...</pre>This is a <a href="https://bugs.launchpad.net/ubuntu/+source/gccxml/+bug/293807">bug</a> with Intrepid Ibex's version of gccxml. Luckily there are <a href="http://www.kloss-familie.de/moin/Root/PatchingGccXml">workarounds</a>. Once bcdgen finishes it's work, it should create a bcd/Python/Python.d, which is where all the Python.h definitions landed. If you open it up and take a look, you can see bcdgen has converted them to D definitions. Way to go automated software development tools!<br /><br />Generally some work is required before the D compiler accepts this file and the bcd page lists some general guidelines for this task. I'm familiar with Python's C interface, but definitely not intimate. This is a secret code that I'm using which means I spent a bit of time fixing weird D compiler errors. It wasn't bad though and I was was able to resolve most of the compile errors without much trouble. I'm my next post I'll write a little bit about that.<br /><hr /><div style="font-size: small;"><ol><li>D does not have link compatibility with C++. Many C++ projects have have external APIs which are C-compatible though. One example from recent memory is the KJS API from WebKit.</li><br /><li>There is a really great project called <a href="http://pyd.dsource.org/celerid.html">CeleriD</a>, which has already done a lot of this work. CeleriD is really geared toward extending Python rather than embedding it though. I decided to strike out on my own as an educational gesture<sup>3</sup> and to avoid depending on distutils for my build process.</li><br /><li><i>Gesture</i> because it's hard to believe this is can be considered larnin'<sup>4</sup>, which is coincidentally the way I feel about most professional software training programs.</li><br /><li>The best thing about the &lt;sup&gt; tag is that it's a formatting markup <i>and</i> a greeting. 'Sup &lt;sup&gt;?</li></ol><br /></div></div>