473,569 Members | 2,790 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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)\n f(3.14)"
def execute():
exec mycode
execute()
I get the error:

root@devmachine 1:/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)\n f(3.14)"
exec mycode
I have tested this in python 2.3 and 2.4.
Regards
Carl

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

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

root@devmachine 1:/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)\n f(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***********@g mail.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***********@g mail.com
<ca***********@ gmail.comescrib i�:
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
3166
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 plotting module which is distributed under a PSF compatible license, and hence we avoid using GPLd code so as to not trigger the GPL requirements. ...
8
1460
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 Perl program when you're calling other modules you'll add "use" statements at the beginning of your script like: use strict; use WWW::Mechanize;...
6
3457
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 testee.py without changing the code for testee.py. testee.py has a module in it that we want to mock in some tests and in others use the real...
0
4531
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 ( -8) / 278 closed (+12) / 527 total ( +4) New / Reopened Patches ______________________
0
7694
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7921
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8118
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7666
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5504
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.