[Python-Dev] IDLE local scope cleanup

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


> Here is my real problem. I used to just pass a regular dictionary to 
> code.InteractiveInterpreter, which worked well enough. But I just 
> discovered an issue with pickling in the PyCrust shell, illustrated 
> below:
> 
> Welcome To PyCrust 0.8 - The Flakiest Python Shell
> Sponsored by Orbtech - Your source for Python programming expertise.
> Python 2.2.2 (#1, Oct 28 2002, 17:22:19) 
> [GCC 3.2 (Mandrake Linux 9.0 3.2-1mdk)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import pickle
> >>> def foo():
> ...     pass
> ...     
> >>> pickle.dumps(foo)
> Traceback (most recent call last):
>   File "<input>", line 1, in ?
>   File "/usr/local/lib/python2.2/pickle.py", line 978, in dumps
>     Pickler(file, bin).dump(object)
>   File "/usr/local/lib/python2.2/pickle.py", line 115, in dump
>     self.save(object)
>   File "/usr/local/lib/python2.2/pickle.py", line 225, in save
>     f(self, object)
>   File "/usr/local/lib/python2.2/pickle.py", line 519, in save_global
>     raise PicklingError(
> PicklingError: Can't pickle <function foo at 0x8654b9c>: it's not found 
> as __main__.foo
> >>> 
> 
> So I decided to switch to using sys.modules['__main__'].__dict__, which 
> eliminated the pickling error, but introduced a bunch of clutter in the 
> local namespace. Any suggestions?

Remove the clutter.  This probably means having a very minimal "real"
main program.  IDLE does this by putting all the real code in the
"run" module, and bootstrapping the subprocess as follows:

  python -c "__import__('run').main()"

This is roughly equivalent to executing

  import run
  run.main()

except that it doesn't create a variable 'run'.

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