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

M.-A. Lemburg mal at egenix.com
Sat Jun 26 13:03:38 CEST 2010

Greg Ewing wrote:
> ghazel at gmail.com wrote:
>> I'm interested in a feature which allows users to discard the locals
>> and globals references from frames held by a traceback object.

Wouldn't it be better to write safer code and not store
a reference to the traceback object in the first place ?

Working with traceback objects can easily introduce hidden
circular references, so it usually better not access them
at all, if you don't have a need for them:

Either like this:

    raise Exception
except Exception, reason:

or by using slicing:

    raise Exception
except Exception, reason:
    errorclass, errorobject = sys.exc_info()[:2]

If you do need to access them, make sure you clean up
the reference as soon as you can:

    raise Exception
except Exception, reason:
    errorclass, errorobject, tb = sys.exc_info()
    tb = None

> I'd like to take this further and remove the need for
> traceback objects to refer to a frame object at all.
> The standard traceback printout only needs two pieces of
> information from the traceback, the file name and line
> number.
> The line number is already present in the traceback
> object. All it would take is the addition of a file name
> attribute to the traceback object, and the frame reference
> could be made optional.

How would you make that reference optional ?

The frames are needed to inspect the locals and globals
of the call stack and debugging code relies on them being

Also: What's the use case for creating traceback objects
outside the Python interpreter core ?

Marc-Andre Lemburg

Professional Python Services directly from the Source  (#1, Jun 26 2010)
>>> Python/Zope Consulting and Support ...        http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ...             http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
2010-07-19: EuroPython 2010, Birmingham, UK                22 days to go

::: Try our new mxODBC.Connect Python Database Interface for free ! ::::

   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
           Registered at Amtsgericht Duesseldorf: HRB 46611

More information about the Python-ideas mailing list