470,643 Members | 1,373 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,643 developers. It's quick & easy.

eval modifies passed dict

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.

Regards
Janto
Jun 27 '08 #1
5 917
On 14 avr, 17:23, Janto Dreijer <jan...@gmail.comwrote:
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
Jun 27 '08 #2
On Apr 14, 5:48*pm, colas.fran...@gmail.com wrote:
On 14 avr, 17:23, Janto Dreijer <jan...@gmail.comwrote:
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
Jun 27 '08 #3
On Apr 14, 4:23*pm, Janto Dreijer <jan...@gmail.comwrote:
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.
Reading the documentation would be a good start:

From http://docs.python.org/lib/built-in-funcs.html:

eval( expression[, globals[, locals]])
The arguments are a string and optional globals and locals. If
provided, globals must be a dictionary. If provided, locals can be any
mapping object. Changed in version 2.4: formerly locals was required
to be a dictionary.
The expression argument is parsed and evaluated as a Python expression
(technically speaking, a condition list) using the globals and locals
dictionaries as global and local name space. If the globals dictionary
is present and lacks '__builtins__', the current globals are copied
into globals before expression is parsed.

--
Arnaud

Jun 27 '08 #4
Janto Dreijer <ja****@gmail.comwrote:
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.
That can exactly be right.

Python always expects a global called '__builtins__'. If it isn't in the
dict you pass to eval to use for globals it will be added. You may, of
course, initialise it yourself if you don't want your script to have
access to all of the standard globals.

The current document is (I think) wrong or at the least misleading. It
says:
If the globals dictionary is present and lacks '__builtins__', the
current globals are copied into globals before expression is parsed.
I think it should say:
If the globals dictionary is present and lacks '__builtins__', the
current value of __builtins__ is added to globals before expression
is parsed.
i.e. only a single variable is assigned, other globals aren't copied.
Jun 27 '08 #5
On 14 avr, 18:05, Duncan Booth <duncan.bo...@invalid.invalidwrote:
Janto Dreijer <jan...@gmail.comwrote:
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.

That can exactly be right.

The current document is (I think) wrong or at the least misleading. It
says:
If the globals dictionary is present and lacks '__builtins__', the
current globals are copied into globals before expression is parsed.

I think it should say:
If the globals dictionary is present and lacks '__builtins__', the
current value of __builtins__ is added to globals before expression
is parsed.

i.e. only a single variable is assigned, other globals aren't copied.
Indeed:

Python 2.5.1 (r251:54863, Mar 7 2008, 03:39:23)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>globals().keys()
['__builtins__', '__name__', '__doc__']
>>b = 2
d = {'a': 1}
eval('a', d)
1
>>d.keys()
['a', '__builtins__']

Jun 27 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Paddy | last post: by
3 posts views Thread by Jeremy Sanders | last post: by
15 posts views Thread by manstey | last post: by
18 posts views Thread by greenflame | last post: by
8 posts views Thread by rdrink | last post: by
8 posts views Thread by abhishek | last post: by
4 posts views Thread by Jon Slaughter | last post: by
reply views Thread by Lie Ryan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.