Python Forum
Pipe traceback to a C program
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pipe traceback to a C program
#1
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:

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


Possibly Related Threads…
Thread Author Replies Views Last Post
  BrokenPipeError: [Errno 32] Broken pipe throwaway34 6 19,083 May-06-2021, 05:39 AM
Last Post: throwaway34
  2 or more processes on the write end of the same pipe Skaperen 4 6,006 Sep-27-2020, 06:41 PM
Last Post: Skaperen
  multiprocessing Pipe.poll very slow seandepagnier 0 3,178 Mar-09-2020, 03:10 AM
Last Post: seandepagnier
  faster socket with multiprocessing.Pipe sixteenornumber 2 8,053 Dec-10-2016, 06:52 PM
Last Post: sixteenornumber
  Filtering an interactive CLI command via pipe(s) Skaperen 2 5,099 Nov-23-2016, 09:17 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020