Greg Ewing 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:
try: raise Exception except Exception, reason: pass
or by using slicing:
try: raise Exception except Exception, reason: errorclass, errorobject = sys.exc_info()[:2] pass
If you do need to access them, make sure you clean up the reference as soon as you can:
try: 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 available.
Also: What's the use case for creating traceback objects outside the Python interpreter core ?