1

I would like to know how to use an existing C interface (a header file) and implement such an interface in Python by using the CFFI library.

For example, I have the following header file, interface.h:

void* foo(void* value);

And I want to implement it in Python. I thought the following program would do the work work, but it doesn't. It creates the function foo, but it doesn't guarantee that the implementation follows the structure defined in the header file.

import cffi

ffibuilder = cffi.FFI()

with open('interface.h') as f:
    data = ''.join([line for line in f if not line.startswith('#')])
    ffibuilder.embedding_api(data)

ffibuilder.set_source("_lib", r'''
    #include "interface.h"
''')

ffibuilder.embedding_init_code("""
    from _lib import ffi

     #include "interface.h"

    @ffi.def_extern()
    def foo(param):
        return __import__(param)

""")

ffibuilder.compile(verbose=True)

How to pass Python objects using CFFI?

In the above example, as a result of calling foo that returns a Python object. Then on the client-side, I have the following:

ffi = FFI()
# Load my lib file
lib = C.CDLL(dll)

h = lib.foo("math")

When I look up the value of h, it shows a number of type integer. However, I was expecting to receive an object. According to the interface, foo must return a pointervoid*. How can I read that object from it? Am I doing something wrong?

4
  • This looks correct to me. What are you really asking? Can you give an example of the kind of failure you're talking about? Commented Mar 8, 2021 at 13:58
  • For instance, if I change the implementation of the method foo to receive two arguments instead of one. It still works (I think it shouldn't because it doesn't match the specification of the header file). Commented Mar 8, 2021 at 14:28
  • 1
    "It still works" as in you can run the above script and compile the .so/.dll file, yes. Just like you could compile it if the function contained a typo in a variable name. It compiles but it fails at runtime. If that's what you are concerned about then using Python in the first place is probably not the right choice... Commented Mar 10, 2021 at 5:05
  • you're right, @ArminRigo! thanks! Commented Mar 10, 2021 at 15:43

2 Answers 2

2

To answer your last question: you are using ctypes, not cffi, in this example. These are different project. It should be apparent because the ffi object is from cffi, but is unused in the following lines. Instead of lib = C.CDLL() with I guess C being an alias to ctypes, you should call lib = ffi.dlopen("...")!

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, you're right. I was using ctypes for loading the DLL file. However, I tried changing it to ffi.dlopen, but it does not work when I try to call any function within the library. This is the output error: File "/python_app.py", line 65, in <module> print(py.foo(5)) File "/python_app.py", line 51, in fooz return self.lib.foo(bn) File "/venv/lib/python2.7/site-packages/cffi/api.py", line 912, in getattr make_accessor(name) File "/venv/lib/python2.7/site-packages/cffi/api.py", line 907, in make_accessor raise AttributeError(name) AttributeError: foo
1

I found that the problem is that when switching from ctypes to CFFI for loading a DLL. The first requires the definition of the methods that are available in the dynamic library. Therefore, adding this fixes the problem

ffi.cdef("""
  void foo(void* value);
""")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.