[mod_python] handlers and globals

Neil Gower ngower at dkp.com
Tue Nov 8 16:47:59 EST 2005


I'm having some trouble trying to run my PSP pages through the standard 
Python profiler.  The problem appears to be that the python globals don't 
behave as the profiler expects when it is run through mod_python.

The test handler (below) works okay with the profiler when invoked from a 
regular command line Python interpreter, but it raises a "NameError: name 
'processPsp' is not defined" exception when run through mod_python.  I'm 
taking that to mean that the profiler can't find the processPsp() method to 

Can anyone offer any suggestions?  I'm stumped at this point.



Details of my setup:
Mod_python error: "PythonHandler profiler-handler"

Traceback (most recent call last):

   File "/usr/lib/python2.3/site-packages/mod_python/apache.py", line 299, in 
     result = object(req)

   File "/var/profiler-test/profiler-handler.py", line 36, in handler
     rc = profile.run( 'processPsp()', 'ProfilerTest.psp.prof' )

   File "/usr/lib/python2.3/profile.py", line 71, in run
     prof = prof.run(statement)

   File "/usr/lib/python2.3/profile.py", line 403, in run
     return self.runctx(cmd, dict, dict)

   File "/usr/lib/python2.3/profile.py", line 409, in runctx
     exec cmd in globals, locals

   File "", line 1, in ?

NameError: name 'processPsp' is not defined

<Directory "/var/profiler-test">
     Options Indexes MultiViews
     AllowOverride None
     Order allow,deny
     Allow from all

     AddHandler mod_python .psp
     PythonHandler profiler-handler
     PythonDebug On


<p>This is a test!

req.write( "<p>So is this!" )


# we have to wrap these imports in a try block to be able to run the
# code outside of mod_python, where they will fail.
     from mod_python import apache, psp

import profile

def processPsp():
     A plain old method that uses global variables for the PSP
     parameters, which makes it easy to use with the profiler.
     theRequest.content_type = "text/html"
     template = psp.PSP( theRequest, theTemplateFile )
     return apache.OK
# end processPsp()

def handler( req ):
     The mod_python hook for handling a page request.
     global theRequest
     global theTemplateFile
     theRequest = req
     theTemplateFile = 'ProfilerTest.psp'

     # This works...
     #rc = processPsp()

     # but this doesn't...
     rc = profile.run( 'processPsp()', 'ProfilerTest.psp.prof' )

     return rc
# end handler()

if __name__ == '__main__':
         handler( None )
     except AttributeError, e:
         # if we actually make it into processPsp(), the null request
         # object will cause an AttributeError.
         print "A not unexpected error: %s" % str(e)
# end main.

More information about the Mod_python mailing list