Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python example? #207

Open
subalterngames opened this issue Feb 29, 2024 · 1 comment
Open

Python example? #207

subalterngames opened this issue Feb 29, 2024 · 1 comment

Comments

@subalterngames
Copy link

There is a test that generates a Python cffi. Is there an example of how to actually use this file?

@Ronnbreizh
Copy link

Hi, I wrote the Python CFFI plugin for safer_ffi when I was working with @danielhenrymantilla.
You can find the package dittoffi on Pypi which uses the files generated by safer-ffi.
Notably, you should pay attention to the _ffi_build.py file, which includes most of the CFFI logic. Here is an excerpt :

from cffi import FFI
ffi = FFI()

# Mostly company internal logic to fetch the correct lib and read the headers 

async def prepare_compilation():
    with read_header_file("dittoffi.cffi") as content:
        ffi.cdef(content)

    with read_header_file("dittoffi.h") as content:
        ffi.set_source("_ffi", content,
            library_dirs = [await find_ditto_path()],
            libraries = ['dittoffi']
        )

asyncio.run(prepare_compilation())

if __name__ == "__main__":
    ffi.compile(verbose=True)

Finally, beware that the build instructions are splitted between the pyproject.toml and the setup.py file due to some CFFI shenanigans.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants