首先,我将 Cython 0.18 与 Python 2.7.4 一起使用。我遇到了一个相当奇怪的错误,我不知道为什么。这是玩具代码:
from cpython cimport bool
cpdef unsigned int func(char *seq1, char *seq2, bool case_sensitive=True):
print 'seq1', seq1, len(seq1)
print 'seq2', seq2, len(seq2)
print
#take care of case sensitivity
if not case_sensitive:
#this is kinda hacky, but I've gotta assign the lowercased string to a Python object before assigning it back to char *
#see http://docs.cython.org/src/userguide/language_basics.html#caveats-when-using-a-python-string-in-a-c-context
temp = seq1.lower()
seq1 = temp
temp = seq2.lower()
seq2 = temp
print 'seq1', seq1, len(seq1)
print 'seq2', seq2, len(seq2)
print
#trim common characters at the beginning of the words
while len(seq1) > 0 and len(seq2) > 0 and seq1[0] == seq2[0]:
temp = seq1[1:]
seq1 = temp
temp = seq2[1:]
seq2 = temp
print 'seq1', seq1, len(seq1)
print 'seq2', seq2, len(seq2)
print
#handle degenerate cases
if not seq1:
return len(seq2)
if not seq2:
return len(seq1)
这是一个示例调用:
>>> from func import func
>>> print func('TUESDAYs', 'tuesday', False)
现在,我希望看到以下内容:
seq1 TUESDAYs 8
seq2 tuesday 7
seq1 tuesdays 8
seq2 tuesday 7
seq1 s 1
seq2 0
1
但我实际看到的是这样的:
seq1 TUESDAYs 8
seq2 tuesday 7
seq1 tuesdays 8
seq2 tuesday 7
seq1 stdout 6
seq2 tuesday 7
0
这到底是怎么回事?首先,为什么要stdout
输出?为什么我没有得到我应该得到的输出?这是 Cython 错误,还是我只是在这里遗漏了一些微不足道的东西?