[Python-Dev] Reference cycles in Exception.__traceback__
Victor Stinner
victor.stinner at gmail.com
Fri Mar 7 00:38:27 CET 2014
Hi,
2014-03-06 16:19 GMT+01:00 Victor Stinner <victor.stinner at gmail.com>:
> By the way, here is my test script to try to create a lightweight
> traceback object without references to locals:
> https://bitbucket.org/haypo/misc/src/tip/python/suppress_locals.py
>
> It works if there is no chained exception.
I updated my proof-of-concept to support Exception.__context__,
Exception.__cause__ and Exception.__suppress_context__:
https://bitbucket.org/haypo/misc/src/tip/python/suppress_locals.py
It creates a lightweight "view" of an exception only keeping
information needed to format a traceback.
> The problem is to build something working with the traceback module. I
> should maybe write my own formatting function reusing some traceback
> functions.
I reused as much code as possible of the traceback module. I only
reimplemented _iter_chain() and _format_exception_iter().
View classes could be simplified, but it would need to modify more
code in the traceback module.
What do you think of my approach? Would it be something useful in
other applications? If yes, I can maybe work on a a better
implementation, and maybe on a PEP, to see how such thing could be
integrated in Python stdlib. It can maybe directly be part of the
traceback module.
I don't know if storing an exception to maybe format it later as a
traceback is a common use case. Usually, exceptions are immediatly
formatted or dropped, no?
Victor
More information about the Python-Dev
mailing list