[mod_python] How to handle exceptions and initialization in mpservlet

Daniel Popowich dpopowich at comcast.net
Sun Sep 5 16:38:15 EDT 2004


Vinj Vinj writes:
> It woudl be helpful to add to the tutorial how to
> handle exceptions in mpservlet. I have spent a
> signifcant amount of time trying to track down
> exceptions. 

It's probably fair to say that the tutorial assumes you are already a
user of mod_python and have a basic understanding of how it works.  I
should probably do a sweep through the tutorial with an eye of someone
who has never used mod_python before.

I use "PythonDebug on" in my apache configs.  All python exceptions go
to the browser and I can quickly debug the problem.  If it's a known
possible exception that a production system can come across, I wrap
them in try/except and "do the right thing" for my app.  In other
words, it's just python code, no special handling is needed in
mpservlets that you wouldn't do in other python programs or mod_python
request handlers.  Of course, you should familiarize yourself with
raising apache.SERVER_RETURN with an appropriate argument, but that's
just mod_python out-of-the-box.

> 1. If there is an exception in a function called
> thorugh _call_ the servlet just returns a blank page. 

I cannot reproduce this problem.  When I raise an exception in a
"_call_" method, the exception propagates back to the browser,
assuming PythonDebug is on.  I have attached a Servlet to the bottom
of this email that "works for me."

> 2. Add a file upload example to the tutorial

Agreed.  I'll add one for the next release.

> 3. How do you initialize server level code...

This is right up there with "how do I maintain global data?" as a FAQ.
What you're after is an Application abstraction which mod_python
doesn't really provide.  The closest thing is the PythonImport
directive in conjuction with interpreter naming.  See section 3.12 of
the mod_python FAQ and section 4.1 of the mod_python manual.

It is on my to-do list to provide an Application abstraction layer in
future versions of mpservlets.  This is non-trivial because of the
differences in apache MPMs, so I'm not sure how soon it will be out.
Grisha's implementation of Session is an example of what it takes to
make your classes "mpm-free", as it were.

Daniel Popowich
-----------------------------------------------
http://home.comcast.net/~d.popowich/mpservlets/


======================================================================
testcall.mps (NOTE: I use HyperText)
======================================================================
# -*- python -*-

from mod_python.servlet import HTMLPage
from HyperText.HTML import *

class testcall(HTMLPage):

    def prep(self):
        HTMLPage.prep(self)
        self.called = 'none'

    def write_content(self):
        form = FORM(method='POST')
        form.append(INPUT(type='submit', name='_call_noE()',
                          value='No Exception'), " ",
                    INPUT(type='submit', name='_call_E()',
                          value='Exception'))

        self.writeln('Called: ', self.called, HR(),
                     form, HR())


    def noE(self):

        self.called = 'noE'

    def E(self):

        self.called = 'E'

        raise 'EXCEPTION'

    ok_methods_to_call = [noE, E]
    
        
======================================================================
output in my browser when I click on "Exception" button
======================================================================

Mod_python error: "PythonHandler mod_python.servlet"

Traceback (most recent call last):

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

  File "/usr/local/lib/python2.3/site-packages/mod_python/servlet.py", line 1455, in handler
    if not servlet.respond():

  File "/usr/local/lib/python2.3/site-packages/mod_python/servlet.py", line 913, in respond
    return Servlet.respond(self) or self.write_html()

  File "/usr/local/lib/python2.3/site-packages/mod_python/servlet.py", line 636, in respond
    return method(self, *args)

  File "/home/popowich/public_html/py/testcall.mps", line 31, in E
    raise 'EXCEPTION'

EXCEPTION



More information about the Mod_python mailing list