[Python-bugs-list] [ python-Bugs-508476 ] exec does not pass on locals/globals

noreply@sourceforge.net noreply@sourceforge.net
Fri, 25 Jan 2002 11:55:01 -0800


Bugs item #508476, was opened at 2002-01-25 06:51
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=508476&group_id=5470

Category: Python Interpreter Core
Group: None
>Status: Closed
>Resolution: Invalid
Priority: 5
Submitted By: a.hofkamp (axhlkhb)
Assigned to: Nobody/Anonymous (nobody)
Summary: exec does not pass on locals/globals

Initial Comment:
Hello,

The program shows the problem.

--------------------
# spam.py
#

def myf(i): return i+1

def myg(i): return f(i)+10


print "Try to call myf() as f()"
exec "print f(0)" in { 'g':myg, 'f':myf }
print

print "Try to call myg() as g()"
print "and indirectly myf() as f()"
exec "print g(0)" in { 'g':myg, 'f':myf }
--------------------

We are trying to exec statements in a custom
environment. A simple form works as expected (the f(0)
call), a more complicated form fails, because the
custom environment is not passed on downwards (in
myg(), "f" is not known).
In the 'real' program, the dictionary is dynamic, thus
tricks like adding "f=myf" to the formal parameter list
of myg cannot be done (not to mention that the custom
environment is quite big).

Apparently, it is not possible to execute functions in
a custom environment. This behaviour severely limits
the usefulness of exec.

Until now, we fail to see the logic in this behaviour,
therefore, we report it as a bug.

The Python version we used is Python 1.5.2, but 2.1.1
also fails to execute the example.

Albert (a.t.hofkamp@tue.nl)


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

>Comment By: Martin v. Löwis (loewis)
Date: 2002-01-25 11:55

Message:
Logged In: YES 
user_id=21627

This is not a bug. The set of globals for a piece of code is
defined at the point when the code is compiled. So the
dictionary you pass is only used to resolve names in the
code, not to resolve names in functions called in the code. 

Suppose you do

exec "urllib.localhost()" in {'socket':0}

would you expect that socket resolves to 0 inside
urllib.localhost?


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

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=508476&group_id=5470