I have a C source/header file that are part of a bigger project. I would like to test this as a unit, independent of the real project. While it would be possible to do this in C by creating a new project with a different main(), I would like to see if I can use Python (3) and its frameworks (eg. nose) to accelerate the construction of tests, use existing reporting frameworks, etc.
I was under the impression that I could do this with CFFI. Here's a sample C file:
// magic.c
// Implementation of magic.
int add(int a, int b)
{
    return a;
}
The header:
// magic.h
// Add two numbers (where a + b is not greater than INT_MAX).
int add(int a, int b);
Here's a script that just tries to compile it so I can call some functions:
# cffi_test.py
import cffi
INCLUDE_DIRS = ('.',)
SOURCES = ('magic.c',)
ffi = cffi.FFI()
ffi.set_source(
    '_magic_tests',
    '#include "magic.h"',
    include_dirs = INCLUDE_DIRS,
    sources = SOURCES,
    libraries = [],
    )
ffi.compile()
Ultimately I plan to have this be part of the setup before a set of unit tests eg. a pure Python function test_add() will call and check the result of the C function add() via the ffi object, which is constructed in the test setup.
The above script seems to work; it runs without error, it creates a _magic_tests.c file, a _magic_tests.cp35-win32.pyd file, and a Release directory. I can also import _magic_tests without an error.
But I can't figure out how to actually call a C function via CFFI. I can't find any documentation for the set_source() function, and it seems pretty integral to the whole process. The overview mentions it a lot, but the reference contains zero occurrences of it. The docs do have a section on calling functions, but it refers to some lib object without showing how it's created. If I look at the previous example there's a lib object created from ffi.dlopen(), but I don't see how to apply that to something that CFFI itself is producing.
My big question (ie. my X problem) is:
The questions arising from my current approach (ie. my Y problems) are:
set_source()? How can I find out what arguments it takes?lib objects that contain the functions I want to call?My current setup is:
I am using CFFI and pycparser from Christoph Gohlke's repository.
CFFI is an external package providing a C Foreign Function Interface for Python. CFFI allows one to interact with almost any C code from Python. However, C++ is not currently supported.
CFFI is a Python module which will read C function prototypes automatically generate some of the marshalling to and from these C functions.
For a project of mine, I use cffi to test my C code. IMHO cffi is a great tool to generate python bindings for C code and therefore think that it is a reasonable tool to use for calling and testing C functions from python. However, your code will only be as cross platform as the C code is, since you have to compile the binding for every platform.
Below you can find a few references to the documentation that should answer your questions. Additionally I wrote some example code to illustrate how you would use cffi. For a larger example, you can find my project at https://github.com/ntruessel/qcgc/tree/master/test.
The documentation for set_source() can be found here https://cffi.readthedocs.io/en/latest/cdef.html
https://cffi.readthedocs.io/en/latest/overview.html explains how you can use CFFI, I recommend API, out-of-line.
Four your example, build_magic_tests.py would look something like this:
from cffi import FFI
ffibuilder = FFI()
# For every function that you want to have a python binding,
# specify its declaration here
ffibuilder.cdef("""
    int add(int a, int b);
                """)
# Here go the sources, most likely only includes and additional functions if necessary
ffibuilder.set_source("magic_tests",
    """
    #include "magic.h"
    """, sources=["magic.c"])
if __name__ == "__main__":
    ffibuilder.compile()
To generate the magic_tests module, you have to run python build_magic_tests.py. The generated module can be imported and used like this:
from magic_tests import ffi, lib
def run_add():
    assert 4 == lib.add(4, 5)
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With