Py_BuildValue返回字符串的问题

apexMing 2016-11-17 01:35:36
做一个C的库的python封装,测试下C返回字符串给python,但结果得到的是整数,不知道为啥。
C代码如下:

static PyObject *
Extest_hello(PyObject *self, PyObject *args) {
int res;
int id;
PyObject* retval;
//char* data=hello();
retval = (PyObject *)Py_BuildValue("s", "hello");
return retval;
}
static PyMethodDef
ExtestMethods[] =
{"hello", Extest_hello, METH_VARARGS},
{NULL, NULL},
};

void initExtest() {
Py_InitModule("extest", ExtestMethods);
}


调用的python如下:

#!/usr/bin/python
from ctypes import *
import os
import ctypes

extest = cdll.LoadLibrary(os.getcwd() + '/build/lib.linux-x86_64-2.7/extest.so')
res = extest.hello()
print res

返回结果如下
[root@bogon:pyt_c]$ python test.py
-1990778765
[root@bogon:pyt_c]$

测试返回整形等都没问题。
...全文
464 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
屎克螂 2016-11-18
  • 打赏
  • 举报
回复
http://blog.csdn.net/warriorpaw/article/details/8280055
apexMing 2016-11-17
  • 打赏
  • 举报
回复
现在人这么少吗

37,743

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧