验证编译扩展类型的兼容性,并将其与cdef

时间:2017-01-30 20:32:24

标签: python compiler-errors cython

常设问题:

  • 为什么Cython编译中的其他错误指向特定的错误行,而这不是<?li>

更新前:

由于难以编译扩展类型,正如“未赢编译”所述。在下面的链接中,有人认为AssertionError与扩展类型有关(在它们的实例化转移到主pyx文件之后)。

来自此forum post的人说“如果你真的想要扩展类型,并且想在你的代码中声明它们的类型,你需要在.pxd文件中声明它们的C属性。”

如果在这种情况下确实如此,问题是当使用pxd文件时pyx文件won’t compile对我来说。在一个pyx文件中编译扩展类型时没有错误,但是在导入后使用扩展类型时我收到错误extTypeName is not a type identifier

我在comments告诉我,扩展类型可以在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

2 个答案:

答案 0 :(得分:2)

回复我可以从您的信息中获取的内容。

  1. 在Cython中使用Python对象是可能的,但只要cdef某些部分就会受到限制。尝试通过&#34; un&#34; -cdefing the dict。
  2. 您所犯的错误表明您的表达式为x[y],其中x为元组,y为int。原则上它应该可以正常工作(使用int索引一个元组就可以了)。因此,您可以查看Cython中的方括号以找出它的来源。
  3. 您提到删除了pxd文件。您是否已将扩展类/其他类型定义的完整声明移至pyx文件?

答案 1 :(得分:0)

扩展类型在编译时不是问题(至少在与主cdef函数相同的pyx文件中声明时);错误正在其他地方出现。

<强>解决

  1. AssertionError是由对此元组的简单索引访问引起的。
  2. 将元组声明为:

    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替换(curIRs [0],curIRs [1])时,AssertionError停止。

    1. 如评论中所述,pyx文件中的main函数不能用cdef定义,因为它是在python脚本中调用的。
    2. Pierre de Buyl在评论中提出的建议非常有用:'删除潜在问题变量的cdef声明,直到错误得到解决。即,在Python中可能没有错误的地方,可能会在Cython中发现不兼容性