如何将字符串从 Python3 传递到 cythonized C++ 函数

gc5*_*gc5 5 c++ cython python-3.x cythonize

我试图了解如何在 Python3 和 cythonized C++ 函数之间传递字符串值。但是我无法使用 Cython 构建库。

特别是我不明白如何声明字符串返回值和字符串参数source.pyx. 对于 int 类型,它可以正常工作。

我在使用 clang 构建期间遇到的错误如下:

candidate function not viable: no known conversion from 'PyObject *' (aka '_object *') to 'char *' for 1st argument
Run Code Online (Sandbox Code Playgroud)

source.pyx的如下:

cdef extern from "source.cpp":
  cdef str fun(str param)

def pyfun(mystring):
  return fun(mystring)
Run Code Online (Sandbox Code Playgroud)

我的source.cpp是:

char * fun(char *string) {
  return string;
}
Run Code Online (Sandbox Code Playgroud)

gc5*_*gc5 5

除了原始代码中的错误之外,我设法使其与以下代码一起工作( Python3 和 C++ 中的类型source.pyx之间的转换):byteschar*

cdef extern from "source.cpp":
  cdef char* fun(char* param)

def pyfun(mystring):
  mystring_b = mystring.encode('utf-8')
  rvalue = fun(mystring_b).decode('utf-8')
  return rvalue
Run Code Online (Sandbox Code Playgroud)

如果内存是malloc在其中使用分配fun的,则还需要释放内存,否则会出现内存泄漏(在使用 C 指针时,始终值得考虑谁拥有内存)。执行此操作的修改版本是:

from libc.stdlib cimport free

# cdef extern as before

def pyfun(mystring):
    cdef char* value_from_fun
    mystring_b = mystring.encode('utf-8')
    value_from_fun = fun(mystring_b)
    try:    
        return value_from_fun.decode('utf-8')
    finally:
        free(value_from_fun)
Run Code Online (Sandbox Code Playgroud)

类型转换的作用与以前相同。


编辑

根据hpaulj在原始问题中的评论,以下是libcpp.string映射 C++std::string中定义的版本<string>

from libcpp.string cimport string

cdef extern from "source.cpp":
  cdef string fun(string param)

def pyfun(mystring):
  mystring_ = mystring.encode('utf-8')
  rvalue = fun(mystring_).decode('utf-8')
  return rvalue
Run Code Online (Sandbox Code Playgroud)