Python vs Java garbage collection?

Stuart D. Gathman stuart at bmsi.com
Sun Dec 29 23:30:39 EST 2002


On Sat, 28 Dec 2002 01:59:53 -0500, Isaac To wrote:

>>>>>> "Stuart" == Stuart D Gathman <stuart at bmsi.com> writes:
> 
>     Stuart> 1) This feature should not reclaim memory, only call
>     __del__(). Stuart> If references were still live, the effect would
>     be the same as Stuart> calling __del__() on a live object.
> 
>     Stuart> 2) __del__() would get called again when memory is actually
>     Stuart> reclaimed.  Therefore, your feature would be much safer if
>     it Stuart> called another special function - say close() or
>     dispose() at Stuart> the end of the block.
> 
>     Stuart> 3) There is probably a way to do what you want without
>     Stuart> additional syntax:
> 
>     Stuart> def foo(): mymutex = auto(acquire_mutex()) myfile = Stuart>
>     auto(open(filename,'w') mysocket = auto(open_socket()) Stuart>
>     do_stuff()
> 
> Is there a PEP for that yet?  If not, what about writing one?

Can you think of a way to register a function to get called when the
current block exits?  If so, then it is pretty straightforward to
implement the proposal in pure python.  The auto function simply adds the
object to a list, and the block exit function calls the cleanup function
for objects in the list with all the appropriate try..finally wrappers.

Actually, you would want to register a function for the calling block, or
perhaps an arbitrary stack frame.  It would also help to be able to
retrieve the current callable object for a frame to make the system
thread-safe. 

I can't see anyway around having to patch the interpreter to get the hook
in there.  And I think the compiler needs to be involved to cleanup at
block exit (instead of function exit).

Basically, every block that contains a call to auto(obj) should be
wrapped in something like:
  
  __auto_list__ = []
  try:
    code_for_block
  finally:
    __auto_exit__(__auto_list__)

-- 
	      Stuart D. Gathman <stuart at bmsi.com>
Business Management Systems Inc.  Phone: 703 591-0911 Fax: 703 591-6154
"Confutatis maledictis, flamis acribus addictis" - background song for
a Microsoft sponsored "Where do you want to go from here?" commercial.



More information about the Python-list mailing list