验证编译扩展类型的兼容性并将其与 cdef 一起使用
- 2025-03-26 09:08:00
- admin 原创
- 17
问题描述:
常问问题:
为什么 Cython 编译中的其他错误会指向特定的错误行,而这个却没有?
更新前:
由于扩展类型难以编译(如下面的“无法编译”链接所述),因此认为 AssertionError 与扩展类型有关(在它们的实例化被转移到主 pyx 文件之后)。
该论坛帖子中的某人说“如果您确实想要扩展类型,并想在代码中声明它们的类型,则需要在 .pxd 文件中声明它们的 C 属性。”
如果在这种情况下确实如此,那么问题在于使用 pxd 文件时 pyx 文件无法编译。在一个 pyx 文件中编译扩展类型时没有错误,但在extTypeName is not a type identifier
导入扩展类型后使用它时会收到错误。
评论中告诉我,可以在 pyx 文件中定义扩展类型,并使用主函数使用它们。这样做时,我收到一个错误,我不知道如何跟踪(AssertionError: unexpected type int and base type tuple object for indexing
)。完整的回溯发布在下面。
难以追踪错误
尝试使用同一个 pyx 文件中声明的扩展类型进行编译时进行回溯(我不确定这是否是问题的根源):
Traceback (most recent call last):
File "setup.py", line 37, in <module>
ext_modules = [Extension("HintToCRdict", ["HintToCRdict.pyx"])]
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/core.py", line 151, in setup
dist.run_commands()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/dist.py", line 953, in run_commands
self.run_command(cmd)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/dist.py", line 972, in run_command
cmd_obj.run()
File "/Library/Python/2.7/site-packages/Cython/Distutils/build_ext.py", line 164, in run
_build_ext.build_ext.run(self)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/command/build_ext.py", line 337, in run
self.build_extensions()
File "/Library/Python/2.7/site-packages/Cython/Distutils/build_ext.py", line 171, in build_extensions
ext.sources = self.cython_sources(ext.sources, ext)
File "/Library/Python/2.7/site-packages/Cython/Distutils/build_ext.py", line 324, in cython_sources
full_module_name=module_name)
File "/Library/Python/2.7/site-packages/Cython/Compiler/Main.py", line 688, in compile
return compile_single(source, options, full_module_name)
File "/Library/Python/2.7/site-packages/Cython/Compiler/Main.py", line 638, in compile_single
return run_pipeline(source, options, full_module_name)
File "/Library/Python/2.7/site-packages/Cython/Compiler/Main.py", line 495, in run_pipeline
err, enddata = Pipeline.run_pipeline(pipeline, source)
File "/Library/Python/2.7/site-packages/Cython/Compiler/Pipeline.py", line 365, in run_pipeline
data = phase(data)
File "/Library/Python/2.7/site-packages/Cython/Compiler/Pipeline.py", line 53, in generate_pyx_code_stage
module_node.process_implementation(options, result)
File "/Library/Python/2.7/site-packages/Cython/Compiler/ModuleNode.py", line 131, in process_implementation
self.generate_c_code(env, options, result)
File "/Library/Python/2.7/site-packages/Cython/Compiler/ModuleNode.py", line 359, in generate_c_code
self.body.generate_function_definitions(env, code)
File "/Library/Python/2.7/site-packages/Cython/Compiler/Nodes.py", line 435, in generate_function_definitions
stat.generate_function_definitions(env, code)
File "/Library/Python/2.7/site-packages/Cython/Compiler/Nodes.py", line 1944, in generate_function_definitions
self.generate_function_body(env, code)
File "/Library/Python/2.7/site-packages/Cython/Compiler/Nodes.py", line 1698, in generate_function_body
self.body.generate_execution_code(code)
File "/Library/Python/2.7/site-packages/Cython/Compiler/Nodes.py", line 441, in generate_execution_code
stat.generate_execution_code(code)
File "/Library/Python/2.7/site-packages/Cython/Compiler/Nodes.py", line 441, in generate_execution_code
stat.generate_execution_code(code)
File "/Library/Python/2.7/site-packages/Cython/Compiler/Nodes.py", line 5851, in generate_execution_code
if_clause.generate_execution_code(code, end_label, is_last=i == last)
File "/Library/Python/2.7/site-packages/Cython/Compiler/Nodes.py", line 5894, in generate_execution_code
self.body.generate_execution_code(code)
File "/Library/Python/2.7/site-packages/Cython/Compiler/Nodes.py", line 441, in generate_execution_code
stat.generate_execution_code(code)
File "/Library/Python/2.7/site-packages/Cython/Compiler/UtilNodes.py", line 321, in generate_execution_code
self.body.generate_execution_code(code)
File "/Library/Python/2.7/site-packages/Cython/Compiler/Nodes.py", line 6402, in generate_execution_code
self.body.generate_execution_code(code)
File "/Library/Python/2.7/site-packages/Cython/Compiler/Nodes.py", line 441, in generate_execution_code
stat.generate_execution_code(code)
File "/Library/Python/2.7/site-packages/Cython/Compiler/UtilNodes.py", line 321, in generate_execution_code
self.body.generate_execution_code(code)
File "/Library/Python/2.7/site-packages/Cython/Compiler/Nodes.py", line 6402, in generate_execution_code
self.body.generate_execution_code(code)
File "/Library/Python/2.7/site-packages/Cython/Compiler/Nodes.py", line 441, in generate_execution_code
stat.generate_execution_code(code)
File "/Library/Python/2.7/site-packages/Cython/Compiler/Nodes.py", line 5851, in generate_execution_code
if_clause.generate_execution_code(code, end_label, is_last=i == last)
File "/Library/Python/2.7/site-packages/Cython/Compiler/Nodes.py", line 5894, in generate_execution_code
self.body.generate_execution_code(code)
File "/Library/Python/2.7/site-packages/Cython/Compiler/Nodes.py", line 441, in generate_execution_code
stat.generate_execution_code(code)
File "/Library/Python/2.7/site-packages/Cython/Compiler/UtilNodes.py", line 89, in generate_execution_code
self.body.generate_execution_code(code)
File "/Library/Python/2.7/site-packages/Cython/Compiler/Nodes.py", line 441, in generate_execution_code
stat.generate_execution_code(code)
File "/Library/Python/2.7/site-packages/Cython/Compiler/Nodes.py", line 6015, in generate_execution_code
self.body.generate_execution_code(code)
File "/Library/Python/2.7/site-packages/Cython/Compiler/Nodes.py", line 441, in generate_execution_code
stat.generate_execution_code(code)
File "/Library/Python/2.7/site-packages/Cython/Compiler/Nodes.py", line 6192, in generate_execution_code
self.body.generate_execution_code(code)
File "/Library/Python/2.7/site-packages/Cython/Compiler/Nodes.py", line 441, in generate_execution_code
stat.generate_execution_code(code)
File "/Library/Python/2.7/site-packages/Cython/Compiler/Nodes.py", line 4786, in generate_execution_code
self.generate_rhs_evaluation_code(code)
File "/Library/Python/2.7/site-packages/Cython/Compiler/Nodes.py", line 5073, in generate_rhs_evaluation_code
self.rhs.generate_evaluation_code(code)
File "/Library/Python/2.7/site-packages/Cython/Compiler/ExprNodes.py", line 699, in generate_evaluation_code
self.generate_subexpr_evaluation_code(code)
File "/Library/Python/2.7/site-packages/Cython/Compiler/ExprNodes.py", line 714, in generate_subexpr_evaluation_code
node.generate_evaluation_code(code)
File "/Library/Python/2.7/site-packages/Cython/Compiler/ExprNodes.py", line 699, in generate_evaluation_code
self.generate_subexpr_evaluation_code(code)
File "/Library/Python/2.7/site-packages/Cython/Compiler/ExprNodes.py", line 714, in generate_subexpr_evaluation_code
node.generate_evaluation_code(code)
File "/Library/Python/2.7/site-packages/Cython/Compiler/ExprNodes.py", line 705, in generate_evaluation_code
self.generate_result_code(code)
File "/Library/Python/2.7/site-packages/Cython/Compiler/ExprNodes.py", line 3737, in generate_result_code
self.type, self.base.type)
AssertionError: unexpected type int and base type tuple object for indexing
解决方案 1:
根据我从您的消息中得到的信息进行回复。
在 Cython 中使用 Python 对象是可能的,但只要您使用
cdef
某些部分,就会受到限制。尝试通过“取消”-cdef 字典。您的错误表明您有一个表达式,其中
x[y]
是x
一个元组和y
一个 int。原则上它应该可以正常工作(使用 int 索引元组应该没问题)。因此,您可以查看 Cython 中的方括号以找出它的来源。您提到删除了 pxd 文件。那么,您是否将扩展类/其他类型定义的完整声明移至 pyx 文件?
解决方案 2:
扩展类型在编译中不是问题(至少在与主 cdef 函数相同的 pyx 文件中声明时);错误出现在其他地方。
已解决:
AssertionError 是由对该元组的简单索引访问引起的。
其中元组声明如下:
cdef:
tuple curIRs
...并在嵌套循环中设置为:
for deriv1 in xrange(len(L1)):
for deriv2 in xrange(len(L2)):
curIRs = (deriv2, deriv1)
...并访问元组索引:
d1[ix] = (curIRs[0], curIRs[1])
这显然是一个无害的疏忽,它curIRs
不只是存储,而是通过索引访问进行复制。然而,错误发生在这里,原因不明:
当 (curIRs[0], curIRs[1]) 被 curIRs 替换时,AssertionError
停止。
正如评论中所述,pyx 文件中的 main 函数不能用 cdef 定义,因为它是在 python 脚本中调用的。
Pierre de Buyl 在评论中的建议非常有帮助:“删除潜在问题变量的 cdef 声明,直到错误得到解决。”也就是说,在 Python 中可能没有错误的地方,Cython 中可能会发现不兼容问题