[Python-ideas] feature to make traceback objects usable without references to frame locals and globals

ghazel at gmail.com ghazel at gmail.com
Sat Jun 26 04:48:08 CEST 2010


I'm interested in a feature which allows users to discard the locals
and globals references from frames held by a traceback object.

Currently, traceback objects are used when capturing and re-raising
exceptions. However, they hold a reference to all frames, which hold a
reference to their locals and globals. These are not needed by the
default traceback output, and can cause serious memory bloat if a
reference to a traceback object is kept for any significant length of
time, and there are even big red warnings in the Python docs about
using them in one frame. (
http://docs.python.org/release/3.1/library/sys.html#sys.exc_info ).

Example usage would be something like:

import sys
    t, v, tb = sys.exc_info()
# ... much later ...
raise t, v, tb

Which would be basically a function to do this:

import sys
    t, v, tb = sys.exc_info()
    c = tb
    while c:
        c.tb_frame.f_locals = None
        c.tb_frame.f_globals = None
        c = c.tb_next
# ... much later ...
raise t, v, tb

Twisted has done a very similar thing with their
twisted.python.failure.Failure object, which stringifies the traceback
data and discards the reference to the Python traceback entirely (
) - they also replicate a lot of traceback printing functions to make
use of this stringified data.

It's worth noting that cgitb and other applications make use of locals
and globals in its traceback output. However, I believe the vast
majority of traceback usage does not make use of these references, and
a significant penalty is paid as a result.

Is there any interest in such a feature?


More information about the Python-ideas mailing list