[Python-3000] self-contained exceptions

Guido van Rossum guido at python.org
Tue Jan 2 21:28:35 CET 2007

On 1/2/07, Jim Jewett <jimjjewett at gmail.com> wrote:
> On 1/1/07, Guido van Rossum <guido at python.org> wrote:
> > I believe the main argument *against* having the tb included in the
> > exception is that it would greatly increase the cyclic nature of
> > tracebacks and stack frames, which in turn would cause lots of code to
> > break due to late GC'ing of unclosed files, etc. For example, this
> > code:
> >
> > try:
> >   1/0
> > except Exception, err:
> >   pass
> >
> > will introduce a cycle from err -> traceback -> stack frame -> err,
> > keeping all locals in the same scope alive until the next GC happens.
> This should probably be made explicit in PEP 344.

Feel free to send a patch to me and the primary author (Ping). If Ping
agrees I can check it in.

> I also keep thinking that there ought to be a solution involving weak
> references, though I don't trust quite trust myself on what that
> should be.  (The only obviously correct solution is to make the new
> err->traceback link a weakref and not clean the sys.* interface.)

The problem with a weakref is that it won't keep the tb alive when you
save the exception for later (as is done for instance by the unittest
module). So it is of limited value and could even be confusing. OTOH
it *does* remove the need to call sys.exc_info() in the most common
case, so I don't want to rule it out completely.

--Guido van Rossum (home page: http://www.python.org/~guido/)

More information about the Python-3000 mailing list