[IronPython] Issues with "exec X in Y"

Willem Broekema metawilm at gmail.com
Sun Jul 24 11:39:22 CEST 2005


On 7/23/05, Jonathan Jacobs <korpse-ironpython at kaydash.za.net> wrote:
>    config = {}
>    exec file('config.py') in config
>    del config['__builtins__']
> [...]
> My patch however doesn't add a "__builtins__" key to the
> mappable types, so the above code would actually error out in IronPython
> with my patch, so someone probably wants to add that.

No, your code relies on an implementation detail. A Python
implementation may add any key, like __builtins__, to the dictionary,
but is not at all required to. Your code should check for the
existence of the key before deleting it.

There are cases where the difference between Python-the-language and
CPython-the-implementation is not clear, but in this case it's
explicitly documented in
<http://www.python.org/doc/current/ref/exec.html>:

  As a side effect, an implementation may insert additional keys into
the dictionaries given besides those corresponding to variable names
set by the executed code. For example, the current implementation may
add a reference to the dictionary of the built-in module __builtin__
under the key __builtins__ (!).


- Willem



More information about the Ironpython-users mailing list