By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,957 Members | 1,969 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,957 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.
Nov 28 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
You would use try: and then on the next line except: I am not sure
what the best answer is but you could write your errors to a file and
then load them if all else fails.

https://sourceforge.net/projects/dex-tracker

Wolfram wrote:
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.
Nov 29 '06 #2

P: n/a
"Er*********@msn.com" <Er*********@msn.comwrote:
>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.
Nov 29 '06 #3

P: n/a
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/...fcconsole.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.
Dec 2 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.