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.
1
vote
1answer
16 views
How to make a C-DLL wrapped with CFFI to callback python
I am currently trying to wrap a c-dll to controll a camera connected via USB. To grab image data off the camera, the SDK-Provider (Thorlabs) describes two possibilities:
Poll the camera in a loop
Use ...
0
votes
1answer
18 views
Python CFFI enum from string name
I have an enum defined in Python cffi. How do I instantiate it by name? The docs say how to get the string name from enum, but not how to create it.
ffibuilder = FFI()
ffibuilder.cdef('typedef enum {...
0
votes
0answers
6 views
Embedding a python class in C
I would like to embed a whole python class in C.
Ideally, I wouldn't have to write the boilerplate wrappers for all my attributes, and methods.
Does CFFI provide a way to automagically generate ...
0
votes
1answer
40 views
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?...
0
votes
0answers
22 views
getting error installing python package brypt and cffi on raspbian stretch
I am trying to install brypt that has dependency on cffi,
but this is landing into the error shown below:
$ pip install bcrypt
Collecting bcrypt
Using cached https://files.pythonhosted.org/...
0
votes
0answers
34 views
Python, CFFI: Get the names of the arguments from the header file
I am using the CFFI module from python to use C functions in line. I want to add a layer of abstraction and would like to automatically generate it. To do this I need to know which of the parameters ...
0
votes
0answers
20 views
Python build CFFI in API mode with Setuptools
I'm trying to learn about creating a CFFI modules, and packaging them with setuptools. When I run the build script build_foo.py I get an API mode library, but when I try to pip install . I get an ABI ...
0
votes
0answers
13 views
Get the name of the extension file built by CFFI
If one builds a CFFI-based extension as
ffi = FFI()
ffi.cdef("...code...")
ffi.set_source("_example", "...code...")
ffi.compile()
a compiled extension file is created, for example on my system it's "...
1
vote
1answer
48 views
Resolving circular shared-object dependencies with ctypes/cffi
I would like to use cffi (or even ctypes if I must) to access a C ABI from Python 3 on Linux. The API is implemented by a number of .so files (let's call them libA.so, libB.so and libC.so), such that ...
0
votes
1answer
51 views
cffi.error.FFIError: multiple declarations of function
I am using CFFI to automatically wrap a DLL using the API - out of line approach.
This is actually done via Continuous Integration and has worked well so far. To make this possible, the DLL header ...
0
votes
1answer
66 views
How to work with Python's cffi and enum arrays
I have a function implemented in C that translates a string to an array of a defined enum. The C code looks like this:
Header file (describes the enum):
/* File `asdf.h` */
enum translation {
...
1
vote
1answer
36 views
memoryview and column-major / FORTRAN buffer
I have a buffer (I obtain it as bytes through cffi) for an column-major array.
Is there a way to obtain a memoryview for it with the correct attributes for Python's buffer protocol ? The method cast ...
0
votes
1answer
65 views
CFFI - Use struct with #ifdef
Currently I am wrapping some existing C-Code to Python. It is working quite well with CFFI, but there is one last thing i don't get.
How do I handle "#ifdef" statements?
Let's say i have this ...
0
votes
0answers
27 views
Creating a pointer to a multi dimensional array using cffi in python
I'm am trying to call a function from a fortran DLL in python. The dll expects pointer. I figured out how to create a pointer to a variable and to an array.
I also need a pointer to a multi ...
0
votes
1answer
60 views
can't install cffi with python3
I install from source code cffi with command "sudo python3 setup.py install"
and get this error
running install
running bdist_egg
running egg_info
writing cffi.egg-info/PKG-INFO
writing ...
1
vote
0answers
93 views
How to wrap a CFFI function in Numba taking Pointers
It should be a easy task, but I can't find a way how to pass a pointer of a scalar value to a CFFI function within a Numba function. Passing a pointer to an array works without problems using ffi....
0
votes
1answer
48 views
How can I get the `const` modifier of a CFFI function in Python?
I am writing a Python wrapper for generated C code, which contains functions that use pointer arguments as outputs. I want to introspect those functions, generated using CFFI, to determine if they are ...
0
votes
0answers
42 views
How to get the pointer in a C function with unknown length using CFFI?
The title of this question may be a little confusing.
I want to call a C function in python with cffi. It's a compression function. Part of the function looks like this:
int compression_float_3d(...
0
votes
0answers
225 views
raise AttributeError(name) AttributeError: LCC_GetChannelHandle
I am very new in python cffi. I have to access my temprature module by using its Index or with its channel name. I am trying with both as you can see in my QmixTC class. I am getting attribute error. ...
2
votes
1answer
122 views
Passing multiple numpy arrays to C using CFFI
I have the following (in real life, the C code is much more complex, and I don't want to change it. It uses globals everywhere).:
from cffi import FFI
ffibuilder = FFI()
ffibuilder.set_source(
"...
0
votes
1answer
461 views
cffi error when compiling faster rcnn pytorch
I'm trying to compile faster_rcnn_pytorch using the instructions given here: https://github.com/longcw/faster_rcnn_pytorch
I get this error:
(p27) [$USER@compute-1-5 faster_rcnn]$ ./make.sh
...
0
votes
0answers
130 views
Get data from returned C pointer in Python - CFFI
I am learning how to use CFFI for python. Currently, I am trying to put the data into an np.array().
I have a C-function looking like:
float * getIdentity()
{
int sz = sizeof(float)*9; // 3x3 ...
0
votes
1answer
225 views
Pass a list of object references to a CFFI function
I am coding on a project where I need to process many tree-like data elements, to overcome some performance issues I want to call a given processing function already written in C with the signature ...
0
votes
0answers
142 views
Why doesn't PyCharm/Anaconda3 find Microsoft Visual C++ 14.0 for CFFI?
I run the test code:
from cffi import FFI
ffi = FFI()
ffi.cdef("""
int add(int, int);
""")
C = ffi.verify("""
int add(int a, int b) {
return a + b;
}
""")
print(C.add(21, 21))
In PyCharm with ...
5
votes
0answers
517 views
What's the way to install Microsoft Visual C++ 14.0 for CFFI that takes the least harddrive space?
I want to write Python software that uses CFFI. CFFI itself throws the error:
distutils.errors.DistutilsPlatformError: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build ...
1
vote
1answer
36 views
Using cffi function with scipy.integrate.nquad
I can't make scipy.integrate.nquad work with cffi function.
I also couldn't find any examples of this online.
Let's say I have a simple c function in my test.py
ffibuilder.set_source("_test",
...
0
votes
2answers
169 views
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 ...
1
vote
0answers
89 views
Cffi : avoid duplicate declaration
I start to use cffi in a big project, with many file import.
As I use structure, I have many structure declaration like this :
ffi.cdef("""
typedef struct sMyStruct
{
int32_t nbData;
...
0
votes
2answers
167 views
PYPY, CFFI import error cffi library '_heap_i' has no function, constant, or global variable named 'initQueue'
So I am trying to use cffi to access a c library quickly in pypy.
I am using my macpro with command line tools 9.1
specifically I am comparing a pure python priority queue, with heapq, with a cffi, ...
1
vote
0answers
32 views
How to declare the predefined struct CLIENT in <rpc/clnt.h> when using python cffi?
Struct CLIENT is defined in rpc/clnt.h, but how can instantiate it when using python cffi?
For example,I have a c header file like this:
#include <rpc/rpc.h>
CLIENT* my_init(char* server_ip);
...
1
vote
2answers
554 views
loading .so library with cffi
I have some C library I want to access in Python using CFFI. After building the library, I get the 2 files: $HOME/libcint/include/cint.h and $HOME/libcint/lib/libcint.so.
Now for the CFFI API mode I ...
1
vote
1answer
375 views
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....
3
votes
0answers
109 views
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, ...
1
vote
1answer
493 views
How can I embed a Python function that returns a string in C using cffi?
I'm trying to embed a Python function in C using PyPy and cffi. I'm following this guide from the PyPy documentation.
The problem is, all the examples I've found operate on ints, and my function ...
0
votes
1answer
225 views
Why does bitbake recipe for python-cffi fail on do_fetch
Not sure is this is the right place to ask this:
I've been struggling with trying to create a recipe to compile python-cffi for the dora branch of bitbake and have had no luck yet. There seems to be ...
0
votes
0answers
93 views
Variable not found in library when importing with CFFI
I am trying to load the following .h file:
3 typedef struct {
4 uint8_t clock_pin;
5 } pinout;
6
7 pinout foo = {.clock_pin = 5};
8 pinout bar = {.clock_pin = 4};
Using the following python ...
0
votes
0answers
120 views
cffi can't parse memcpy from string.h
I watched a very compelling lecture on unit testing C code with Python and I've been trying to write some test code using the strategy. When I attempted to run a very simple test, I receive the error:
...
1
vote
3answers
468 views
How to cast a pointer to a Python cffi struct to System.IntPtr (.NET)?
I need to pass a System.IntPtr to a .NET function (Python with pythonnet). This pointer should refer to a struct created in cffi.
I found this:
from CLR.System import IntPtr, Int32
i = Int32(32)
p = ...
1
vote
1answer
249 views
Passing C objects (CFFI) to .NET (Pythonnet)
Using Python, I would like to use C or C++ structs defined in a dll and pass them to .NET dll written in C#. The C or C++ dll is added using cffi, the .NET one is loaded using pythonnet.
The ...
1
vote
0answers
37 views
Signature of a Python CFFI Function?
I am wrapping a number of libraries using the cffi Python module. I am writing mock libraries for them, and I want to determine if a particular invocation of a cffi wrapped function will result in an ...
0
votes
1answer
126 views
Typeerror when using CFFI to test C code using struct
I'm working on a cffi testing demo, and when I try to run the python tester file, it returns the following error: TypeError: initializer for ctype 'Car *' appears indeed to be 'Car *', but the types ...
3
votes
1answer
2k views
CFFI: TypeError: initializer for ctype 'char[]' must be a bytes or list or tuple, not str
Using the CFFI library for Python, I'm trying to coax a Python string into a char*, so that I can pass it to a C function that accepts char*. I can't seem to figure out what the right incantation is. ...
1
vote
1answer
266 views
Best way to pass pointer to C function from Python using CFFI
I want to create a Python wrapper to a C function in a thirdparty library that has a signature such as
int f(double* x);
where the function f modifies the input argument x (i.e., call by reference ...
0
votes
0answers
55 views
set_source_surface from RecordingSurface to PDFSurface with origin translation gives a PNG but empty PDF
in this example I am drawing some line elements 30px away from the origin in the Y coordinate while moving along he X coordinate. This is drawn on a recording surface because in my real code I don't ...
0
votes
1answer
569 views
How to instantiate a struct in python cffi?
I'm trying to instantiate a struct using the Python cffi library. I'd like to instantiate a struct from my own .h file as well as ones from the standard library.
import datetime
import os
from ...
0
votes
1answer
232 views
Python CFFI doesn't copy typedef from cdef() into generated C file
I'm feeding a generated header file into ffi.cdef(), with a bunch of typedefs like this at the beginning:
typedef enum
{
LE_GPIO_EDGE_NONE = 0,
LE_GPIO_EDGE_RISING = 1,
// ...etc...
}
...
0
votes
1answer
255 views
CFFI how to avoid manual setting of LD_LIBRARY_PATH
I am using python CFFI to build a wrapper around some external library, called libfpta, which I would like to put inside my python repo (myrepo/lib/libfpta.so) and thus distribute alongside with ...
2
votes
0answers
285 views
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 ...
0
votes
1answer
106 views
Ctypes: DataTypes from C-definition
I have the address of a function and also its "c-like" representation (from llvmlite, but this is not that important).
For a function, which adds up two double values and returns them the code would ...
1
vote
1answer
98 views
'CompiledFFI' instead of 'FFI' when including 'cairocffi'
I want to use Pango with the Cairo backend in Python 3. I chose 'cairocffi' for reasons that are not relevant here. I am trying to run the example in the documentation. It calls Pango through CFFI. ...