我在使用python 3中的cython编译c扩展时遇到问题。它使用python 2.7进行编译和工作正常,但是在python 3.4.3中,当使用3.4(anaconda发行版)构建时出现以下错误:
python setup.py build_ext --inplace
running build_ext
building 'module' extension
gcc -pthread -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -Iincludes -I/home/user/anaconda/include/python3.4m -c module.cpp -o build/temp.linux-x86_64-3.4/module.o
cc1plus: warning: command line option "-Wstrict-prototypes" is valid for Ada/C/ObjC but not for C++
rawdata.cpp: In function ‘int __pyx_pf_7rawdata_13RawDataReader___cinit__(__pyx_obj_7rawdata_RawDataReader*, PyObject*, int, int)’:
rawdata.cpp:852: error: ‘PyString_AsString’ was not declared in this scope
error: command 'gcc' failed with exit status 1
问题在于我的大多数其他模块都是针对3.4编写的,因此保留2.7并不是一个真正的选择。
如何解决这个问题?
答案 0 :(得分:4)
所以事实证明在python 3中python字符串的处理方式不同,所以对我来说简单的解决方案就是用PyString_AsString(s)
替换s.encode('UTF-8')