在将工具更新到 python3 时,我在 MacOS 上遇到了这个问题。
print(args.secret)
h = hmac.HMAC(args.secret.decode("hex"),hashes.SHA512(),backend=default_backend())
Run Code Online (Sandbox Code Playgroud)
输出
39830c786a16
h = hmac.HMAC(hex(int(args.secret, 16)), hashes.SHA512(), backend=default_backend())
File "/usr/local/lib/python3.7/site-packages/cryptography/hazmat/primitives/hmac.py", line 31, in __init__
self._ctx = self._backend.create_hmac_ctx(key, self.algorithm)
File "/usr/local/lib/python3.7/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 202, in create_hmac_ctx
return _HMACContext(self, key, algorithm)
File "/usr/local/lib/python3.7/site-packages/cryptography/hazmat/backends/openssl/hmac.py", line 34, in __init__
key_ptr = self._backend._ffi.from_buffer(key)
TypeError: from_buffer() cannot return the address of a unicode object
Run Code Online (Sandbox Code Playgroud)
可能是加密包的问题。一些答案建议重新安装加密包及其依赖项。(cffi、pycparser、六)
pip3 list
Run Code Online (Sandbox Code Playgroud)
six 1.14.0
cryptography 2.8
pycparser 2.19
cffi 1.13.2
Run Code Online (Sandbox Code Playgroud)
还是不行。有人遇到过类似的问题吗?
基本上我想用C编写的Python程序调用函数.
所以(据我所知)我的选择是:
将C模块转换为python二进制模块真的更快吗?
两个解决方案是否都支持向C函数发送python回调?
SIP是生成python接口的好选择吗?还有其他选择吗?
其中任何一个都有其他特殊性吗?
我正在尝试在 windows7(x64) 上使用 pyinstaller(3.0) 生成可执行文件 (x86)。我已经用 pip 安装了 cffi 和其他需要的包。我可以从命令行成功导入 cffi 和 _cffi_backend 模块:
>>> import cffi
>>> import _cffi_backend
>>> cffi.__file__
'C:\\Program Files (x86)\\Python27\\lib\\site-packages\\cffi\\__init__.pyc'
>>> _cffi_backend.__file__
'C:\\Program Files (x86)\\Python27\\lib\\site-packages\\_cffi_backend.pyd'
Run Code Online (Sandbox Code Playgroud)
>
在尝试运行生成的可执行文件时,我收到以下错误:
File "C:\Program Files (x86)\Python27\lib\site-packages\cryptography\hazmat\bindings\openssl\binding.py", line 13, in <module>
from cryptography.hazmat.bindings._openssl import ffi, lib
File "c:\program files (x86)\python27\lib\site-packages\PyInstaller-3.0-py2.7.egg\PyInstaller\loader\pyimod03_importers.py", line 517, in load_module
module = imp.load_module(fullname, fp, filename, ext_tuple)
ImportError: No module named _cffi_backend
Run Code Online (Sandbox Code Playgroud)
您可能会在此处看到完整的启动日志。
顺便说一句,我已经用mingw编译了libcffi库并得到了一个DLL文件。然后复制到库路径之一,我再次在应用程序启动时遇到相同的错误。
我不知道为什么 _cffi_backend 库无法在应用程序启动时加载,即使它存在并且在 python 路径中。我不擅长 python,不幸的是我被困在这将近 2 天,任何建议和指导将不胜感激。
谢谢。
试图在Windows上调用cffi.FFI.verify()会产生以下错误:
distutils.errors.DistutilsPlatformError: Unable to find vcvarsall.bat
Run Code Online (Sandbox Code Playgroud)
我想用mingw编译而不是msvc.我试图通过创建c:\ Python27\Lib\distutils\distutils.cfg使distutils使用mingw
[build]
compiler = mingw32
Run Code Online (Sandbox Code Playgroud)
但这似乎不影响cffi,我仍然得到vcvarsall.bat缺失错误.
那么如何让cffi使用gcc/mingw(或者一般其他的C编译器)呢?
使用 pip 版本 20.2.4、Python 3.8.2 和 Big Sur 11.0.1。
当我运行时pip install cryptography,我收到此错误:
Building wheels for collected packages: cffi
Building wheel for cffi (setup.py): started
Building wheel for cffi (setup.py): finished with status 'error'
ERROR: Command errored out with exit status 1:
command: /Users/xxx/projects/xxx/venv/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/g8/nj6ghjhgj11j84rsjmqqb00000gn/T/pip-install-aufpkz3c/cffi/setup.py'"'"'; __file__='"'"'/private/var/folders/g8/nj6ghjhgj11j84rsjmqqb00000gn/T/pip-install-aufpkz3c/cffi/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /private/var/folders/g8/nj6ghjhgj11j84rsjmqqb00000gn/T/pip-wheel-asotxqfl
cwd: /private/var/folders/g8/nj6ghjhgj11j84rsjmqqb00000gn/T/pip-install-aufpkz3c/cffi/
Complete output (42 lines):
running bdist_wheel
running build
running build_py
creating build
creating build/lib.macosx-10.14.6-x86_64-3.8
creating …Run Code Online (Sandbox Code Playgroud) 如果我pip install cffi,我会得到这个:
building '_cffi_backend' extension
c:\mingw\bin\gcc.exe -mdll -O -Wall -Ic/libffi_msvc -IC:\python27\include -IC:\python27\PC -c c/_cffi_backend.c -o build\temp.win32-2.7\Release\c\_cffi_backend.o
...
(lots of warnings)
...
c/libffi_msvc\win32.c: In function 'ffi_call_x86':
c/libffi_msvc\win32.c:48:2: error: '_asm' undeclared (first use in this function)
c/libffi_msvc\win32.c:48:2: note: each undeclared identifier is reported only once for each function it appears in
c/libffi_msvc\win32.c:48:7: error: expected ';' before '{' token
c/libffi_msvc\win32.c:162:1: warning: control reaches end of non-void function [-Wreturn-type]
error: command 'gcc' failed with exit status 1
Run Code Online (Sandbox Code Playgroud)
这显然是在使用 MinGW。
我还尝试在这里从轮子安装: …
这应该是一项简单的任务,但我找不到如何将标量值的指针传递给 Numba 函数内的 CFFI 函数的方法。使用 传递指向数组的指针不会出现问题ffi.from_buffer。
示例函数
import cffi
ffi = cffi.FFI()
defs="void foo_f(int a,double *b);"
ffi.cdef(defs, override=True)
source="""
#include <stdio.h>;
void foo_f(int a,double *b){
printf("%i",a);
printf(" ");
printf("%f",b[0]);
}
"""
ffi.set_source(module_name="foo",source=source)
ffi.compile()
Run Code Online (Sandbox Code Playgroud)
将指针传递给数组
import numpy as np
import numba as nb
import cffi
ffi = cffi.FFI()
import numpy as np
import ctypes
import foo
nb.cffi_support.register_module(foo)
foo_f = foo.lib.foo_f
@nb.njit()
def Test(a,b):
a_wrap=np.int32(a)
#This works for an array
b_wrap=ffi.from_buffer(b.astype(np.float64))
foo_f(a_wrap,b_wrap)
a=64.
b=np.ones(5)
Test(a,b)
Run Code Online (Sandbox Code Playgroud)
这可以正常工作,但如何修改函数Test以获取标量值b=5.而不修改 …
我有 C 头文件,其中包含一些函数和结构定义。但是该 C 头文件包含多个其他 C 头文件。我需要的一些结构使用其他头文件中的其他结构作为其成员。如何使用 CFFI API 模式而不将每个头文件复制并粘贴到 cdef 和 set_source 中?
假设我在 Linux 计算机上有一个完全空的 Python+Pip+R (pip 19.3.1) 环境,并且我想使用rpy2pip 安装该包。由于我位于公司防火墙后面,因此我将 pip 配置为使用私有存储库。
[global]
index-url = http://private.com/artifactory/api/pypi/PyPI/simple
trusted-host = private.com
Run Code Online (Sandbox Code Playgroud)
现在我执行pip install rpy2,我会得到以下错误:
Couldn't find index page for 'cffi'
Download error on https://pypi.python.org/simple/
Run Code Online (Sandbox Code Playgroud)
cffi因此,pip 尝试通过从官方 PyPi 存储库查找和安装来解决嵌套依赖关系。它完全忽略我配置的存储库。
当我pip install cffi && pip install rpy2一次又一次地运行时,一切都按预期进行。
这是完整的错误输出:
ERROR: Command errored out with exit status 1:
command: /usr/bin/python3 -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-8vuadu93/rpy2/setup.py'"'"'; __file__='"'"'/tmp/pip-install-8vuadu93/rpy2/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-install-8vuadu93/rpy2/pip-egg-info
cwd: /tmp/pip-install-8vuadu93/rpy2/
Complete …Run Code Online (Sandbox Code Playgroud) I am trying to execute a tiny CFFI app using python 3, but I always get Segmentation fault: 11. When I compile the same code using python 2, it works.
api.py
import cffi
ffibuilder = cffi.FFI()
with open('inter.h') as f:
data = ''.join([line for line in f if not line.startswith('#')])
ffibuilder.embedding_api(data)
ffibuilder.set_source("p3", r'''
#include "inter.h"
''')
ffibuilder.embedding_init_code("""
from p3 import ffi
@ffi.def_extern()
def fooz():
return 4
""")
ffibuilder.compile(verbose=True)
Run Code Online (Sandbox Code Playgroud)
client:
from cffi import FFI
ffi = FFI()
ffi.cdef("""
int fooz(void); …Run Code Online (Sandbox Code Playgroud) python-cffi ×10
python ×8
c ×3
pip ×2
artifactory ×1
cffi ×1
cryptography ×1
macos ×1
mingw ×1
numba ×1
numpy ×1
performance ×1
pyinstaller ×1
python-3.x ×1
python-sip ×1
rpy2 ×1