Problems with exec scoping

Erik Walthinsen omega at temple-baptist.com
Sun Jun 2 23:49:54 EDT 2002


I'm working on a project that involves significant use of python embedded
in HTML.  I've modified an existing package (psp.py) to work in my
environment, and cleaned up its syntax a little (more to do still).  It
returns a string containing the entire page with verbatim html in
stringIO.write() statements and the code out in the clear.

My problem is that all the pages written in this style need to have
access to variables in other parts of the program (the http server
functionality is only a small part of it the whole system).  My solution
was to optionally pass a dictionary with the variables I need to have in
the page's scope:

def send_page(....,filename,....,dict={}):
  ...process filename into execdata...

  localdict = locals()
  for key in dict.keys():
    localdict[key] = dict[key]
  exec execdata in globals(), localdict

class someclass:
  objects = {}
  def somefunction(filename):
    send_page(....,filename,....,{ objects = someclass.objects })

The problem I'm running into now is that if I print out the repr() of the
'objects' variable from within the python in the HTML page, I get a dict
with the right keys, but the values of each of the keys are non-existant.
This obviously puts a bit of a crimp in my plans, since the whole point of
making the dict available is to use the objects contained.

I've seen the other threads on exec scope, and they seem like they might
be applicable, but the thread mostly covers the black magic of most
efficiently creating the right dictionary for exec.  That doesn't seem to
be relevant to this problem, but I might be wrong.  I've only been doing
python for a couple weeks now, so there's a lot I don't know yet.

The entire project will be available in CVS shortly, at
http://savannah.gnu.org/projects/metanet/, with captive_send_page residing
in captivewebserver.py and the someclass/somefunc being CaptiveClient/ 
captive_http_request in captiveclient.py.

In the meantime I'll code up the pages I need manually, but that can't
scale very far.

TIA,
   Omega

      Erik Walthinsen <omega at temple-baptist.com> - System Administrator
        __
       /  \                GStreamer - The only way to stream!
      |    | M E G A        ***** http://gstreamer.net/ *****
      _\  /_



More information about the Python-list mailing list