[Python-Dev] IDLE local scope cleanup

Guido van Rossum guido@python.org
Wed, 13 Nov 2002 15:00:05 -0500

> How does IDLE clean up it's local scope on startup? By that I mean the 
> following. When you start IDLE and do a dir() you get the following:
> Python 2.2.2 (#1, Oct 28 2002, 17:22:19) 
> [GCC 3.2 (Mandrake Linux 9.0 3.2-1mdk)] on linux2
> Type "copyright", "credits" or "license" for more information.
> GRPC IDLE Fork 0.8.9
  ^This is a clue.

> >>> dir()
> ['__builtins__', '__doc__', '__name__']
> >>> 
> Only three items are returned by dir(), just like in the regular Python 
> interpreter. Now I know how IDLE sets this up in its code, but I can't 
> seem to achieve the exact same results with PyCrust. And when I add a 
> print statement to the IDLE source code (PyShell.py):
> class ModifiedInterpreter(InteractiveInterpreter):
>     def __init__(self, tkconsole):
>         self.tkconsole = tkconsole
>         locals = sys.modules['__main__'].__dict__
>         print locals.keys()
>         InteractiveInterpreter.__init__(self, locals=locals)
>         self.save_warnings_filters = None
> I can see that locals contains a bunch of stuff (well, one extra item 
> when you run idle.py, and a bunch of stuff when you run PyShell.py), 
> similar to what I see in PyCrust. So where does it all go by the time 
> IDLE is up and running? Where does locals get "cleaned up"? Every one 
> of my hunches has lead to me down a dead end. I give up! Please help.

IDLE doesn't use these locals any more; they're decoys.  The GRPC
version runs the interpreter in a subprocess and the subprocess is
more careful.

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