[Python-Dev] cycle-GC question

Guido van Rossum guido@python.org
Tue, 19 Dec 2000 11:01:46 -0500


> might be possible to avoid this circular reference but I don't
> know enough about how RExec works.  Would something like:
> 
>     def add_module(self, mname):
>         if self.modules.has_key(mname):
>             return self.modules[mname]
>         self.modules[mname] = m = self.hooks.new_module(mname)
>         if mname != '__builtin__':
>             m.__builtins__ = self.modules['__builtin__']
>         return m
>     
> do the trick?

That's certainly a good thing to do (__builtin__ has no business
having a __builtins__!), but (in my feeble experiment) it doesn't make
the leaks go away.

Note that almost every module participates heavily in cycles: whenever
you define a function f(), f.func_globals is the module's __dict__,
which also contains a reference to f.  Similar for classes, with an
extra hop via the class object and its __dict__.

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