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

Colin H hawkett at gmail.com
Thu May 27 15:39:28 CEST 2010


Yep fair call - was primarily modifying Guido's example to make the point about not being able to delete from the globals returned from exec - cheers,

Colin

On Thu, May 27, 2010 at 2:09 PM, Scott Dial <scott+python-dev at scottdial.com> wrote:

On 5/27/2010 7:14 AM, Colin H wrote:

def definestuff(usercode): context = {...} stuff = {} stuff.update(context)

exec(usercode, stuff) returnstuff = {} returnstuff.update(stuff) del returnstuff['builtins'] for key in context: if key in returnstuff and returnstuff[key] == context[key]: del returnstuff[key] return returnstuff I'm not sure your application, but I suspect you would benefit from using an identity check instead of an eq check. The equality check may be expensive (e.g., a large dictionary), and I don't think it actually is checking what you want -- if the usercode generates an eq-similar dictionary, wouldn't you still want that? The only reason I can see to use eq is if you are trying to detect usercode modifying an object passed in, which is something that wouldn't be addressed by your original complaint about exec (as in, modifying a global data structure). Instead of: if key in returnstuff and returnstuff[key] == context[key]: Use: if key in returnstuff and returnstuff[key] is context[key]: -- Scott Dial scott at scottdial.com scodial at cs.indiana.edu



More information about the Python-Dev mailing list