execfile: NameError exception thrown for things in locals()

Tim Peters tim.one at home.com
Fri Apr 6 19:51:43 EDT 2001


[Robin Thomas]
> ...
> So Python does *not* use the optimization if the function's code
> contains any exec statement. But Python does not turn off the
> optimization if you use execfile(), for various reasons not worth
> getting into here. That sucks, but the workaround is to throw in
> an exec that does nothing. Also, you can make sure that execfile()
> does not use the current locals namespace, by passing in a new
> namespace of your own.
>
> Hey, everyone, is this a known bug with a planned fix?

Yes, execfile is a known bug <0.6 wink>.  Python's local namespaces are
generally not writable (which the Ref Man warns about but Python hasn't
bothered to enforce yet).  execfile etc were *intended* to be used at the
module global level, where the local namespace is the same as the global
namespace (and global namespaces *are* reliably writable).

So this is what you can expect (or rail against) in the indefinite future:

1. Python will eventually enforce the Ref Man's warnings against trying
   to modify locals(), whether directly or indirectly.

2. The forms of exec and execfile that don't require specifying
   namespace dicts explicitly will eventually go away (and, by #1,
   "locals()" will not be allowed as an explicit namespace target
   for them).

the-bug-is-that-python-allows-you-to-say-things-it-never-intended-
    to-understand-ly y'rs  - tim





More information about the Python-list mailing list