Python vs Java garbage collection?

holger krekel pyth at devel.trillke.net
Mon Dec 30 08:26:34 EST 2002


Isaac To wrote:
> >>>>> "Stuart" == Stuart D Gathman <stuart at bmsi.com> writes:
>     Stuart> Can you think of a way to register a function to get called when
>     Stuart> the current block exits?  If so, then it is pretty
>     Stuart> straightforward to implement the proposal in pure python.  The
>     Stuart> auto function simply adds the object to a list, and the block
>     Stuart> exit function calls the cleanup function for objects in the list
>     Stuart> with all the appropriate try..finally wrappers.
> 
> Perhaps no need to do it in the block level.  After all, there is no
> block-level variables in Python either.  (Even "[a+1 for a in [1]]" leaves a
> local variable a in the function.)  It might be simpler if variable
> management is done when function, class and file scope exits.
> 
>     Stuart> Actually, you would want to register a function for the calling
>     Stuart> block, or perhaps an arbitrary stack frame.  It would also help
>     Stuart> to be able to retrieve the current callable object for a frame
>     Stuart> to make the system thread-safe.
> 
> No comment, since I don't quite understand this.
> 
>     Stuart> I can't see anyway around having to patch the interpreter to get
>     Stuart> the hook in there.  And I think the compiler needs to be
>     Stuart> involved to cleanup at block exit (instead of function exit).
> 
> Yes, any way to mimic that without modifying the language itself would lead
> to ugly code.  So I ask about a PEP.

I am currently experimentally implementing something along these lines. 
It does require introducing new syntax along with a new object protocol.  
I currently call it 'Indented Execution' as it allows an object to define 
__enter__ and __leave__ methods wrapping execution of 'its' indented block.  

The design is completly orthogonal to any refcount or memory management details. 

Though it begins to work it's still in the flow the more i get to know the
python core internals.  Btw, it's a pleasure reading and modifying the core 
python C code.  I didn't touch C/C++/Java much since i got to know python.  
I was suprised at the readability of the core code.  I try to keep the
changes as minimal as possible. 

A preview example (could still change) of what my experiments accomplish:

    <autoclose f1=open('/etc/passwd') f2=open('etc/shadow')>
        data1 = f1.read()
        data2 = f2.read()

    # f1 and f2 will be closed here, even if there was an exception
    # in the indented code block. Any exception will be propagated
    # of course.  

'autoclose' is the name of an object which provides 

    def __enter__(self, **kw):
        "keyword arguments containing e.g. the above 'f1' and 'f2'"

    def __leave__(self)
        """called if execution leaves the indented block"""

i didn't see an easy way to reuse the 'SETUP_FINALLY/END_FINALLY' 
bytecodes so i introduced new bytecodes.  They might go away later. 
But more about this when i have figured out the gory details of 
local namespaces & compiler passes among others. First i want
to get my unit tests to pass :-)

It's not only a conincedence that this resembles xml-syntax.  Actually
i started out using pythonic ':' syntax but this had a few problems (syntax 
ambiguity among others).  Besides, 'Indented Execution' could allow to define 
xml-documents and conversions with a very familiar (python + xml) syntax.  

There will be no closing tags, though. 

The whole idea is to build on indentation as opposed to funny closing
characters or tags.  The open/close and acquire/release protocols are
a variation of this theme. 

ok, that's enough for now.  i couldn't hold myself telling you about
this fun project :-) It's actually motivated by recent discussions here
on c.l.py and a suggestion of Michael Hudson (who aims to provide a 
new 'with' keyword achieving a significant subset of my goals). 

cheers,

    holger




More information about the Python-list mailing list