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
4
votes
1 answer

Function always returns meaningless value

I'm writing C functions to be called in pypy3 by cffi. However, the wrapped functions always return a meaningless value in pypy3, no matter what the true return value is. The output of the printf() function tells me everything works fine in the C…
Alex
  • 335
  • 2
  • 12
4
votes
1 answer

add flags to cffi compile process

I use cffi module to wrap a simple c code. the problem is, I need to add a flag to make it compile (std=c99). currently I have something like that: from cffi import FFI ffibuilder = FFI() with open("test.c", 'r') as f: …
tal
  • 860
  • 7
  • 19
4
votes
1 answer

How to properly wrap a C library with Python CFFI

I am trying to wrap a very simple C library containing only two .C source files: dbc2dbf.c and blast.c I am doing the following (from the documentation): import os from cffi import FFI blastbuilder = FFI() ffibuilder = FFI() with…
fccoelho
  • 6,012
  • 10
  • 55
  • 67
4
votes
1 answer

Conventions for memory management and destructors / free() with Python's CFFI?

If I'm wrapping a C class: from ._ffi import ffi, lib class MyClass(object): def __init__(self): self._c_class = lib.MyClass_create() What are best practices for making sure that lib.MyClass_destroy(…) is called? Does cffi have some…
David Wolever
  • 148,955
  • 89
  • 346
  • 502
4
votes
1 answer

How do I pass a pointer to a C function with Python's CFFI?

How can I pass a pointer to a C function with Python's CFFI? For example, if the library I'm wrapping has two functions: void some_function(void (*callback)()) { callback(); } void some_callback() { printf("callback!\n"); } How can I call…
David Wolever
  • 148,955
  • 89
  • 346
  • 502
3
votes
0 answers

How to make an async call to blocking CFFI functions

I have a set of blocking C-based functions. I am trying to find a way to make async calls to those blocking functions from python. Is there a way to do this from a python interface? for example async def my_func(): …
BarisY
  • 171
  • 2
  • 4
  • 14
3
votes
1 answer

Building Python-C Extension using CFFI, but Setuptools does not include custom header files in build

I'm trying to use the CFFI package in Python to create a Python interface for already existing C-code. I am able to compile a C library by following this blog post. Now I want to make it so that this python library is available without any fancy…
rafferino
  • 31
  • 1
3
votes
1 answer

Rust struct into PyObject in rust-cpython

I am using rust-cpython to write functions in Rust that is callable in Python. I have an existing struct that is used as an output. How to I make this into a PyObject that rust-cpython can understand? My struct looks like this: struct Block { …
kentwait
  • 1,969
  • 2
  • 21
  • 42
3
votes
2 answers

How to handle member padding in C struct when reading cffi.buffer with numpy.frombuffer?

I have to read an array of C structs returned from a dll and convert it to a Numpy array. The code uses Python's cffi module. The code works so far but I don't know how to handle the member padding in the struct that np.frombuffer complains…
Joe
  • 6,758
  • 2
  • 26
  • 47
3
votes
1 answer

Do I need to free memory returned from a C function called via CFFI?

I have this example code that has a function text() returning a newly allocated string: ffi_test = FFI() ffi_test.set_source('_test', ''' char* test() { return strdup("hello world"); } ''') ffi_test.cdef(''' char* test(); void free(void…
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
3
votes
0 answers

Overriding Conda Package Installation with Pip

I'm trying to build the necessary dependencies to run some deep learning code and am running into issues with the c foreign function interface (cffi) installation. I need conda's accelerate package, however the package is built on top of an old…
3
votes
0 answers

Parallel drawing with GTK and Cairo in Python 3

I am making a GTK application that will draw complex images that can take long time to finish. Because of that I can't do the drawing in the DrawingArea's 'draw' callback. I decided to use Python's multiprocessing module that allows true parallelism…
3
votes
1 answer

Flask-Bcrypt - AttributeError: 'module' object has no attribute 'ffi' - deployed to AWS Beanstalk

I have a Flask application which has been deployed to Ubuntu instances as well as working correctly in my local virtualenv. The issue lies with Flask-Bcrypt, which extends Bcrypt. When I deploy to AWS Beanstalk I receive the follow error: File…
3
votes
1 answer

How to find address of a pointer to structure and cast it to void** in CFFI

My code in C++ is StructureEx* obj; // structure functionEx((void**)&obj); and my function is int functionEx(void** obj); //calling function I am new to CFFI. So my question is How can I achieve the same in CFFI? How to find the address of a…
Ash
  • 809
  • 1
  • 8
  • 14
3
votes
2 answers

Create a CData-typed buffer in Python with CFFI

I am trying to create a buffer with a special type in Python, to send it to a C function wrapped with CFFI. In C, I have something like: typedef unsigned char UINT8; typedef UINT8* PUINT8; Then, in Python, the above lines are in the ffi.cdef()…
DRz
  • 1,078
  • 1
  • 11
  • 29
1 2
3
14 15