473,785 Members | 2,209 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_SimpleFil e() crashed, probably due to incompatible FILE
structures. So I am using PyRun_SimpleStr ing 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
3 4010
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_SimpleFil e() crashed, probably due to incompatible FILE
structures. So I am using PyRun_SimpleStr ing 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
"Er*********@ms n.com" <Er*********@ms n.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_SimpleStr ing ( 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4382
by: AIM | last post by:
Error in msvc in building inheritance.obj to build hello.pyd Hello, I am trying to build the boost 1.31.0 sample extension hello.cpp. I can not compile the file inheritance.cpp because the two files containing some templates: adjacency_list.hpp and mem_fn.hpp can not compile. Does anyone have any solutions?
13
4486
by: Thelma Lubkin | last post by:
I use code extensively; I probably overuse it. But I've been using error trapping very sparingly, and now I've been trapped by that. A form that works for me on the system I'm using, apparently runs into problems on the system where it will actually be used, and since I used so little error-trapping it dies very ungracefully. I will of course try to fix whatever is causing the error and add error-trapping to the functions where the...
0
2028
by: poojamangal | last post by:
I want to upload images or pdf files. but i m unable to do so. i got error. please help me to sort it out.. my code is: <% 'on error resume next Class FreeASPUpload Public UploadedFiles Public FormElements Private VarArrayBinRequest
1
3051
by: zaley | last post by:
Py_Finalize ERROR! In my C++ program ,python is embeded . I create one win thread to run embedded Python code . So at the begin of thread function I call "Py_Initialize" and at the end of thread function call "Py_Finalize" . But after I began thread several times,the program crashed in function "Py_Finalize". I can see the error occured at function "PyObject_ClearWeakRefs" when "Py_Finalize" called "type_dealloc";
0
9646
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9484
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10350
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10157
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10097
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6742
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5518
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4055
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.