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

"Martin v. Löwis" martin at v.loewis.de
Sun Jun 13 04:10:12 EDT 2004


Oliver Schoenborn wrote:
> I checked on the jython site, the cvs repository, and the inspect module is
> only partially implemented as a wrapper around the Python inspect module.
> This problem you are having seems related to implementation, not technique.

Hmm. My claim was that your "portable" implementation is not, in fact,
portable. It might be possible to create a portable implementation that
works on different versions of CPython, as well as on versions of
Jython, but I haven't seen one.

> The technique itself can be summarized as follows:
> 
> - Create a function that wraps a try/finally around the function of interest
> - Make class whose instances need "cleanup" automatically tell the function
> in which they were created (the "function of interest" above) that they
> should be cleaned up upon function exit. This requires three things:

I claim that this might not be possible to do across different
implementations of Python. It relies on the ability to look at the local
variables of a caller of a function, and the Python language does not
guarantee support for such a mechanism.

> The inspect module is used in the very last task. The new function attribute
> is only used as a convenient place to store information which is closely
> tied to the function. But there are other possibilities. E.g. instead of
> creating a function attribute, the ScopeGuardian could use a global stack of
> list of objects. An instance of NeedsFinalization would use the top list of
> the stack to add a reference to istelf there.

In that implementation, how would you know how many objects to cleanup?

> It looks like Jython has some parts of python lib implemented natively, some
> borrowed, and some not implemented, based on need and manpower, so the fact
> that the inspect module is not available hardly qualifies the technique as
> "not working". If Jython doesn't use stack, and implementing in terms of
> global var not feasible, *and* there is no other implementation possible in
> Jython, then yeah, I agree, scope.py is not portable to Jython. However that
> doesn't mean that the technique can't be implemented different in Python
> interpreter and Jython interpreter.

Yes, but I have no reason to believe you an implementation is possible
until I have seen an implementation.

It might be that an implementation of the feature would be very
expensive even if not used, in which case the feature also would not
be acceptable for inclusion in Python.

However, this is probably the time to ask for a PEP that properly
describes the API and the semantics of the feature.

As to your original question: Can this be implemented in the C
interpreter directly? The answer is: yes, probably, depending
on what precisely the feature is.

Regards,
Martin




More information about the Python-Dev mailing list