[Python-Dev] Adding a builtins parameter to eval(), exec() and __import__().

Mark Shannon mark at hotpy.org
Fri Mar 9 13:57:30 CET 2012


Victor Stinner wrote:
>> The reason I am proposing this here rather than on python-ideas is that
>> treating the triple of [locals, globals, builtins] as a single
>> "execution context" can be implemented in a really nice way.
>>
>> Internally, the execution context of [locals, globals, builtins]
>> can be treated a single immutable object (custom object or tuple)
>> Treating it as immutable means that it can be copied merely by taking a
>> reference. A nice trick in the implementation is to make a NULL locals
>> mean "fast" locals for function contexts. Frames, could then acquire their
>> globals and builtins by a single reference copy from the function object,
>> rather than searching globals for a '__builtins__'
>> to find the builtins.
> 
> Creating a new frame lookup for __builtins__ in globals only if
> globals of the new frame is different from the globals of the previous
> frame. You would like to optimize this case? If globals is unchanged,
> Python just increments the reference counter.

I'm more interested in simplifying the code than performance.
We this proposed approach, there is no need to test where the globals 
come from, or what the builtins are; just incref the namespace triple.

> 
> When globals is different from the previous frame? When you call a
> function from a different module maybe?
> 
> Do you have an idea of the speedup of your optimization?

No. But it won't be slower.

Cheers,
Mark


More information about the Python-Dev mailing list