Exec and namespaces
aquarius at kryogenix.org
Tue Jul 24 14:03:51 CEST 2001
I've got a CGI which reads a file and then execs its contents. The file
defines a function, and that function can't find itself to call
recursively; it gets a NameError.
[ aquarius at giles ] /home/httpd/kryogenix.org/current/web $ cat foo.cgi
sys.stderr = sys.stdout
print "Content-type: text/html\n\n"
fp = open("foo.txt")
[ aquarius at giles ] /home/httpd/kryogenix.org/current/web $ cat foo.txt
if quux == "test":
print "Hello, world!"
[ aquarius at giles ] /home/httpd/kryogenix.org/current/web $ python foo.cgi
Traceback (innermost last):
File "foo.cgi", line 13, in ?
File "foo.cgi", line 11, in main
File "<string>", line 8, in ?
File "<string>", line 6, in bar
My question here is: is there anything I can do about this? I assume
that you can't call bar() from inside bar() because it only knows about
two namespaces, global (which main() is in) and the "inside-bar()"
namespace, and bar() itself is in neither. I've tried this with both
1.5.2 and 2.0.1 and there doesn't appear to be any different (aside
from the wording of the NameError changing to "There is no variable
named 'bar'" under 2.0.1, which I assume is the same thing).
Aq -- I wish I understood namespaces
"The grand plan that is Aquarius proceeds apace"
-- Frank Miller, "Ronin"
More information about the Python-list