catching Python exceptions...

Hi

I was wondering where the code is that catches Python exceptions and writes them out to stdout.
I was wanting to redirect these to a wxLog but couldn't find it in wxPySimpleApp...

Thanks
david

look at the python standard traceback module (pydoc traceback)
i.e.

import traceback
try:
  something bad
except:
  traceback.print_exc()

will result in pretty much the standard python behavior. The traceback module
also has functions to get the trace as a string, which then could be used
with wx.Log

Hope that helps

UC

···

On Friday 29 October 2004 07:59 am, David Fraser wrote:

Hi

I was wondering where the code is that catches Python exceptions and
writes them out to stdout.
I was wanting to redirect these to a wxLog but couldn't find it in
wxPySimpleApp...

Thanks
david

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org

- --
Open Source Solutions 4U, LLC 2570 Fleetwood Drive
Phone: +1 650 872 2425 San Bruno, CA 94066
Cell: +1 650 302 2405 United States
Fax: +1 650 872 2417

Thanks Uwe, I know how to get tracebacks for a particular exception.
But in a wxPython app the tracebacks resulting from events get printed out to the screen and the app continues.
I want to catch *all* these tracebacks, not just catch errors in particular conditions...
Is that just standard Python handling or is there a hook in wxPython somewhere that does it?

Uwe C. Schroeder wrote:

···

look at the python standard traceback module (pydoc traceback)
i.e.

import traceback
try:
    something bad
except:
    traceback.print_exc()

will result in pretty much the standard python behavior. The traceback module
also has functions to get the trace as a string, which then could be used
with wx.Log

Hope that helps

UC

On Friday 29 October 2004 07:59 am, David Fraser wrote:

>Hi

>I was wondering where the code is that catches Python exceptions and
>writes them out to stdout.
>I was wanting to redirect these to a wxLog but couldn't find it in
>wxPySimpleApp...

>Thanks
>david

>---------------------------------------------------------------------
>To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
>For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org

--
Open Source Solutions 4U, LLC 2570 Fleetwood Drive
Phone: +1 650 872 2425 San Bruno, CA 94066
Cell: +1 650 302 2405 United States
Fax: +1 650 872 2417

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org

David Fraser wrote:

Thanks Uwe, I know how to get tracebacks for a particular exception.
But in a wxPython app the tracebacks resulting from events get printed out to the screen and the app continues.
I want to catch *all* these tracebacks, not just catch errors in particular conditions...
Is that just standard Python handling or is there a hook in wxPython somewhere that does it?

Isn't it just sys.stderr? Try making a file object and setting sys.stderr to that. Something like:

import sys
file = open("/tmp/err.txt", "w")
sys.stderr = file

···

--
Paul McNett - http://paulmcnett.com
vcard: http://paulmcnett.com/pm.vcf

David Fraser wrote:

Hi

I was wondering where the code is that catches Python exceptions and writes them out to stdout.
I was wanting to redirect these to a wxLog but couldn't find it in wxPySimpleApp...

It's happening in the C code. Every time control returns from a Python event handler or other callback then the C code in the wrappers will check if there was an exception and will call PyErr_Print() if so.

PyErr_Print will write the traceback to sys.stderr so you can intercept them simply by replaceing sys.stderr with any object that has a write() method. Be careful about threads though since that write function could be called from worker threads.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

How about

app=MainApp(redirect=1,filename='log.txt')

where MainApp is the wx.App object of your application.
That should redirect anything to log.txt - including all exceptions

  Uwe

- --
Open Source Solutions 4U, LLC 2570 Fleetwood Drive
Phone: +1 650 872 2425 San Bruno, CA 94066
Cell: +1 650 302 2405 United States
Fax: +1 650 872 2417

···

On Friday 29 October 2004 03:08 pm, Paul McNett wrote:

David Fraser wrote:
> Thanks Uwe, I know how to get tracebacks for a particular exception.
> But in a wxPython app the tracebacks resulting from events get printed
> out to the screen and the app continues.
> I want to catch *all* these tracebacks, not just catch errors in
> particular conditions...
> Is that just standard Python handling or is there a hook in wxPython
> somewhere that does it?

Isn't it just sys.stderr? Try making a file object and setting
sys.stderr to that. Something like:

import sys
file = open("/tmp/err.txt", "w")
sys.stderr = file

Robin Dunn wrote:

David Fraser wrote:

Hi

I was wondering where the code is that catches Python exceptions and writes them out to stdout.
I was wanting to redirect these to a wxLog but couldn't find it in wxPySimpleApp...

It's happening in the C code. Every time control returns from a Python event handler or other callback then the C code in the wrappers will check if there was an exception and will call PyErr_Print() if so.

PyErr_Print will write the traceback to sys.stderr so you can intercept them simply by replaceing sys.stderr with any object that has a write() method. Be careful about threads though since that write function could be called from worker threads.

Fantastic, thanks Robin. If I get it working I'll put it in the Wiki or something

David

Uwe C. Schroeder wrote:

···

On Friday 29 October 2004 03:08 pm, Paul McNett wrote:

>David Fraser wrote:

>>Thanks Uwe, I know how to get tracebacks for a particular exception.
>>But in a wxPython app the tracebacks resulting from events get printed
>>out to the screen and the app continues.
>>I want to catch *all* these tracebacks, not just catch errors in
>>particular conditions...
>>Is that just standard Python handling or is there a hook in wxPython
>>somewhere that does it?

>Isn't it just sys.stderr? Try making a file object and setting
>sys.stderr to that. Something like:

>import sys
>file = open("/tmp/err.txt", "w")
>sys.stderr = file

How about

app=MainApp(redirect=1,filename='log.txt')

where MainApp is the wx.App object of your application.
That should redirect anything to log.txt - including all exceptions

    Uwe

Thanks, I'll try that too...

David