FPE: Add bindings to exception tracebacks.

Discussion in 'Python' started by Nathan, Feb 19, 2007.

  1. Nathan

    Nathan Guest

    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 <module>
    # With bindings:
    # f = <function f at 0x300f12f0>
    # Source:
    f(42)
    File "/home/n/tmp/demo-bindann.py", line 11, in f
    # With bindings:
    c = 42
    # g = <function g at 0x300f1330>
    # 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 <lambda>
    # With bindings:
    z = 42
    # Source:
    return (lambda z: z+'foo')(x)
    TypeError: unsupported operand type(s) for +: 'int' and 'str'
     
    Nathan, Feb 19, 2007
    #1
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.