python-cffi

How can use CFFI to call an existing C function given the source code?

大兔子大兔子 提交于 2021-02-06 10:15:07
问题 I have a C source/header file that are part of a bigger project. I would like to test this as a unit, independent of the real project. While it would be possible to do this in C by creating a new project with a different main() , I would like to see if I can use Python (3) and its frameworks (eg. nose) to accelerate the construction of tests, use existing reporting frameworks, etc. I was under the impression that I could do this with CFFI. Here's a sample C file: // magic.c // Implementation

Why does Pip disregard configured repository with nested dependencies?

穿精又带淫゛_ 提交于 2020-02-25 03:41:56
问题 Problem Let us say I have a completely empty Python+Pip+R (pip 19.3.1) environment on a Linux machine and I want to install the package rpy2 with pip. Since I am behind a corporate firewall I configure pip to use a private repository. [global] index-url = http://private.com/artifactory/api/pypi/PyPI/simple trusted-host = private.com Now I execute pip install rpy2 and I will get back the following error: Couldn't find index page for 'cffi' Download error on https://pypi.python.org/simple/ So

Does PyPy support gmpy2?

☆樱花仙子☆ 提交于 2020-01-06 23:52:11
问题 It seems from the discussions issue #60 and issue #40 that PyPy couldn't build gmpy before. All I intend to use currently is the probable prime is_prime code which is conveniently in gmpy2. I get the impression that the more calls to gmpy2 means less efficiency for PyPy. Is using gmpy2 possible currently, or do I have to use something like GMPY_CFFI? The error I get when using pip in PyPy is cannot open include file 'mpir.h' 回答1: You should use GMPY_CFFI. gmpy and gmpy2 rely on too many

ImportError: No module named couchbase._libcouchbase

≯℡__Kan透↙ 提交于 2020-01-06 02:16:26
问题 This only happens for me in Travis under the pypy build. Here's the exact error string: Traceback (most recent call last): File "app_main.py", line 75, in run_toplevel File "app_main.py", line 581, in run_it File "<string>", line 1, in <module> File "tests/test_pycouchbase.py", line 15, in <module> from pycouchbase.utils import * File "pycouchbase/__init__.py", line 8, in <module> from .connection import Connection File "pycouchbase/connection.py", line 3, in <module> from couchbase.bucket

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

天大地大妈咪最大 提交于 2020-01-03 16:53:37
问题 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 sort of wrapper around objects that will call a destructor when the Python object is GC'd, for example something like: my_obj = managed(lib.MyClass_create(), destructor=lib.MyClass_destroy) Or should that destructor logic be in the class's __del__ ? Something like

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

落爺英雄遲暮 提交于 2020-01-02 07:53:48
问题 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 *); ''') ffi_test.compile(verbose=True) This works fine: In [1]: from _test import ffi, lib In [2]: x = lib.test() In [3]: ffi.string(x) Out[3]: b'hello world' In [4]: lib.free(x) However, I could not find anything in the docs whether I actually need to

Accessing cffi enums

这一生的挚爱 提交于 2019-12-23 18:53:34
问题 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, without re-declaring it? Can't find any mentions in the docs. 回答1: Use ffi.dlopen , and access the enum value by qualifying using the return value of the ffi.dlopen : >>> from cffi import FFI >>> ffi = FFI() >>> ffi.cdef('typedef enum {RANDOM,

Parallel drawing with GTK and Cairo in Python 3

℡╲_俬逩灬. 提交于 2019-12-23 17:02:18
问题 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 and does not have problems with GTK and thread-safety. Python's multiprocessing module uses Pickle protocol to communicate between processes. GTK and Cairo objects do not implement the protocol. My solution was to convert the Cairo surface to

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

谁都会走 提交于 2019-12-13 16:40:37
问题 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 "/opt/python/current/app/application.py", line 391, in user_login if bc.check_password_hash(user.password, password): File "/opt/python/run/venv/lib/python2.7/site-packages/flask_bcrypt.py", line 193, in check_password_hash return safe_str_cmp(bcrypt

Declare struct containing time_t field in Python CFFI

限于喜欢 提交于 2019-12-13 14:28:00
问题 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 time of a file): import cffi ffi = cffi.FFI() ffi.cdef(""" // From POSIX struct timespec { time_t tv_sec; long tv_nsec; ...; }; struct stat { struct timespec st_mtim; ...; }; // From "man 2 lstat" int lstat(const char *path, struct stat *buf); """)