Python custom traceback?

2 views
Skip to first unread message

Doug

unread,
Oct 4, 2006, 1:06:45 PM10/4/06
to edupython
I'm interested in exploring a custom traceback error handler, but I
can't figure out if it is possible. I'd like to do something like:

#-----------------------------
from mylibrary import *
x = 7 / 0
#-----------------------------

such that I can control what happens when an unhandled exception is
thrown. I want to put something in mylibrary. Now, I know I can:

#-----------------------------
import sys
try:
x = 7 / 0
except:
custom_handler(sys.exc_type, sys.exc_value, sys.exc_traceback)
#-----------------------------

but I don't want to surround all of my code in a try/except clause. Is
this possible? Are there hooks into the standard interpreter's
unhandled exception handling?

Thanks for any hints,

-Doug

Vern Ceder

unread,
Oct 4, 2006, 1:27:39 PM10/4/06
to edup...@googlegroups.com
I think you might want the traceback module - have you looked at it?

http://docs.python.org/lib/module-traceback.html

Cheers,
Vern

--
This time for sure!
-Bullwinkle J. Moose
-----------------------------
Vern Ceder, Director of Technology
Canterbury School, 3210 Smith Road, Ft Wayne, IN 46804
vce...@canterburyschool.org; 260-436-0746; FAX: 260-436-5137

Doug Blank

unread,
Oct 4, 2006, 2:20:13 PM10/4/06
to Dethe Elza, edup...@googlegroups.com
Ah, yes, that's it! Thanks! -Doug

On 10/4/06, Dethe Elza <de...@livingcode.org> wrote:
You can take a look at sys.excepthook and see if that does what you
need.

For example:

>>> import sys
>>> def myhook(type, value, traceback):
...     print 'type:', type
...     print 'value:', value
...     print 'traceback:', traceback
...
>>> sys.excepthook = myhook
>>> 1/0
type: <type 'exceptions.ZeroDivisionError'>
value: integer division or modulo by zero
traceback: <traceback object at 0x63fd0>


I hope that helps.

--Dethe


On 4-Oct-06, at 10:06 AM, Doug wrote:

>
> I'm interested in exploring a custom traceback error handler, but I
> can't figure out if it is possible. I'd like to do something like:
>
> #-----------------------------
> from mylibrary import *
> x = 7 / 0
> #-----------------------------
>
> such that I can control what happens when an unhandled exception is
> thrown. I want to put something in mylibrary. Now, I know I can:
>
> #-----------------------------
> import sys
> try:
>    x = 7 / 0
> except:
>    custom_handler(sys.exc_type , sys.exc_value, sys.exc_traceback)

> #-----------------------------
>
> but I don't want to surround all of my code in a try/except clause. Is
> this possible? Are there hooks into the standard interpreter's
> unhandled exception handling?
>
> Thanks for any hints,
>
> -Doug
>
>
>
in the Internet Age. --Dean Baker



Jeffrey Elkner

unread,
Oct 4, 2006, 3:00:37 PM10/4/06
to edup...@googlegroups.com
Hi All,

The deadline for submissions of presentation proposals for PyCon 2007 is October 31st. Given how much good came out of getting education meetings at last year's pycon (this list, for starters), I wanted to see if we could do some advanced planning to make this year's pycon even better.

I've got to run (students are coming), so I'll follow this up later...

jeff elkner

Vern Ceder

unread,
Oct 5, 2006, 3:23:16 PM10/5/06
to edup...@googlegroups.com
Definitely count me in for whatever I can do to help out. Of course
we'll want to have the dinner/BOF again, that was very good.

Do we want to suggest an education strand? Well, maybe not quite a
strand, but at least some grouping of ed related talks?

Cheers,
Vern

--

Reply all
Reply to author
Forward
0 new messages