Mor*_*eus 3 python python-c-api python-3.x
如何从PyObject中获取指向字符串的char*.例如,这是python脚本,
Test.Connect("272.22.20.65", 1234)
Run Code Online (Sandbox Code Playgroud)
这是C++代码,
static PyObject* Connect(PyObject *self, PyObject *args)
{
PyObject* pIP;
PyObject* pPort;
if (!PyArg_UnpackTuple(args, "Connect", 2, 2, &pIP, &pPort))
{
return NULL;
}
const char* zIP = GetAsString(pIP);
long iPort = PyLong_AsLong(pPort);
Run Code Online (Sandbox Code Playgroud)
我想将该IP地址作为char*(GetAsString是一个虚拟函数:D).请注意我使用的是Python 3.1.
PS我不认为这个问题得到了正确答案,因为Python 3中没有PyStringObject或PyString_AsString.不是吗?