Reputation: 3776
Euler Math Toolbox (www.euler-math-toolbox.de) has an interface with Python 2. Now it should interface with Python 3. But I seem to be unable to read output from Python. Below is a simple example of what I am trying to do. The code fails in the last step, PyBytes_AsString(). Probably, I am completely off track here.
Please advice.
#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;
}
Upvotes: 1
Views: 1054
Reputation: 3776
The trick is, of course, to convert from unicode with PyUnicode_AsEncodedString(t,"utf-8","strict") before applying PyBytes_AsString().
Upvotes: 2