1

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;
}

1 Answer 1

2

The trick is, of course, to convert from unicode with PyUnicode_AsEncodedString(t,"utf-8","strict") before applying PyBytes_AsString().

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.