I want to use python C API to escape a string. The string would be later used in a python eval as an argument for a python method, so currently I'm using this construct:
char* Escape(char* string) {
PyObject *var = PyUnicode_FromString(string);
free(string);
PyObject *varRepr = PyObject_Repr(var);
char* str = PyUnicode_AsUTF8(varRepr);
Py_DECREF(var);
Py_DECREF(varRepr);
return str;
}
Is there a more effective/better way?