eval modifies passed dict

J

Janto Dreijer

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
 
C

colas.francis

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
 
J

Janto Dreijer

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

['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
 
A

Arnaud Delobelle

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.
 
C

colas.francis

Janto Dreijer said:
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__']
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top