473,327 Members | 2,118 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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 983
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Paddy | last post by:
Hi, I got tripped up on the way eval works with respect to modules and so wrote a test. It seems that a function carries around knowledge of the globals() present when it was defined. (The...
3
by: Jeremy Sanders | last post by:
Hi - I'm trying to subclass a dict which is used as the globals environment of an eval expression. For instance: class Foo(dict): def __init__(self): self.update(globals()) self = 42
15
by: manstey | last post by:
Hi, I have a text file called a.txt: # comments I read it using this:
18
by: greenflame | last post by:
I want to make a function that does the following. I will call it thefunc for short. '||Char>>' I tried the following def thefunc(s): s = "||" + s + ">>"
8
by: rdrink | last post by:
n.n.h. (noob needs help) Ok, I've been beating my head against this for a day... time to ask others. To explain things as simply as possible: I am trying to use eval() to evaluate some simple...
8
by: abhishek | last post by:
>>a,b=3,4 7 Now I want to evaluate y by substituting for the evaluated value of x. eval(y) will try to add "a+b" to 3 and return an error. I could do this, 10 but this becomes unwieldy if I...
4
by: Jon Slaughter | last post by:
I'm using eval to excute some mixed php and html code but I cannot debug it. I am essentially using filegetcontents to load up a php/html file and then inserting it into another php/html file and...
6
by: RandomElle | last post by:
Hi there I'm hoping someone can help me out with the use of the Eval function. I am using Access2003 under WinXP Pro. I can successfully use the Eval function and get it to call any function with...
0
by: Lie Ryan | last post by:
On Tue, 30 Sep 2008 16:04:34 -0500, William Purcell wrote: when you pass mydict, it is used as the global variables in the eval, right? Then, you passed a code to eval('...', mydict), sometimes...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.