[Python-Dev] Fast access to __builtins__

Raymond Hettinger Raymond Hettinger" <python@rcn.com
Thu, 27 Mar 2003 16:37:59 -0500

>From past rumblings, I gather that Python is moving
towards preventing __builtins__ from being shadowed.

I would like to know what you guys think about going ahead 
with that idea whenever the -O optimization flag is set.

The idea is to scan the code for lines like:

    LOAD_GLOBAL  2 (range)

and, if the name is found in __builtins__, then lookup
the name, add the reference to the constants table and, 
replace the code with something like:

   LOAD_CONST   5 (<type 'range'>)

The opcode replacement bypasses module level shadowing 
but leaves local shadowing intact.  For example:

modglob = 1            
range = xrange 
def f(list):
     for i in list:         # local shadowing of 'list' is unaffected
          print ord(i)    # access to 'ord' is optimized
          j = modglob  # non-shadowed globals are unaffected
          k = range(j)   # shadowing of globals is ignored

I've already tried out a pure python proof-of-concept and it is 
straightforward to recode it in C and attach it to PyCode_New().  

Raymond Hettinger