Issue Details (XML | Word | Printable)

Key: MODPYTHON-164
Type: New Feature New Feature
Status: Closed Closed
Resolution: Fixed
Priority: Major Major
Assignee: Graham Dumpleton
Reporter: Graham Dumpleton
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
mod_python

Allow req.add_handler()/req.register_*_filter() to take module/function for handler.

Created: 18/Apr/06 06:22 PM   Updated: 17/Apr/07 10:39 AM
Return to search
Component/s: core
Affects Version/s: None
Fix Version/s: 3.3.1

Time Tracking:
Not Specified

Resolution Date: 30/Jul/06 10:43 AM


 Description  « Hide
Currently, the:

  req.add_handler(phase, handler, dir)
  req.register_input_filter(filter_name, filter, dir)
  req.register_output_filter(filter_name, filter, dir)

functions require the handler/filter to be a string. This string identifies the module that should be imported and which contains the necessary function, plus optionally, an alternate named function to the default for the phase or filter type. For example:

  req.add_handler("PythonHandler", "mymodule::myhandler")

It would be simpler if the handler/filter argument could be overloaded and instead supplied an actual module reference or callable object reference. For example:

  import mymodule

  def myhandler(req):
    ...

  def fixuphandler(req):
    req.add_handler("PythonHandler", mymodule)
    req.add_handler("PythonHandler", mymodule.myhandler)
    req.add_handler("PythonHandler", myhandler)
    return apache.OK

This would be easier than having to construct a string module/function reference when you have direct access to the handler function.

In the main the "dir" argument would be irrelevant. The only circumstance where it would matter is where PythonInterpPerDirective was used as it could be used to control the interpreter the code executed within. If not supplied, it could be argued that the directory where the supplied module/function is defined in should be used as "dir".

 All   Comments   Work Log   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
Graham Dumpleton made changes - 29/Jul/06 05:16 AM
Field Original Value New Value
Assignee Graham Dumpleton [ grahamd ]
Graham Dumpleton made changes - 29/Jul/06 05:16 AM
Status Open [ 1 ] In Progress [ 3 ]
Repository Revision Date User Message
ASF #426776 Sat Jul 29 10:06:33 UTC 2006 grahamd Added support such that when using req.register_input_filter(),
req.register_output_filter() and req.add_handler() it is now possible to
supply directly a callable object to be used as well as the existing
string reference identifying the module/function to be called. (MODPYTHON-164)
Files Changed
MODIFY /httpd/mod_python/trunk/src/hlistobject.c
MODIFY /httpd/mod_python/trunk/src/hlist.c
MODIFY /httpd/mod_python/trunk/src/filterobject.c
MODIFY /httpd/mod_python/trunk/src/include/requestobject.h
MODIFY /httpd/mod_python/trunk/src/mod_python.c
MODIFY /httpd/mod_python/trunk/lib/python/mod_python/importer.py
MODIFY /httpd/mod_python/trunk/src/requestobject.c
MODIFY /httpd/mod_python/trunk/lib/python/mod_python/__init__.py
MODIFY /httpd/mod_python/trunk/lib/python/mod_python/apache.py
MODIFY /httpd/mod_python/trunk/Doc/appendixc.tex
MODIFY /httpd/mod_python/trunk/src/include/mpversion.h
MODIFY /httpd/mod_python/trunk/test/htdocs/tests.py
MODIFY /httpd/mod_python/trunk/src/include/filterobject.h
MODIFY /httpd/mod_python/trunk/src/include/mod_python.h
MODIFY /httpd/mod_python/trunk/Doc/modpython4.tex
MODIFY /httpd/mod_python/trunk/test/test.py
MODIFY /httpd/mod_python/trunk/src/include/hlist.h
MODIFY /httpd/mod_python/trunk/src/include/mod_python.h.in

Graham Dumpleton added a comment - 29/Jul/06 10:10 AM
Change made, but require argument to functions be the actual callable object, no module reference allowed as suggested originally. Thus can write:

from mod_python import apache

def uppercase(filter):
    s = filter.read()
    while s:
        filter.write(s.upper())
        s = filter.read()
    if s is None:
        filter.close()

def handler(req):
    req.content_type = 'text/plain'
    req.write('handler')
    return apache.OK

def fixuphandler(req):
    req.handler = 'mod_python'
    req.register_output_filter("UPPERCASE", uppercase)
    req.add_output_filter("UPPERCASE")
    req.add_handler('PythonHandler', handler)
    return apache.OK

where Apache configuration just defines the fixup handler. For example:

PythonFixupHandler handlers

The speed increase is significant, as it avoids having to perform additional module lookups when string reference used.

This whole feature is very promising, as it would allow quite powerful dispatch mechanisms to be implemented where the decisions are made in the fixup handler phase rather than the response handler itself. All in all, provides more flexibility by doing it this way.

Graham Dumpleton made changes - 30/Jul/06 10:43 AM
Fix Version/s 3.3 [ 12310101 ]
Status In Progress [ 3 ] Resolved [ 5 ]
Resolution Fixed [ 1 ]
Graham Dumpleton made changes - 17/Apr/07 10:39 AM
Status Resolved [ 5 ] Closed [ 6 ]