Questions tagged [python-cffi]

Foreign Function Interface for Python calling C code. The aim of this project is to provide a convenient and reliable way of calling C code from Python.

Foreign Function Interface for Python calling C code. The aim of this project is to provide a convenient and reliable way of calling C code from Python.

More Details

216 questions
3
votes
1 answer

Using Python's CFFI and excluding system headers

I'm trying to use Python's CFFI to develop Python bindings to a scientific model written in C. The CFFI documentation is a little sparse and I'm stuck at the cdef stage. My process up to now has followed these steps: Preprocess the header…
jhamman
  • 5,867
  • 19
  • 39
3
votes
1 answer

How do I wrap a C-library including its header into a python program using CFFI?

from cffi import FFI ffi = FFI() header_path = '/usr/include/libelf.h' with open(header_path) as f: ffi.cdef(f.read()) lib = ffi.dlopen('/usr/local/lib/libelf.so') The code above is the one I am actually struggling with. For using some…
Maximilian
  • 1,325
  • 2
  • 14
  • 35
3
votes
1 answer

How can I access errno from Python's CFFI?

I'm using cffi to wrap a library that uses errno to return error values. How can I read errno from cffi?
David Wolever
  • 148,955
  • 89
  • 346
  • 502
3
votes
3 answers

Accessing cffi enums

Suppose I define an enum under cffi: from cffi import FFI ffi = FFI() ffi.cdef('typedef enum {RANDOM, IMMEDIATE, SEARCH} strategy;') Now this can be easily accessed when calling cdef again. But how would I then like to access this enum in python,…
simonzack
  • 19,729
  • 13
  • 73
  • 118
3
votes
1 answer

How to use shared dynamic libraries with python-cffi (in linux)?

OS: CentOS 6 (64bit) I have a dynamic library (.so) in C. And I want to create an abstraction layer of Python over it and then use it to implement my logic. I have decided to use CFFI for this since it doesn't deal with any kind of dsl (domain…
utsavanand
  • 329
  • 1
  • 3
  • 15
3
votes
1 answer

Declare struct containing time_t field in Python CFFI

I am using CFFI to call a C function from Python that returns a struct. The struct is defined with a time_t element. How do I declare the struct to CFFI so that I can access it from Python? For example, I tried the following (to get the modified…
Mechanical snail
  • 29,755
  • 14
  • 88
  • 113
2
votes
1 answer

python3.10 bdist_wheel did not run successfully when install cffi

i installed Ubuntu 22.04 (fresh installation) an create an virtualenv with Python 3.10. The installed packages are as…
2
votes
1 answer

Use CFFI for using Python function inside Fortran. Warnings in cffi build. I can't get results from print in console or file, but not error in running

Coding I followed documentation described in "CFFI Documentation Release 1.15.0" section "9.1 Usage" but with semplification, using a 'identity' function. Step 1 - plugin.h # ifndef CFFI_DLLEXPORT # if defined(_MSC_VER) # define CFFI_DLLEXPORT…
Henry
  • 21
  • 2
2
votes
1 answer

using an int * (array) return value in python via cffi?

I have a C function int * myfunc() { int * ret = (int *) malloc(sizeof(int)*5); ... return ret; } in python I can call it as ret = lib.myfunc() but I can't seem to figure out how to actually use ret in the python code (i.e. cast it to an…
spotter
  • 1,178
  • 1
  • 9
  • 19
2
votes
0 answers

pipenv version 2020.6.2 unable to install some packages? pip3 is able to install same package pipenv can''t?

I would love to use pipenv but two packages are causing problems for me? There appears to be several similar posts over past 2+ years with no real answer I could find. I got the latest version of pipenv (2020.6.2) and am asking for install latest…
John M.
  • 31
  • 3
2
votes
1 answer

How to Generate SignedUrls for CloudFront in Lambda Using Python?

I'm currently having an issue with my python code, this code generates a Cloud Front signed URL and prints it. I have a virtual environment in VS Code and it works fine but as soon as I upload it to lambda I get the error: "errorMessage": "No…
2
votes
1 answer

Is there a way to call async interface of rust from python?

I wrap some functions of reqwest of rust into req.lib file and successfully call it from python by using cffi. However reqwest::blocking::Client force me to use multi-threading in python. I find reqwest can be called in async mode in rust. I wonder…
ShenLei
  • 567
  • 1
  • 5
  • 17
2
votes
0 answers

How to use python cffi library with TI cl2000 compiler?

I am trying to use cffi for compiling code written for TI's f28035 microcontroller. Then I'd like to unit test the code with the python unittest library. I need TI's cl2000 compiler instead of visual c++. I've added the path to cl2000 to my Path…
2
votes
1 answer

Using standard library headers with CFFI

I am using Python and CFFI to write some unit tests for a library that I have written in C, this library involves socket programming on Linux. The .c file is compiled into a shared library and then loaded using ffi.dlopen(). Naturally the struct…
s_ber
  • 123
  • 4
2
votes
0 answers

Putting a cffi function in an lru_cache causes segfault

If I put a cffi function in an lru_cache, I get a segfault, but cannot figure out why. I am compiling simple C functions at runtime with the cffi package. I want to cache the results of this compilation because compilation has a non-trivial cost.…
drhagen
  • 8,331
  • 8
  • 53
  • 82