我正在尝试编写一个带有一些c和一些python部分的模块.我正在使用cython弥补差距.
我想在python中存储我的(非常长的)字符串常量,因为它的语法更好:
const char long_string = "\npart of string\n"
"next part\n"
"last part\n";
与:
long_string = """
part of string
next part
last part
"""
(字符串比这长得多,而且更复杂 - 我不希望每次我想用语法高亮来编辑它时都要添加和删除"s和\n"s.事实上,它们是openCL内核.)
我需要能够使用cython将它们变成c字符串,根据文档我应该只需要:
cdef bytes py_bytes = py_string.encode()
cdef char* c_string = py_bytes
并且没有手动内存管理,c_string只要我保留引用就可以工作py_bytes.
但是,我无法使用简单的printf测试.这是我的cython文件:
cdef extern from "stdio.h":
printf(char* string)
def go():
py_string = """
a complicated string
with a few
newlines.
"""
cdef bytes py_bytes = py_string.encod