MemoryError: Cannot allocate write+execute memory for ffi.callback() requests报错
本机环境:Mac OS 11.2 python3.8
在requests的时候存在问题。
r = requests.get(url, headers=headers, timeout =10)
报的错误是这样的:
MemoryError: Cannot allocate write+execute memory for ffi.callback(). You might be running on a system that prevents this. For more information, see https://cffi.readthedocs.io/en/latest/using.html#callbacks
并且伴随着一堆ssl认证失败。
问题诊断:和ssl相关的某个库没有写和执行权限,经过资料查询初步确定为pyopenssl,openssl是有写和执行权限的,而pyopenssl则没有。我是因为下载了Scrapy,多了个这玩意。
解决办法:
看看自己有没有pyopenssl这个库,如果有则删除之:
执行:
pip3 uninstall pyopenssl
参考:
pyOpenSSL is not compatible with systems that prevent writeable and executable memory pages. You either have to disable the security feature, use another TLS library like the builtin ssl module, or redesign+rewrite pyOpenSSL's callback system.
it's a problem in pyOpenSSL. OpenSSL is fine.
I see how my comment can be understand the wrong way. I was referring to another Python TLS library such as ssl module from Python's standard library. OpenSSL does not require executable+writable memory. The problem only effects pyOpenSSL.
It's an implementation artifact of pyOpenSSL's glue code that wraps OpenSSL's C-API and makes it available for Python. pyOpenSSL uses CFFI, which is a Python interface to libffi. libffi (library for foreign function interface) uses dynamic code creation for dynamic callbacks. Every time the code passes a Python method to OpenSSL, it has to wrap the Python method into machine code, so it looks like a C function to the C-API of OpenSSL. It writes dynamic code to an executable memory page. CFFI calls this old style callbacks.
There are ways to work around the problem, but it's complicated and lots of work. Nobody has contributed a solution yet.