[issue16781] execfile/exec execution in other than global scope uses locals(), leading to undefined behavior

R. David Murray report at bugs.python.org
Wed Dec 26 21:55:09 CET 2012


R. David Murray added the comment:

Do you mean that *modifying* locals() in the function scope is undefined behavior?  That makes sense, and is a documented limitation.

So we need to clarify the execfile/exec documentation to note that if called in anything other than the global scope, locals() will get used as the locals dictionary, and this will lead to undefined behavior if any operation is performed that updates the local namespace...and thus you are best recommend to always pass one or two dictionaries to execfile/exec, so that you *know* what is getting updated.

Although I have to say that the exec/execfile doing something that is specified to lead to undefined behavior smells like a bug.  (Not that we could fix it even if we agreed that it was, for backward compatibility reasons.)

----------
title: execfile/exec execution of class statement does not access locals() -> execfile/exec execution in other than global scope uses locals(), leading to undefined behavior

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue16781>
_______________________________________


More information about the Python-bugs-list mailing list