Search code examples
pythonstringcharpython-c-api

convert cstring to python string object


I have a function wrapper for c and python. This wrapper will receive 2 string arguments and convert these two into a tuple and pass it to python script. fileName and className are the c string arguments. pFunc is the python function name.

PyObject *pArgs, *pValue,*pFunc;
pValue = PyString_FromString(fileName);
PyTuple_SetItem(pArgs, 0, pValue);
pValue = PyString_FromString(className);
PyTuple_SetItem(pArgs, 0, pValue);
pValue = PyObject_CallObject(pFunc, pArgs);

the error was cannot convert QString to const char*

i have tried the PyUnicode_FromString API, but still doesnt work, and gave the same error

the inputs are strings and i just want to make the PyObject_CallObject work, regardless of what type of pValue is (preferably strings too).... how do i do this...


Solution

  • the error was cannot convert QString to const char*

    So your strings fileName and className are QString objects, not C strings (const char*)? You have to convert the data first, for example using QString::toAscii():

    pValue = PyString_FromString(fileName.toAscii().data());