Want to call C from Python? Use D!

In my last blog post I wrote about the power of D’s compile-time reflection and string mixins, showing how they could be used to call D from Python so easily it might as well be magic. As amazing as that may be for those of us who have D codebases we want to expose to Python users, this doesn’t help the vastly more numerous programmers who want to call pre-existing C code instead. If C had D’s metaprogramming abilities, imagine seamlessly calling into nanomsg with as much as ease as I showed in my previous blog post. Well… about that.

D can easily interoperate with C, with the only requirement being that the function and data structure declarations be translated into D syntax. But once the translation is done, those declarations are now D code that can be reflected on, fed to autowrap, and automagically wrapped for Python consumption. That would be a pretty powerful combo if not for the boring work of translating all needed declarations, macros included. It’s still a lot easier than talking to the Python C API itself of course, but maybe not quite killer feature material.

However, I wrote a little project called dpp because I’m lazy and don’t want to hand-translate C to D. Envious of C++’s and Objective C’s credible claim to be the only languages that can seamlessly interoperate with C (due to header inclusion and compatible syntax), I tried to replicate the experience in the D world. Using dpp, one can #include C headers in what would otherwise be D code and use it as one would in C++, even going to the point of supporting preprocessor macros. I wrote about the project in a different blog post.

Given this .dpp file:

// nanomsg.dpp
#include "nanomsg/nn.h"
#include "nanomsg/pipeline.h"

And this .d file:

import autowrap;
mixin(
    wrapDlang!(
        LibraryName("nanomsg"), // name of the .so
        Modules(Yes.alwaysExport, "nanomsg") // name of the D module
    )
);

When we build both of those files above into nanomsg.so, we get to write this Python code that actually sends packets:

from nanomsg import (nn_socket, nn_close, nn_bind, nn_connect,
                     nn_send, nn_recv, AF_SP, NN_PUSH, NN_PULL)
import time

uri = "inproc://test"

pull = nn_socket(AF_SP, NN_PULL)
nn_bind(pull, uri)
time.sleep(0.05)  # give it time to set up (awful I know, but meh)

push = nn_socket(AF_SP, NN_PUSH)
nn_connect(push, uri)
msg = b'abc'
nn_send(push, msg, len(msg), 0)

Python, welcome to C, via D, and without even having to write any code to do it. Did I mention that AF_SP, NN_PUSH, and NN_PULL are all C macros? And yet, look at Python importing and using them like a boss.

Want to try it yourself? It’s on github.

If you want to call C from Python, use D.

Tagged , , , , , ,

17 thoughts on “Want to call C from Python? Use D!

  1. […] In my last blog post I wrote about the power of D’s compile-time reflection and string mixins, showing how they could be used to call D from Python so easily it might as well be magic. As ama… Read more […]

  2. […] Want to call C from Python? Use D 2 by atilaneves | 0 comments on Hacker News. […]

  3. New top story on Hacker News: Want to call C from Python? Use D – The Pakistani News Corner says:

    […] Want to call C from Python? Use D 2 by atilaneves | 0 comments on Hacker News. […]

  4. […] For complete news please follow article link on atilaneves […]

  5. Or you could use the ctypes module built-in to Python, which will allow simple function calls to be done with basically no extra effort, and more complex ones, with a bit of minor fuss.

    Really the only major issue I’ve had with ctypes is getting C #define’d constants in, but it’s not usually a big deal, depending on the library.

    • atilaneves says:

      In my experience, any non-trivial C API needs the preprocessor. Like the one in the example. “Not a big deal” and “not a deal at all” are worlds apart.

    • atilaneves says:

      I’ve actually had time to look at ctypes now and I don’t know how anyone who read the blog post could suggest it in comparison to this. The work involved in calling nanomsg from Python using ctypes is so much that I wouldn’t even want to try just to prove how bad it is.

  6. […] Want to call C from Python? Use D 19 by atilaneves | 1 comments on Hacker News. […]

  7. […] Want to call C from Python? Use D 26 by atilaneves | 1 comments on Hacker News. […]

  8. […] Want to call C from Python? Use D 26 by atilaneves | 1 comments on Hacker News. […]

  9. […] Want to call C from Python? Use D 30 by atilaneves | 2 comments on Hacker News. […]

  10. […] Want to call C from Python? Use D 72 by atilaneves | 41 comments on Hacker News. […]

  11. […] Want to call C from Python? Use DIn my last blog post I wrote about the power of D’s compile-time reflection and string mixins, showing how they could be used to call D from Python so easily it might as well be magic. As ama… […]

  12. […] week I suggested that if you want to call C code from Python that you should use D. I still think that 4 lines of […]

Leave a comment