Raymond Hettinger wrote:
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 (
)
Using the -O for this is not a working possibility. -OO is reserved for optimizations which can change semantics, but even there, I'd rather like a per-module switch than a command line switch. BTW, why not have a new opcode for symbols in the builtins and then only tweak the opcode implementation instead of having the compiler generate different code ?
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
_______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev
-- Marc-Andre Lemburg eGenix.com Professional Python Software directly from the Source (#1, Mar 27 2003)
Python/Zope Products & Consulting ... http://www.egenix.com/ mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
Python UK 2003, Oxford: 5 days left EuroPython 2003, Charleroi, Belgium: 89 days left