[Python-Dev] variable name resolution in exec is incorrect

Greg Ewing greg.ewing at canterbury.ac.nz
Thu May 27 05:13:16 CEST 2010


On 27/05/10 11:33, Michael Foord wrote:
> On 27/05/2010 00:38, Greg Ewing wrote:

>> Maybe the second scope argument to exec() should
>> be deprecated?
>>
> Sounds good to me, certainly ends the confusion over this undoubtedly
> unintuitive behaviour. :-)

Although it's a fair point that it can be useful to have
a way of capturing definitions made by the execed code,
while still making an environment of other stuff available
to it.

So, I'd be in favour of changing the behaviour of exec
so that the local scope is made visible inside functions.
However, it would be non-trivial to implement this the
way things are currently structured, which is probably one
of the reasons it hasn't already been done.

I don't think that simply using LOAD_NAME inside the function
would work, because that would only look in the function's
own local namespace first, then in the global one. There is
just no mechanism available at the moment for the function
to know about the local namespace passed in to exec.

The way that functions get access to names in enclosing
local scopes is by having them passed in as cells, but that
mechanism is only available for optimised local namespaces,
not ones implemented as dicts.

-- 
Greg


More information about the Python-Dev mailing list