[mod_python] How to handle exceptions and initialization in mpservlet

Vinj Vinj vinjvinj at yahoo.com
Fri Sep 3 12:54:10 EDT 2004


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. 

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

I added a try/except block in respond using the
following two receips from ASPN (I've attached the
code). This not only gives you the exceptions but also
gives the values of the local variables.

2. Add a file upload example to the tutorial

3. How do you initialize server level code. I plan on
having several python processes (each linked to one
apache fork) listening on each port and then use a
sticky load balancer to ake sure the user goes back to
the same process that was handling his request. Also
in my db I keep track of all the mod_python servers.
Each of these need to read from its own configuration
file. I was planning on putting a PYTHON_OPTION in the
httpd configuration. But how do i get access to it at
init time. I know you can get to it by req.get_option
but that happens only a request time. Is there any way
to get access to this at mod_python initialization
time. 
import sys, traceback

-----------------------------------------------------

def print_exc_web(servlet):
    """
    Print the usual traceback information, followed by
a listing of all the
    local variables in each frame.
    """
    tb = sys.exc_info()[2]
    while 1:
        if not tb.tb_next:
            break
        tb = tb.tb_next
    stack = []
    f = tb.tb_frame
    while f:
        stack.append(f)
        f = f.f_back
    stack.reverse()
    servlet.writeln(ErrorMsg())
    servlet.writeln("Locals by frame, innermost last")
    for frame in stack:
        servlet.writeln('')
        servlet.writeln("Frame %s in %s at line %s" %
(frame.f_code.co_name,
                                            
frame.f_code.co_filename,
                                            
frame.f_lineno))
        for key, value in frame.f_locals.items():
            servlet.writeln("\t%20s = " % key,)
            #We have to be careful not to cause a new
error in our error
            #printer! Calling str() on an unknown
object could cause an
            #error we don't want.
            try:
                servlet.writeln(value)
            except:
                servlet.writeln("<ERROR WHILE PRINTING
VALUE>")

def ErrorMsg(escape=0):
    """
    returns: string

    simualtes the traceback output and if argemument
    <escape> set to 1 (true) the string will be
    converted to fit into html documents without
problems.
    """
    import traceback, sys, string

    type=None
    value=None
    tb=None
    limit=None
    type, value, tb = sys.exc_info()
    body = "Traceback (innermost last):\n"
    list = traceback.format_tb(tb, limit) +           
traceback.format_exception_only(type, value)
    body = body + "%-20s %s" % (
        string.join(list[:-1], ""),
        list[-1],
        )
    if escape:
        import cgi
        body = cgi.escape(body)
    return body





More information about the Mod_python mailing list