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

W

Wolfram

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
error.


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
bytes.
- 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
python.
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.
 
W

Wolfram

You would use try: and then on the next line except:

Thanks for the idea, but it did not help. I can not wrap every pythion
line in python, so I wrote the following code on the C++ side:

-------------- snip -----------------
try
{
int ret = PyRun_SimpleString ( p ) ;
if(ret==-1)
{
::MessageBox(0, "Error in Python call!", "", 0);
}
else
{
assert(ret == 0);
}
}
catch( ...) {
::MessageBox(0, "exception", "", 0 );
}
-------------- snip -----------------


I call this where "p" is an "execute" of a *.py file containing an
error. I get the "Error in Python call!", but not the "exception".
Any help, including links to information, welcome.


TIA, Wolfram Kuss.
 
W

Wolfram

Maybe of interest to people loking for a solution:

I solved my issue by changing the program retroactively from a pure
MFC app to a console one using this procedure:
http://homepage3.nifty.com/ysflight/mfcconsole/mfcconsole.html

I am still not sure why all of my other attempts failed, but most
somehow changed stderr after the program started and before the python
initialisation call. Maybe by binding in the python libraries some
initialisation is done automatically before the first "user" C++ line
is called and so any change done by the C++ code ignored?

Oh well, it works now, even if with a kludge (I always need to have a
console, even if I do not use Python).

Bye bye, Wolfram Kuss.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top