[mod_python] How can one examine dynamically added handlers?

Graham Dumpleton grahamd at dscpl.com.au
Wed Jan 4 22:56:23 EST 2006

Joseph Barillari wrote ..
> I'm a big fan of mod_python -- I've been using it for a few days and
> it's been great so far.

Yet you seem to be diving straight into a complex area of mod_python
where few if any people dwell. Ie., most wouldn't use dynamic registration
of handlers.

> I'm a bit confused by the add_handler function, though.  Is there any
> way to get a list (from within the PDB, for instance) of all the
> dynamically registered handlers for a given request?

The handlers of the current executing handler phase are available in
"req.hlist". Unfortunately, the only way of seeing the complete list is
to iterative call "req.hlist.next()" which has the side effect of discarding
that at the head of the list. In other words, to seem them you actually
unravel the list. If doing it from inside Pdb session and you are going
to abort the request, probably not a problem, in other circumstances
unravelling the list will prevent later handlers from running as they
will be deregistered. There is no way of interogating from Pdb the
handlers for a later phase.

> Because add_handler fails silently if the supplied handler is bogus,
> it's not easy to figure out where problems lie. If I add a
> PythonHandler with add_handler, and it doesn't get executed, is there
> an easy way to tell if it didn't get executed because it (a) wasn't
> the first handler on the list, (b) had an invalid module name, or (c)
> some other reason?

First off, ensure you are using mod_python 3.2.5b at least. Some
handler registration issues when using PythonHandlerModule and
other cases are dealt with in that. Better reporting of other issues can
be obtained by applying the changes mentioned in:


on top of 3.2.5b.

> If specificity helps, my current problem is this: I have the following
> two lines in a PythonTransHandler:
> req.add_handler("PythonLogHandler","mptest::loghandler")
> req.add_handler("PythonHandler","mptest::handler")
> The PythonLogHandler gets executed, but the PythonHandler doesn't.

In your .htaccess file for that directory or other appropriate place
in Apache configuration for that directory, actually have:

  SetHandler mod_python


  AddHandler mod_python .ext

where ".ext" is replaced with what ever extension the URL will have
you are using to access the resource?

Without one of these, a PythonHandler will not be called, whether it
be specified in the Apache configuration or add dynamically using
req.add_handler() by an earlier handler phase.

> I suspect this is because the default action for log-handlers is to
> execute everything, but the default action for content handlers
> (PythonHandler) is to execute only the first handler in the list -- so
> the default apache handler gets executed instead. I'm not sure how to
> override this using add_handler.

All handlers in any phase will be executed provided that each returns
apache.OK. If they return any other value processing of further handlers
in that phase will be aborted. Aborting in a phase, can cause later
phases to be aborted as well, except for PythonLogHandler which is
always called regardless.

The other return types that a handler could return are apache.DECLINED
which will cause aborting of just that phase and Apache will go looking
for inbuilt handlers that might instead apply to the request. Eg, serving
up a static file. Later phases still run in this case. If apache.DONE is
returned, it indicates that handler has responded with a complete
response, including setting appropriate status in request object. As such
any further handlers in that phase or latter phases will be skipped.

Handlers may also return various HTTP error code responses which again
will cause subsequent handlers to that phase and later phases to be skipped.

Thus, what happens depends on what your return from your handler.


More information about the Mod_python mailing list