How to get a char* from a PyObject which points to a string

python, python-3.x, python-c-api

Solution

First you encode it, then you retrieve it. Don't forget to decref the temporary.

Problem

How can I get a char* from a PyObject which points to a string. For example, this is the python script, ``` Test.Connect("272.22.20.65", 1234) ``` and this is the C++ code, ``` 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); ``` I want to get that IP address as a char* (GetAsString is a dummy function :D ). Please note that I'm using Python 3.1. P.S. I don't think this question got the correct answer , since there is no PyStringObject or PyString_AsString in Python 3. Isn't it ?

Original source

Related problems