在编译扩展types时validation兼容性,并在cdef中使用它们
常问问题:
- 为什么在Cython编译中的其他错误指向特定的错误行,而这不是?
在更新之前:
由于难以编译扩展types,如下面的'will not compile'链接所引用的,认为AssertionError与扩展types相关(在其实例化被转移到主pyx文件之后)。
有人从这个论坛发帖说:“如果你真的想要扩展types,并希望在你的代码中声明他们的types,你需要在.pxd文件中声明他们的C属性。
如果在这种情况下是这样的话,问题是在使用pxd文件时,pyx文件将不能编译 。 在一个pyx文件中编译扩展types时没有错误,但是接收到错误extTypeName is not a type identifier
在导入后使用扩展types时extTypeName is not a type identifier
。
我在评论中被告知,扩展types可以在pyx文件中用主函数使用它们来定义。 这样做,我收到一个错误,我不知道如何跟踪( AssertionError: unexpected type int and base type tuple object for indexing
)。 完整的追溯在下面发布。
很难追查错误
当试图用在同一个pyx文件中声明的扩展types进行编译时的追溯(我不知道是什么原因):
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
我可以从你的信息中得到答复。
- 在Cython中使用Python对象是可能的,但是一旦你
cdef
某些部分,就会受到限制。 尝试通过“un” – 制定字典。 - 你有错误表明你有一个expression式是
x[y]
,其中x
是一个元组,y
是一个int。 原则上它应该工作正常(用int int索引一个元组就可以)。 所以你可以看看Cython中的方括号来找出它来自哪里。 - 你提到已经删除了pxd文件。 你有没有把扩展类/其他types定义的完整声明移动到pyx文件?
扩展types在编译时不是问题(至less在与主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])被curIRreplace时, AssertionError
停止。
- 正如注释中所述,pyx文件中的主函数不能用cdef定义,因为它是在python脚本中调用的。
Pierre de Buyl在评论中的build议非常有帮助:“删除潜在问题variables的cdef声明,直到错误得到解决。 即在Python中可能没有错误的地方,Cython中可能会发现一个不兼容的地方
- 无法设置静态对象字段的值(错误LNK2001:无法parsing的外部符号)
- 一个variables的c ++多重定义
- 编译器错误C3493:'func'不能隐式捕获,因为没有指定默认捕获模式
- 为什么C#编译器允许在IEnumerable <T>和TAlmostAnything之间进行显式转换?
- ios – parsingNSObjCRuntime,NSZone和NSObject中的问题
- 为什么我用@Override获得“必须重写超类方法”?
- 什么是“未声明的标识符”错误,我该如何解决?
- 错误:安装脚本退出错误:命令“x86_64-linux-gnu-gcc”失败,退出状态为1
- Swift类错误:属性未在super.init调用初始化