[mod_python] debuging modpython publisher issues

Jim Gallacher jpg at jgassociates.ca
Fri Jun 2 17:10:56 EDT 2006


David Bear wrote:
> I'm getting really frustrated. I have a python program that works.
> Then make modifications that should affect things -- then it breaks.
> 
> I've got to figure a better way of debugging.
> 
> Here's an example:
> 
> I have a python program:
> 
> 
> def _return_psp(req, ad, tmpl="empty.pspml"):
>     '''
>     returns a psp template with all vars and whatever is in ad
>     '''
>     ad.setdefault('title', 'Empty Page')
>     vars = {"ad": ad}
>     return psp.PSP(req, tmpl, vars=vars)
> 
> def getDebug(req):
>     '''
>     returns values for debug and object introspection
>     '''
>     form = req.form
>     info = []
>     info.append('request uri %s ' % req.uri)
>     info.append('req.connection.base_server: %s ' % req.connection.base_server)
>     bodycontent = ''.join(info)
>     return _return_psp(req, dict(body=bodycontent, title="Debug Page"), "empty.pspml")
> 
> # if __name__ == "__main__":
> #    sys.exit(1)
> 
> I have a psp file called empty.pspml that looks like this:
> -----------
> 
> <%
> """ psp  """
> try:
>     title = ad['title']
> except KeyError:
>     title = "Key Error in AD"
> try:
>     dbg = ad['body']
> except KeyError:
>     dbg = 'Didnt have debug Info'
> %>
> <html>
> <head>
>   <title>
>    <%= title %>
>   </title>
> </head>
> <body>
> <h1><%= title %></h1>
> <hr>
> <%= dbg %>
> <hr>
> </body>
> </html>
> 
> ----------
> 
> Okay. It all works the way I expect. Calling the url
> mypython.py/getDebug
> 
> returns the results I expect.
> 
> Then I add a function like this:
> 
> def index(req):
>      return _return_psp(req, dict(body="this is stupid", title="stupid
>      title"))
> 
> 
> Now everything apache returns is a 404 (but the apache not found page
> is NOT returned, ie the body content of the http response is empty)
> 
> Looking in my apache logs all I see is the modpython is reimported a
> module:
> 
> 
> Fri Jun 02 11:51:21 2006] [notice] mod_python: (Re)importing module
> 'mod_python.publisher'
> [Fri Jun 02 11:51:21 2006] [notice] mod_python: (Re)importing module
> 'mypthon' with path set to '['/htdocs/mp']'
> 
> 
> There's got to be a better way. I comment out all the import
> statements the are modpython specific, and use a command line python
> session to import the module -- it imports without errors. I was
> thinking importing it would reveal any syntactic errors... 
> 
> Any other pointers on debug methods?

Nothing earth shattering here, but you may find some of the following 
ideas useful if you are not already using them.

req.log_error(msg) is always helpful for stuffing debug messages into 
the apache error log.

Make sure your apache log configuration is not dropping any messages. 
You want: LogLevel debug

For a 404 response, check the access log to make sure you are actually 
requesting what you think you are requesting. Typos happen.

Use some tool (LiveHeaders in Firefox, curl, wget, lynx, whatever) to 
examine the headers being returned by apache. This may help you catch 
typos such as req.content_type = 'test/html'. Sometimes seeing a typo in 
a context different from your code will help you find it.

When you see a blank page in the browser, confirm that it really *is* 
blank. You may be generating some broken html which is obscuring the 
real output. For example "<hello world" will appear to be to be a blank 
page. Either "view source" in your browser or set req.content_type = 
'text/plain' to help debug.

Likewise, some content you are generating does not seem to be showing in 
your page, make sure it is not being obscured by some broken html.

Make sure you are not unintentionally catching exceptions and obscuring 
the real source of an error.

If it's late at night, go do bed. It's amazing how many bugs are 
magically gone when you get back to work after a good sleep. :)

Jim


More information about the Mod_python mailing list