quetz-mod_python-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Sean Treadway" <se...@superchannel.org>
Subject Local scope in PSP
Date Sun, 31 Aug 2003 21:05:31 GMT
Hi all, two things.

First, are there archives of this list?

Second... I've entered into the realm of some python demi-voodoo,
tracking down a non-intuitive behavior of calling functions defined
within a PSP page.  Please correct my interpretation of the cause, or
the terminology of the description if it is incorrect.

Problem: Calling a function defined in a PSP page from the same PSP page
works, but calling a function defined in a PSP page from another
function defined in the same PSP page raises a NameError.

Test case:  

Given the following PSP source:

<%
def world(req):
	req.write('world')

def hello_world(req):
	req.write('Hello ')
	world(req)

hello_world(req)
%>

The expected output is:  """Hello world"""

The actual output is:

"""Hello 
Mod_python error: "PythonHandler mod_python.psp"

Traceback (most recent call last):

...

  File "/home/seant/www/test.psp", line 9, in ?
    

NameError: global name 'world' is not defined
"""

I have tracked this down to the overriding of the local scope within
lib/python/mod_python/psp.py during the execution of the code fragment.
The second optional parameter to the "exec code in globals(), {'req':
req ...}" statement affects the local scope, overriding the actual local
scope defined within the compiled source.

Resolution:

Execute the code object in a modified global scope, adding the
additional variables to a copy of the global variables instead of
passing a predefined local scope.  This allows each PSP page to define
its own local scope.

Below is a patch against the current CVS release 3.1.0a.  Would you
please review it because there may be a reason the "mod_python"
variables were passed in the local scope.

Thanks to all that made this smart solution
-Sean


Index: psp.py
===================================================================
RCS file: /home/cvspublic/httpd-python/lib/python/mod_python/psp.py,v
retrieving revision 1.20
diff -r1.20 psp.py
261,262c261,266
<             exec code in globals(), {"req":req, "session":session,
<                                      "form":form, "psp":psp}
---
>             global_scope = globals().copy()
>             global_scope.update({"req":req, "session":session,
>                                  "form":form, "psp":psp})
> 
>             exec code in global_scope
>



Mime
View raw message