使用融合类型定义融合类型?

2024-04-18 17:11:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用Cython中的fused types来写这样的东西:

import numpy as np
cimport numpy as np

ctypedef fused T0:
   np.complex128_t
   np.float64_t

ctypedef fused ArrayT0:
   T0[:]
   T0[:,:]

def func(ArrayT0 a):
    cdef T0 ret = a.sum()
    return ret

但是,它不起作用并导致此编译错误:

Compiling bug.pyx because it changed.
[1/1] Cythonizing bug.pyx

Error compiling Cython file:
------------------------------------------------------------
...

ctypedef fused T0:
   np.complex128_t
   np.float64_t

ctypedef fused ArrayT0:
^
------------------------------------------------------------

bug.pyx:8:0: Compiler crash in AnalyseDeclarationsTransform

File 'ModuleNode.py', line 124, in analyse_declarations: ModuleNode(bug.pyx:1:0,
    full_module_name = 'bug')
File 'Nodes.py', line 431, in analyse_declarations: StatListNode(bug.pyx:1:0)
File 'Nodes.py', line 1250, in analyse_declarations: FusedTypeNode(bug.pyx:8:0,
    name = 'ArrayT0',
    types = [...]/2)
File 'Nodes.py', line 1273, in analyse: FusedTypeNode(bug.pyx:8:0,
    name = 'ArrayT0',
    types = [...]/2)

Compiler crash traceback from this point on:
  File "/home/pierre/.pyenv/versions/3.7.2/lib/python3.7/site-packages/Cython/Compiler/Nodes.py", line 1273, in analyse
    return PyrexTypes.FusedType(types, name=self.name)
  File "/home/pierre/.pyenv/versions/3.7.2/lib/python3.7/site-packages/Cython/Compiler/PyrexTypes.py", line 1636, in __init__
    for subtype in t.types:
AttributeError: 'MemoryViewSliceType' object has no attribute 'types'

我的密码有问题吗?这不管用吗?你知道吗

请注意,这个简单的示例可以很好地工作:

import numpy as np
cimport numpy as np

ctypedef fused T0:
   np.complex128_t
   np.float64_t

def func(T0[:, :] a):
    cdef T0 ret = a.sum()
    return ret

Tags: nameinpynplinebugcythonfile