从C++代码返回的字符串填充有随机值

2024-10-01 07:39:39 发布

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

我试图为一些C++代码编写一个Cython包装器。当我编译并执行C++文件时,输入字符串被正确翻译。输出示例:

T*GRSLR*L
RKVDLCDSL
[...]

但是,当我在Python中从Cython包装器运行代码时,字符串填充如下:

b'T*GRSLR*R\x8f\x96\x01'
b'RKVDLCDSR\x8f\x96\x01'
[...]

有时这只发生在一个序列上,有时发生在所有序列上。你知道吗

我使用下面的Cython代码与我的C++代码进行接口:

# file: sixframe.pyx
from libcpp.string cimport string
from libcpp.vector cimport vector

cdef extern from "six_frame.cpp":
    cdef void initCodonTable()
    cdef vector[string] sixFrame(string seq)

def six_frame(bytes seq):
    return sixFrame(seq)

initCodonTable()

我的C++函数的签名是:

// file: six_frame.cpp
vector<string> sixFrame(string seq){
    ...
}

这个错误看起来好像我使用了错误的类型,并且没有正确检测到字符串的结尾。我怀疑错误是在Cython代码中,因为C++可执行文件是按照预期的方式工作的。有人知道这里出了什么问题吗?你知道吗


Tags: 字符串代码fromstring错误frameseqcython