I'm trying to embed Python in a C program. It works fine if I simply want to execute something simple. Here's the C code:
In the docs I've read that there is no way to get the error information with this high level embedding. If the Python line I try to execute is wrong, I do get the traceback printed onto my console. Is there absolutely no way to pipe this information to the C program? I guess not, but I'm just hoping.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#define PY_SSIZE_T_CLEAN #include <Python.h> / * compile with gcc - I / usr / include / python3. 11 - o embed_python embed_python.c - L / usr / lib - lpython3. 11 * / int main( int argc, char * argv[]) { int err; wchar_t * program = Py_DecodeLocale(argv[ 0 ], NULL); if (program = = NULL) { fprintf(stderr, "Fatal error: cannot decode argv[0]\n" ); exit( 1 ); } / / Py_SetProgramName(program); / * optional but recommended * / Py_Initialize(); err = PyRun_SimpleString( "print(\"hello!\")" ); if (Py_FinalizeEx() < 0 ) { exit( 120 ); } PyMem_RawFree(program); return 0 ; } |