Error handling. Python embedded into a C++ app.

Wolfram Wolfram at invalid.invalid
Tue Nov 28 17:23:19 CET 2006

I have a problem with displaying errors in an embedded situation.

The "main program" I want to embed Python into is a windows, MFC,
non-console, C++ application. My issue is that I have not been able to
"catch" error messages from python, for example syntax errors. 

PyRun_SimpleFile() crashed, probably due to incompatible FILE
structures. So I am using PyRun_SimpleString to call the python
"execute" command to execute a *.py file. As a test for stdout I can
do a "print" in the *.py and I test stderr using an on-purpose name

Here is what I tried:

- Use AllocConsole and 
	freopen("CON", "w", stdout);
  freopen("CON", "w", stderr);
to redirect stderr and stdout to a new console window.
The C++ stderr/stdout is successfully redirected before I start python
with  Py_Initialize(), but Python does not output into it at all. In
case it is a "not flushed yet" issue, I even added a Py_Finalize()
afetr executing the *.py file.
- I used a TKInter based solution found on the net
- I reopened stderr and stdout in C++ to a file. It always stays at 0
- I tried to reset stderr in the python code (sorry, forgot details).

What is the best way to access Python error messages?
I prefer a C++ to a python way as I currently can debug C++ but not
Is there one way I can use to "catch" exceptions, "runtime errors" and
"syntax errors" in case there is a difference between them? Sorry for
the newbie questions but neither a look into my Python-book nor onto
google helped.

Bye bye,
Wolfram Kuss.

More information about the Python-list mailing list