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



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



Relevant Pages

  • embedded python and interpreter threads
    ... So, I have an amazing, functioning foobar class that embeds python. ... I want to be able to have multiple foobar objects at ... be instantiated collects the stdout and stderr of all the other objects ...
    (comp.lang.python)
  • Re: Python 2.2.1 and select()
    ... of a subprocess (both stdout and stderr, ... Debian Sarge system running Python 2.2.1 it's a no-go. ...
    (comp.lang.python)
  • Re: os.popen on windows: loosing stdout of child process
    ... import sys ... When I run the same python program on a unix box, ... a bit on the web and groups for eg. 'python windows popen stdout lost' ... I'm using the windows version of python 2.5 from activestate. ...
    (comp.lang.python)
  • Re: reading piped input in Windows
    ... >Can anyone point me to a portable solution that works both under Windows ... This will be slower than also supplying the absolute path to python, ... stdout and stderr. ...
    (comp.lang.python)
  • os.popen on windows: loosing stdout of child process
    ... When I use os.popen, I find that under windows, the stdout ... instead of appearing in the DOS window ... When I run the same python program on a unix box, ...
    (comp.lang.python)