eval modifies passed dict

Janto Dreijer jantod at gmail.com
Mon Apr 14 11:52:38 EDT 2008


On Apr 14, 5:48 pm, colas.fran... at gmail.com wrote:
> On 14 avr, 17:23, Janto Dreijer <jan... at gmail.com> wrote:
>
> > It seems eval is modifying the passed in locals/globals. This is
> > behaviour I did not expect and is really messing up my web.py app.
>
> > Python 2.5.1 (r251:54863, Mar  7 2008, 04:10:12)
> > [GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
> > Type "help", "copyright", "credits" or "license" for more information.>>> d = dict(a=1)
> > >>> d.keys()
> > ['a']
> > >>> eval("a", d)
> > 1
> > >>> d.keys()
>
> > ['a', '__builtins__']
>
> > That can't be right.
>
> From the documentation of eval[1]
> "If the globals dictionary is present and lacks '__builtins__', the
> current globals are copied into globals before expression is parsed."
>
> [1]http://docs.python.org/lib/built-in-funcs.html

Thanks!

I'll take it to the webpy group as one of their methods unexpectedly
propagates this effect.

Janto



More information about the Python-list mailing list