473,232 Members | 1,678 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,232 software developers and data experts.

python exec behaves inconsistent with respect to module imports

Hello

I am completely puzzled why the following exec code does not work:

mycode = "import math\ndef f(y):\n print math.floor(y)\nf(3.14)"
def execute():
exec mycode
execute()
I get the error:

root@devmachine1:/opt/qbase# python error1.py
Traceback (most recent call last):
File "error1.py", line 5, in ?
execute()
File "error1.py", line 4, in execute
exec mycode
File "<string>", line 4, in ?
File "<string>", line 3, in f
NameError: global name 'math' is not defined
Note that the following code _does_ work:

mycode = "import math\ndef f(y):\n print math.floor(y)\nf(3.14)"
exec mycode
I have tested this in python 2.3 and 2.4.
Regards
Carl

Sep 5 '07 #1
4 2921
Am Wed, 05 Sep 2007 13:12:24 +0000 schrieb ca***********@gmail.com:
I am completely puzzled why the following exec code does not work:

mycode = "import math\ndef f(y):\n print math.floor(y)\nf(3.14)"
def execute():
exec mycode
execute()
I get the error:

root@devmachine1:/opt/qbase# python error1.py
Traceback (most recent call last):
File "error1.py", line 5, in ?
execute()
File "error1.py", line 4, in execute
exec mycode
File "<string>", line 4, in ?
File "<string>", line 3, in f
NameError: global name 'math' is not defined
Note that the following code _does_ work:

mycode = "import math\ndef f(y):\n print math.floor(y)\nf(3.14)"
exec mycode
I have tested this in python 2.3 and 2.4.
exec breaks nested namespaces here.

Essentially

exec "import math"

puts the "math" name into the local namespace, but

"def f(): math.floor"

looks up "math" in the global namespace. On the module level global and
local namespace are identical
>>globals() is locals()
True

but inside a function they are distinct
>>def f(): return globals() is locals()
....
>>f()
False

A workaround is to declare "math" as global:
>>s = """
.... global math
.... import math
.... def f(y): print math.floor(y)
.... f(3.14)
.... """
>>def execute():
.... exec s
....
>>execute()
3.0

or pass it explicitly:

[new interpreter session]
>>s = """
.... import math
.... def f(y, math=math): print math.floor(y)
.... f(3.14)
.... """
>>def execute():
.... exec s
....
>>execute()
3.0

Peter
Sep 5 '07 #2
Hi

Thank you for the explanation

It seems that python behaves different for variables created in the
toplevel namespace, versus deeper namespaces.

CODE:

def g():
a=2
print "a in LOC:",locals().has_key('a')
print "a in GLO:",globals().has_key('a')
def f():
print "a in LOC:",locals().has_key('a')
print "a in GLO:",globals().has_key('a')
print a
f()
g()

b=3
print "b in LOC:",locals().has_key('b')
print "b in GLO:",globals().has_key('b')
def h():
print "b in LOC:",locals().has_key('b')
print "b in GLO:",globals().has_key('b')
print b
h()
RESULT:

a in LOC: True
a in GLO: False
a in LOC: True
a in GLO: False
2
b in LOC: True
b in GLO: True
b in LOC: False
b in GLO: True
3
DISCUSSION:

locals() are passed through from a function to a nested function
locals() are not passed if you go from top-level to a nested function.

globals() are visible from all levels
globals() are auto-populated at top level

So now get back to my exec code in the previous post.
The exec applies a mixture of both rules
1. The exec looks at its namespace to apply the rule for globals().
Since I'm not at top-level, variables are not auto-populated in the
globals(), unless I add the keyword 'global' like you suggested.
2. However, the exec applies the other rule for locals(). It does NOT
copy the locals() from one level to the nested one, although it
should.

To me this seems a bug in python exec:
- exec should look at its context to find out if the exec is called
top-level or deeper
- if top-level: auto-populate globals, and do not allow inheritance of
locals to nested levels
- if non top-level: dont populate globals, but allow inheritance of
locals to nested levels.

What do you think?


Sep 5 '07 #3
Am Wed, 05 Sep 2007 14:45:11 +0000 schrieb ca***********@gmail.com:
So now get back to my exec code in the previous post.
The exec applies a mixture of both rules
1. The exec looks at its namespace to apply the rule for globals().
Since I'm not at top-level, variables are not auto-populated in the
globals(), unless I add the keyword 'global' like you suggested.
2. However, the exec applies the other rule for locals(). It does NOT
copy the locals() from one level to the nested one, although it
should.

To me this seems a bug in python exec:
- exec should look at its context to find out if the exec is called
top-level or deeper
- if top-level: auto-populate globals, and do not allow inheritance of
locals to nested levels
- if non top-level: dont populate globals, but allow inheritance of
locals to nested levels.
I like your systematic approach.
What do you think?
Too much work :)

Seriously, the local namespace in a normal python function is not a
dictionary, and the decision on how to access a variable is made at
compile time. One way to achieve consistency would be to always use
dictionaries and have the lookup work its way up through the enclosing
namespaces. Of course that would kill performance...

Peter
Sep 5 '07 #4
En Wed, 05 Sep 2007 11:45:11 -0300, ca***********@gmail.com
<ca***********@gmail.comescribi�:
Thank you for the explanation

It seems that python behaves different for variables created in the
toplevel namespace, versus deeper namespaces.
Yes. Older Python versions had only two scopes: local and global. A name
was either local or global. Now, there are nested scopes too.
If a name is bound inside a function (by example, an identifier at the
left hand side of an assignment) it is local to that function (unless the
global statement is used). The check is made at compile time.
If a name is bound at the module level, it is a global variable.
Names used in a function but defined on an enclosing function are free
variables.
CODE:

def g():
a=2
print "a in LOC:",locals().has_key('a')
print "a in GLO:",globals().has_key('a')
def f():
print "a in LOC:",locals().has_key('a')
print "a in GLO:",globals().has_key('a')
print a
f()
g()

b=3
print "b in LOC:",locals().has_key('b')
print "b in GLO:",globals().has_key('b')
def h():
print "b in LOC:",locals().has_key('b')
print "b in GLO:",globals().has_key('b')
print b
h()
DISCUSSION:

locals() are passed through from a function to a nested function
locals() are not passed if you go from top-level to a nested function.
No.
A function (more generally: a code block) can refer to local variables,
free variables, or global variables.
Local variables are NOT stored in a dictionary, but on a fixed size array,
and indexed by ordinal instead of name. The compiler knows exactly which
variables are local at compile time, just looking at the code.
Free variables are accessed thru "cell" objects, which keep a reference to
the enclosing scope. The compiler knows about free variables too (they are
local to some enclosing scope). Try adding these lines at the end of g
function above:

f()
a=-1
f()

Global variables are those in the module where the function is defined
(plus some built-in names) - they're searched by name at runtime.
Local variables are, uhm, local :) and not "passed" anywhere.

locals() is a dictionary built on-demand. It includes local variables
*AND* free variables too.
In function f above, there are no local variables, and `a` is a free
variable.
In function g, `a` and `f` are local variables.
In function h, `b` is a local variable.
globals() are visible from all levels
Yes, unless an inner scope shadows the name. Putting b=5 at the first line
on function h above hides the global `b`
globals() are auto-populated at top level
Yes; global variables are those defined at the module level.
See the Language Reference <http://docs.python.org/ref/naming.html>
So now get back to my exec code in the previous post. [...] To me this
seems a bug in python exec:
Now that you know a bit more about locals and globals, try to understand
what exec does in your example.
Note that the exec statement has two optional arguments that are used as
the global and local namespaces: <http://docs.python.org/ref/exec.html>

--
Gabriel Genellina

Sep 6 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: John Hunter | last post by:
I have a question about what it takes to trigger GPL restrictions in python code which conditionally uses a GPL library. Here is the context of my question. matplotlib, which I develop, is a...
8
by: Gekitsuu | last post by:
I've been reading a lot of python modules lately to see how they work and I've stumbled across something that's sort of annoying and wanted to find out of there was a good reason behind it. In a...
6
by: Silfheed | last post by:
Heyas So we have the following situation: we have a testee.py that we want to automatically test out and verifiy that it is worthy of being deployed. We want our tester.py to test the code for...
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 349 open ( +7) / 3737 closed (+25) / 4086 total (+32) Bugs : 939 open (-12) / 6648 closed (+60) / 7587 total (+48) RFE : 249 open...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
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
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
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...

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.