7 votes

Want to call C from Python? Use D!

2 comments

  1. [2]
    Deimos
    Link
    Hmm, interesting. You can call C from Python directly (as the article mentions), but it's kind of ugly and difficult. Tildes does that for markdown processing, passing markdown through the C...

    Hmm, interesting. You can call C from Python directly (as the article mentions), but it's kind of ugly and difficult. Tildes does that for markdown processing, passing markdown through the C library for GitHub's variant of CommonMark. This is the file that sets up all the functions to be able to call them, you have to define all the return and argument types: https://gitlab.com/tildes/tildes/-/blob/master/tildes/tildes/lib/cmark.py

    5 votes
    1. arghdos
      Link Parent
      If you're willing to pull in another package, I have typically used Cython for C bindings. Much cleaner than ctypes IMO, but I like that the D method doesn't require you to rewrite the interface...

      If you're willing to pull in another package, I have typically used Cython for C bindings. Much cleaner than ctypes IMO, but I like that the D method doesn't require you to rewrite the interface to the external code -- that's a huge plus, assuming it works well. I also know people who quite like pybind11 (roughly Boost.Python minus Boost), but I haven't used it myself, and it's primarily designed to be implemented inside of the C code (hence, you'd still be writing a C wrapper).

      4 votes