[Python-Dev] variable name resolution in exec is incorrect (original) (raw)

Guido van Rossum guido at python.org
Thu May 27 03:05:46 CEST 2010


On Wed, May 26, 2010 at 5:53 PM, Colin H <hawkett at gmail.com> wrote:

Thanks for the possible workaround - unfortunately 'stuff' will contain a whole stack of things that are not in 'context', and were not defined in 'usercode' - things that python embeds - a (very small) selection -

{..., 'NameError': <type 'exceptions.NameError'>, 'BytesWarning': <type 'exceptions.BytesWarning'>, 'dict': <type 'dict'>, 'input': <function input at 0x10047a9b0>, 'oct': , 'bin': , ...} It makes sense why this happens of course, but upon return, the globals dict is very large, and finding the stuff you defined in your usercode amongst it is a very difficult task.  Avoiding this problem is the 'locals' use-case for me.  Cheers,

No, if taken literally that doesn't make sense. Those are builtins. I think you are mistaken that each of those (e.g. NameError) is in stuff -- they are in stuff['builtins'] which represents the built-in namespace. You should remove that key from stuff as well.

--Guido

Colin

On Thu, May 27, 2010 at 1:38 AM, Guido van Rossum <guido at python.org> wrote: This is not easy to fix. The best short-term work-around is probably a hack like this:

def definestuff(usercode):  context = {...}  stuff = {}  stuff.update(context)  exec(usercode, stuff)  for key in context:  if key in stuff and stuff[key] == context[key]:  del stuff[key]  return stuff -- --Guido van Rossum (python.org/~guido)

-- --Guido van Rossum (python.org/~guido)



More information about the Python-Dev mailing list