[Python-Dev] Evil reference cycles caused Exception.__traceback__

Guido van Rossum guido at python.org
Mon Sep 18 10:52:27 EDT 2017

On Mon, Sep 18, 2017 at 2:48 AM, Antoine Pitrou <solipsis at pitrou.net> wrote:

> On Mon, 18 Sep 2017 11:31:12 +0200
> Victor Stinner <victor.stinner at gmail.com> wrote:
> >
> > Ideally, CPython 3.x should never create reference cycles. Removing
> > Exception.__traceback__ is the obvious "fix" for the issue. But I
> > expect that slowly, a lot of code started to rely on the attribute,
> > maybe even for good reasons :-)
> The real issue is not reference cycles, but the fact that a traceback
> frame keeps all locals alive.  When the frame's execution is finished,
> that information is practically almost always useless, but is kept for
> the rare cases of debugging (and perhaps dubious features such as
> some of py.test's magic).  So we're constantly paying the price of this
> minor, albeit probably useful (*), debugging-related feature with
> annoying object lifetime issues that are sometimes quite difficult to
> track down and solve.
> (*) (I've hardly ever used pdb so I don't know if people often examine
> the locals of finished frames, but I suppose some do)

Yes, that's what `pdb.pm()` is for.

> Perhaps it would be useful to have a way to tell the interpreter to
> automatically clear all locals on frames when they have finished
> executing.  It could be a thread-local setting (or, better, a PEP 550
> context variable setting).

In Python 2 the traceback was not part of the exception object because
there was (originally) no cycle GC. In Python GC we changed the awkward
interface to something more useful, because we could depend on GC. Why are
we now trying to roll back this feature? We should just improve GC. (Or
perhaps you shouldn't be raising so many exceptions. :-)

--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20170918/63719445/attachment.html>

More information about the Python-Dev mailing list