Re: FPE: Add bindings to exception tracebacks.

2007-02-19 Thread Gabriel Genellina
En Mon, 19 Feb 2007 19:47:26 -0300, Nathan <[EMAIL PROTECTED]> escribió:

> Throughout my python development career, I've occasionally made
> various developer tools to show more information about assertions or
> exceptions with less hassle to the programmer.  Until now, these tools
> didn't pass a utility vs pain-to-use threshold.
>
> Now I've created a tool I believe to have passed that threshold, which
> I call "binding annotated exception tracebacks".  In short, this tool
> adds text showing relevant local bindings to each level in a stack
> trace print out.

Something very similar already exists in the standard library, but using a  
very misleading name, "cgitb".
It works much better with a source file (so it can print source lines too)

=== begin tb.py ===
import cgitb
cgitb.enable(format="text")

def f(c):
 d = 2*c
 return g(c)

def g(x):
  return (lambda z: z+'foo')(x)

f(42)
=== end tb.py ===

C:\TEMP>python tb.py

Python 2.5: c:\apps\python\python.exe
Mon Feb 19 22:27:26 2007

A problem occurred in a Python script.  Here is the sequence of
function calls leading up to the error, in the order they occurred.

  C:\TEMP\tb.py in ()
 7
 8 def g(x):
 9  return (lambda z: z+'foo')(x)
10
11 f(42)
f = 

  C:\TEMP\tb.py in f(c=42)
 4 def f(c):
 5 d = 2*c
 6 return g(c)
 7
 8 def g(x):
global g = 
c = 42

  C:\TEMP\tb.py in g(x=42)
 7
 8 def g(x):
 9  return (lambda z: z+'foo')(x)
10
11 f(42)
z undefined
x = 42

  C:\TEMP\tb.py in (z=42)
 7
 8 def g(x):
 9  return (lambda z: z+'foo')(x)
10
11 f(42)
z = 42
x undefined
: unsupported operand type(s) for +: 'int'  
and 'str
'

The above is a description of an error in a Python program.  Here is
the original traceback:

Traceback (most recent call last):
   File "tb.py", line 11, in 
 f(42)
   File "tb.py", line 6, in f
 return g(c)
   File "tb.py", line 9, in g
 return (lambda z: z+'foo')(x)
   File "tb.py", line 9, in 
 return (lambda z: z+'foo')(x)
TypeError: unsupported operand type(s) for +: 'int' and 'str'

-- 
Gabriel Genellina

-- 
http://mail.python.org/mailman/listinfo/python-list


FPE: Add bindings to exception tracebacks.

2007-02-19 Thread Nathan
Hi folks!

Throughout my python development career, I've occasionally made
various developer tools to show more information about assertions or
exceptions with less hassle to the programmer.  Until now, these tools
didn't pass a utility vs pain-to-use threshold.

Now I've created a tool I believe to have passed that threshold, which
I call "binding annotated exception tracebacks".  In short, this tool
adds text showing relevant local bindings to each level in a stack
trace print out.

I consider it to be so useful that it should be part of the standard
library.  I'm not sure the best process to propose this (shall I make
a PEP?  -is this already an FPE?), so I thought I'd start with a
published early/often patch, then point people to it.

I've submitted a patch against the 2.6 head on the sf tracker as
ticket 1654974, or try this url:

http://sourceforge.net/tracker/index.php?func=detail&aid=1654974&group_id=5470&atid=305470

The patch modifies the traceback module.  It's also entirely
reasonable to have this functionality in a new, separate module (and
also it may be implemented in earlier python versions), but for my
personal build I wanted all programs to use this new feature.

Here's an example to clarify.  Consider the following script:

#! /usr/bin/env python2.6

import sys
import traceback

# Install annotated exception printing:
sys.excepthook = lambda t, v, b: traceback.print_exception(t, v, b,
annotate=True)

def f(c):
d = 2*c
return g(c)

def g(x):
return (lambda z: z+'foo')(x)

f(42)


-The output (with the patch of course) is:

Traceback (most recent call last):
  File "/home/n/tmp/demo-bindann.py", line 16, in 
# With bindings:
# f = 
# Source:
f(42)
  File "/home/n/tmp/demo-bindann.py", line 11, in f
# With bindings:
c = 42
# g = 
# Source:
return g(c)
  File "/home/n/tmp/demo-bindann.py", line 14, in g
# With bindings:
x = 42
# Source:
return (lambda z: z+'foo')(x)
  File "/home/n/tmp/demo-bindann.py", line 14, in 
# With bindings:
z = 42
# Source:
return (lambda z: z+'foo')(x)
TypeError: unsupported operand type(s) for +: 'int' and 'str'
-- 
http://mail.python.org/mailman/listinfo/python-list