[mod_python] python module loaded, but not parsed (huh?)

Graham Dumpleton grahamd at dscpl.com.au
Sat Jan 14 01:34:43 EST 2006

On 14/01/2006, at 2:52 PM, Fabiano Sidler wrote:

> Wow! Two answers within this short time! Are you competing yourself? ;)
> 2006/1/14, Graham Dumpleton <grahamd at dscpl.com.au>:
>> Hmmm, I looked at your code, its awfully complicated.
> Which one? The older is much more difficult than the one I have by
> now. Actually, the container now only set the name for invoking the
> application (handler in this case) and generates some static classes
> like Request.
In this last thread, you have only posted one bit of code. I used the 
"complicated" because you use features line metaclasses, static methods
creation, overloading of new, etc. It just seems to fail the test of 
it simple for me. I can't really see the reason for doing it all, it 
just seems
to me that it is only going to result in something that will be hard to
maintain by someone done the track.

> The problem now is: How can I get the name 'handler' to be set at the
> right place? I'll work on it after a few hours of sleep! ;)
Well, I had a snooze for a couple of hours and didn't help me much. ;-)

In one of your emails you said you were using configuration of:

  SetHandler python-program
  PythonHandler mymodule
  PythonDebug On

This means mymodule::handler() needed to exist. Your quote code
did not even have a mymodule.py and there was no handler() at
global scope in the code, the only reference to handler() was nested
in a function. You were somehow trying to stuff the handler() method
into a module of a class, but that wasn't going to work as the type of
cls.__module__ is a string not a module, so attempts to set Request
and handler in the module were going to be applied to a string object.
Ie., a value containing the name of the module, not the actual module.
All in all, it wasn't even clear where the entry point was going to be 
trigger stuff to created. It also worried me as well that you were 
req.write() a static method.

Let me describe a few basic concepts about mod_python which may
help you (or not).

When you specify 'PythonHandler', the value can be the name of a
module, in which case that module must provide a 'handler()" function
as entry point. Or, it can specify 'module::name' where name is the
name of a function in the module to use as an entry point. Whatever
the entry point is, it needs to take the single argument which is the
request object.

What most people don't realise is that if 'name' identifies an unbound
method of a class type, an instance of the class type will be created
and then the method will be called. Thus you can have:

  # .htaccess

  SetHandler python-program
  PythonHandler example::MyWebApp.__call__

  # example.py

  from mod_python import apache

   class MyWebApp:
     def __init__(self,req):
     def __call__(self,req):
       req.content_type = 'text/plain'
       return apache.OK
In terms of what you are doing, if you wanted an instance of MyWebApp
to be created on each class, this is what you would be best off doing.
The __init__() method could do any initialisation which might be 
to mod_python to create your server independent interface. Because
__init__() must accept a request object, derived classes couldn't 
it though for their own purposes.

Now I don't know if this helps in anyway. I could ramble on a bit more 
other things that may be of interest, but have to run out the door 
right now.


More information about the Mod_python mailing list