Jun-22-2020, 03:07 PM
(This post was last modified: Jun-22-2020, 04:05 PM by Gribouillis.)
I try to read a string from a python object in the C-API but seem to be incapable to find the documentation on how to do that properly in Python 3. The problem could be Unicode or any other issue I am not aware of. I had a solution for Python 2.
So what is wrong with the following code? It fails in the last step with out==0.
---------------------------------
So what is wrong with the following code? It fails in the last step with out==0.
---------------------------------
#include <stdio.h> #include <Python.h> const char* pystart = "t = \"test\""; int main() { printf("Test Programm for Python 3.8\n"); Py_Initialize(); PyObject* pModule = PyImport_AddModule("__main__"); //create main module if (!pModule) { printf("Could not create main module in Python."); return 0; } if (PyRun_SimpleString(pystart) == -1) { printf("Could not run pystart."); return 0; } PyObject* t = PyObject_GetAttrString(pModule, "t"); if (!t) { printf("Could not get t."); return 0; } char* out = PyBytes_AsString(t); if (!out) { printf("Could not get the string."); return 0; } printf(out); return 1; }