[Python-Dev] Re: Object finalization for local (ie function)scopes

Oliver Schoenborn oliver.schoenborn at utoronto.ca
Sat Jun 12 20:00:35 EDT 2004

From: "Martin v. Löwis" <martin at v.loewis.de>
> David Abrahams wrote:
> > I'm sure I missed something important along the way here, but the fact
> > that you can generate Java bytecode for Python's "finally" in Jython,
> > it seems to me, _proves_ that you can also generate code to that will
> > be executed at the end of a function even in the presence of
> > exceptions.
> Certainly. However, I'm uncertain whether you find out, in this finally
> block, what objects require this finally-ization. Part of this
> uncertainty comes from my failure to understand how the proposed
> mechanism works in the first place - ie. what is the precise set of
> objects that should be finally-ized.

Those objects that have "registered" themselves to the "cleaner" require
finally-ization. Hence, if no objects registered themselves, there is only
the overhead of an empty finally. The implementation details can worry about
how registration takes place, and who does the cleanup. In scope.py,
"registration" is handled by derivation from class NeedsFinalization -- 
which I can eventually rename so please let's not get stuck on terminology
at this stage. The cleaner is a function called ScopeGuardian.

> If any algorithm for such a
> mechanism would need to find out what objects have been created while
> the function was running, and invoke finally-ization on those, that
> may not be possible to implement: you can't get hold of all objects
> that have been created in Java, AFAIK (1).

The technique, as state above, does NOT require that.

> If it involves looking at
> all local variables, it might be implementable, but might cause a
> significant slowdown even if the feature is not used.

The technique, as state above, does NOT require that either.


More information about the Python-Dev mailing list