Cythonize 以 'fatal error C1002: compiler is out of heap space in pass 2' 结尾

标签 cython cythonize

我尝试对 .py 脚本进行 cythonize。它是一个 PyQt5 gui,带有大量 QToolButtons 和一个有效的 EventFilter。 c 模块构建成功,但是编译失败,出现以下错误:

d:\stuff\mapform2a.c(11338) : fatal error C1002: compiler is out of heap space in pass 2 LINK : fatal error LNK1257: code generation failed error: command 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\x86_amd64\link.exe' failed with exit status 1257

编译器来自 Visual Studio 2019。Python 3.5.5(是的,旧的,我知道,但我有原因......)。

尝试“cythonize -i script.py”时有什么方法可以增加堆空间吗?

Cython 文档对此确实不清楚(至少对于非 C 专家来说......)

编辑 完整日志如下:

C:\temp\MapForm>python setup.py build_ext --inplace Compiling MapForm2A.py because it changed. [1/1] Cythonizing MapForm2A.py C:\Anaconda3\lib\site-packages\Cython\Compiler\Main.py:369: FutureWarning: Cython directive 'language_level' not set, using 2 for now (Py2). This will change in a later release! File: C:\temp\MapForm\MapForm2A.py tree = Parsing.p_module(s, pxd, full_module_name) running build_ext building 'MapForm2A' extension creating build creating build\temp.win-amd64-3.5 creating build\temp.win-amd64-3.5\Release C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\x86_amd64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -IC:\Anaconda3\include -IC:\Anaconda3\include "-IC:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE" "-IC:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\ATLMFC\INCLUDE" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\shared" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\winrt" /TcMapForm2A.c /Fobuild\temp.win-amd64-3.5\Release\MapForm2A.obj MapForm2A.c creating C:\temp\MapForm\build\lib.win-amd64-3.5 C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\x86_amd64\link.exe /nologo /INCREMENTAL:NO /LTCG /DLL /MANIFEST:EMBED,ID=2 /MANIFESTUAC:NO /LIBPATH:C:\Anaconda3\libs /LIBPATH:C:\Anaconda3\PCbuild\amd64 "/LIBPATH:C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\LIB\amd64" "/LIBPATH:C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\ATLMFC\LIB\amd64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.18362.0\ucrt\x64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.18362.0\um\x64" /EXPORT:PyInit_MapForm2A build\temp.win-amd64-3.5\Release\MapForm2A.obj /OUT:build\lib.win-amd64-3.5\MapForm2A.cp35-win_amd64.pyd /IMPLIB:build\temp.win-amd64-3.5\Release\MapForm2A.cp35-win_amd64.lib MapForm2A.obj : warning LNK4197: export 'PyInit_MapForm2A' specified multiple times; using first specification Creating library build\temp.win-amd64-3.5\Release\MapForm2A.cp35-win_amd64.lib and object build\temp.win-amd64-3.5\Release\MapForm2A.cp35-win_amd64.exp Generating code c:\temp\mapform\mapform2a.c(7545) : fatal error C1002: compiler is out of heap space in pass 2 LINK : fatal error LNK1257: code generation failed error: command 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\x86_amd64\link.exe' failed with exit status 1257

我只能补充一点,在引发 C1002 异常之前,该进程在“生成代码”消息上停留了大约 90 秒。

安装文件相当标准:

from setuptools import setup
from Cython.Build import cythonize

setup(
    ext_modules = cythonize("MapForm2A.py")
)

该模块是纯 PyQt5(pyuic5 输出),没有其他依赖项,如果不使用 Cython 直接解释,则可以正常工作。

编辑:解决方案(也许有人需要它)。 感谢@DavidW(在下面的评论中进行讨论)。

Setup.py 必须按以下方式修改:

from distutils import _msvccompiler
_msvccompiler.PLAT_TO_VCVARS['win-amd64'] = 'amd64'

from setuptools import setup
from Cython.Build import cythonize

setup(
    ext_modules = cythonize("MapForm2A.py"),
)

前两行强制使用 64 位工具链。

最佳答案

为了对评论中解决的问题提供更多解释:基本问题似乎是您正在编译一些大型且复杂的内容,并且 MSVC 在链接步骤中耗尽了内存。

微软有a page about this error其中建议了许多选项,其中主要的一个是使用 64 位编译器。 (请注意,这与您编译的是 32 位还是 64 位模块无关 - 它只是编译器可执行文件的选择)

编译 Python 扩展模块(尤其是 setup.py)时,编译器设置通常由 distutils 选择。不幸的是,看起来 distutils 选择强制使用 32 位编译器(请参阅 https://github.com/python/cpython/blob/e488e300f5c01289c10906c2e53a8e43d6de32d8/Lib/distutils/_msvccompiler.py#L160 )。

我的建议是深入了解 setup.py 顶部的 distutils 内部结构(在进行任何实际设置之前)以覆盖此设置

from distutils import _msvccompiler
_msvccompiler.PLAT_TO_VCVARS['win-amd64'] = 'amd64'

本质上,您真正要做的就是将选项 amd64 传递给 Microsoft 提供的用于设置编译器的 vcvarsall.bat 脚本,从而获得 64 位编译器构建 64 位扩展。

关于Cythonize 以 'fatal error C1002: compiler is out of heap space in pass 2' 结尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65197248/

相关文章:

python - 包装一个函数,该函数返回一个指向带有 ctypes 的 python 对象的指针

cython - 对 cython 类使用深度复制函数的问题

python - 使用cython将c++中的helloworld包装到python中时出错

python - 如何使用 pybrain 等外部 python 库在 cython 中编译我的 python 代码

python - 在 "pip3 install cython"之后在 Ubuntu 22.04 上找不到 cythonize

multithreading - Prange 减慢 Cython 循环速度

cython - 将python对象转换为cython指针

python - 在 Cython 中使用 C+ +'s ` str.erase()`

python - 我得到一个 AttributeError : module 'collections' has no attribute 'Iterable' when I try to install libraries using PIP

python - Cython: fatal error : 'numpy/arrayobject.h' 文件未找到,使用 numpy