Using import in an exec'ed string 
Author Message
 Using import in an exec'ed string

I'm wondering why, if I give a customized globals
and locals parameter to exec, import doesn't work
anymore (that is, it doesn't anything to the namespace.):

a="""
import string

def f():
        print string.split("aaa aaa")

f()"""

exec(a)

----------------

a="""
import string

def f():
        print string.split("aaa aaa")

f()"""

exec(a, {}, {})

(Note that in the larger program I'm working with,
I don't simply clear globals - I add a few functions
to it, that's all, but it has the same result).

Could anyone enlighten me?

--

Boudewijn Rempt  | http://www.*-*-*.com/



Thu, 22 Jan 2004 14:59:45 GMT  
 Using import in an exec'ed string

Quote:

> I'm wondering why, if I give a customized globals
> and locals parameter to exec, import doesn't work
> anymore (that is, it doesn't anything to the namespace.):
> a="""
> import string

> def f():
>      print string.split("aaa aaa")

> f()"""

> exec(a)

[works]

Quote:
> exec(a, {}, {})

[fails - NameError on string inside f]

Nothing to do with import. This fails too:

a = """
x = 3
def f():
  print x
f()
"""
exec(a, {}, {})

But, this works:
d = {}
exec(a, d, d)

- Gordon



Thu, 22 Jan 2004 19:43:26 GMT  
 Using import in an exec'ed string

Quote:

> Nothing to do with import. This fails too:

> a = """
> x = 3
> def f():
>   print x
> f()
> """
> exec(a, {}, {})

> But, this works:
> d = {}
> exec(a, d, d)

I've now determined that the breaking factor is the passing of
a locals variable. I don't know why - wish I would, because I
want to explain the behaviour in a chapter on implementing macro
extensions for python applications. (There is an ominous sentence on
messing with locals in the language reference, though).

The following script shows a bit of my experiments:

print "GLOBALS:\t", globals()
print "LOCALS:\t\t", locals()

s = """
import string
x=1

if globals().has_key("string"): print "OOOK"
if locals().has_key("string"): print "EEEK"

def f():
    print string.split("xxx xxx")

f()

"""    
print "=======: Kaal"
exec s
print "=======: Empty dicts"
exec (s, {})
print "=======: Empty dicts from vars"
g={}
l={}
exec (s, g)
print "=======: Messed up dicts"
g=globals()
g["x"] = 10
exec s in g
print "=======: Functions"
g=globals()
g["x"] = 10
exec (s, globals(), globals())

--
Boudewijn | http://www.valdyas.org



Fri, 23 Jan 2004 15:54:20 GMT  
 Using import in an exec'ed string

Quote:


> > Nothing to do with import. This fails too:

> > a = """
> > x = 3
> > def f():
> >   print x
> > f()
> > """
> > exec(a, {}, {})

> > But, this works:
> > d = {}
> > exec(a, d, d)

> I've now determined that the breaking factor is the passing of
> a locals variable. I don't know why - wish I would, because I
> want to explain the behaviour in a chapter on implementing macro
> extensions for Python applications. (There is an ominous sentence on
> messing with locals in the language reference, though).

The reason it fails is that if you provide a locals dict all name
bindings on the outermost level will be put into the locals dict, but
not into the globals dict. The function f has its own locals of course
and can't access the names in the exec's locals. This is similar to the
situation of nested functioins where the inner function can't access the
local names of the surrounding functions.

Playing around with this a bit, I found that this happens even with
nested scopes:

Python 2.2a1 (#1, Jul 19 2001, 19:38:55)
[GCC 2.95.2 20000220 (Debian GNU/Linux)] on linux2
Type "copyright", "credits" or "license" for more information.

Quote:
>>> g = {}
>>> l = {}
>>> s = """

... import string
... def f():
...     string.split("a b")
... f()
... """
Quote:
>>> exec s in g, l  

Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<string>", line 5, in ?
  File "<string>", line 4, in f
NameError: global name 'string' is not defined

It seems to me that with nested scopes f should be able to access the
surrounding scope's names.

   Bernhard

--
Intevation GmbH                                 http://intevation.de/
Sketch                                 http://sketch.sourceforge.net/
MapIt!                                               http://mapit.de/



Fri, 23 Jan 2004 18:33:10 GMT  
 Using import in an exec'ed string

[ ... ]
Quote:

> It seems to me that with nested scopes f should be able to access the
> surrounding scope's names.

Indeed it should, but remember that these scopes are STATIC (i.e. determined
at compile time) and not dynamic ("I'll just have a look in my caller's
local namespace to see whether what I want is there").

Thus by the time you call f, the compiler has already determined which names
are from nested scopes, by looking at functions whose definition surrounds
the definition of f. If there aren't any, no nested scopes.

regards
 STeve
--
http://www.holdenweb.com/



Fri, 23 Jan 2004 20:15:41 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. return val of exec'ed prog

2. interaction with exec'ed process

3. How to pass arguments to program been exec'ed

4. waiting for an exec'ed process to finish

5. Redirecting output of a exec'ed application

6. Q: interacting with exec'ed commands ?

7. Capturing output from exec'ed command

8. intern'ed strings and deepcopy()

9. exec'ing an import

10. problem with exec: import doesn't seem to work

11. OT: using GPL'ed icons

12. Using 'exec' in subroutines

 

 
Powered by phpBB® Forum Software