This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author amaury.forgeotdarc
Recipients amaury.forgeotdarc, kfitch
Date 2008-11-21.21:53:42
SpamBayes Score 3.432542e-05
Marked as misclassified No
Message-id <1227304424.26.0.13912053096.issue4381@psf.upfronthosting.co.za>
In-reply-to
Content
I agree that it is confusing; in short: a bare exec() does not play well with closures;  
This is a consequence of the python execution model, and is unlikely to change.

Here, the class 'foo' is stored in the function's local variables.
But the execution of the body of the 'bar' class searches names in its local scope (the 
class body) and the global scope (the module level), and misses the function's 
locals...

I strongly suggest to avoid pure exec() statements; always specify a global and/or a 
local dictionary.

In your case, the following works:

def doExec(text):
  d = {}  # or:   d = dict(globals())
  exec text in d
  print d.keys()
History
Date User Action Args
2008-11-21 21:53:45amaury.forgeotdarcsetrecipients: + amaury.forgeotdarc, kfitch
2008-11-21 21:53:44amaury.forgeotdarcsetmessageid: <1227304424.26.0.13912053096.issue4381@psf.upfronthosting.co.za>
2008-11-21 21:53:43amaury.forgeotdarclinkissue4381 messages
2008-11-21 21:53:42amaury.forgeotdarccreate