[Python-Dev] variable name resolution in exec is incorrect (original) (raw)
Scott Dial scott+python-dev at scottdial.com
Thu May 27 15:09:55 CEST 2010
- Previous message: [Python-Dev] variable name resolution in exec is incorrect
- Next message: [Python-Dev] variable name resolution in exec is incorrect
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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 user_code 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 user_code 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
- Previous message: [Python-Dev] variable name resolution in exec is incorrect
- Next message: [Python-Dev] variable name resolution in exec is incorrect
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]