Comment 7 for bug 269966

Revision history for this message
Fernando Perez (fdo.perez) wrote : Re: [Bug 269966] Re: garbage collection problem

On Sun, Mar 15, 2009 at 4:07 PM, matthew arnison <email address hidden> wrote:
> I read over your patch, and the main functional change as I see it that
> the cache of old modules is now a dict instead of a list. The dict is
> keyed by the %run script path. This means that only 1 module copy is
> cached per script path.

Yup :) It's the dead-simple, obvious solution in retrospect, but it
took me a while to realize that. I had a really nice 'aha' moment
when I understood it, and it's a great little example of the power of
Python's data structures.

In fact, the original fix was simply to change in iplib, around line 345:
       self._user_main_modules = []
to:
       self._user_main_modules = {}

and then in %run, instead of:
       self.shell._user_main_modules.append(main_mod)
use:
       self.shell._user_main_modules[os.path.abspath(main_mod.__file__)]=main_mod

So the whole patch, at its core, was 2 lines. I ended up coding a
fancier version, with a real API to access this so that magics don't
go mucking around with private internals of the shell object, but in
the end this was the whole idea, 2 lines :)

I think I'll write up a little blog post about this, it's a good
illustration of the (well known) fact that thinking clearly about your
data structures can save you a lot of time, and how Python exposes
naturally data structures that are very well adapted to many problems.

> Nice solution. Thanks for continuing to share your work on IPython.

My pleasure. Thanks for the feedback!

f