[mod_python] 'foo.psp is not a file' 404

Graham Dumpleton grahamd at dscpl.com.au
Wed Jul 5 18:23:15 EDT 2006


greg wrote ..
> When a nonsense filename is entered instead of a valid template,
> PSP spits out this exception:
> 
> ValueError: /local/path/foo.psp is not a file
> 
> I would like to make this return a 404 error instead. Is there an easier
> way of doing it besides making my own handler, calling PSP manually, and
> catching the exception?

Using a handler to wrap it is one way, but I wouldn't call psp and catch
the exception. Instead I would check for the files existence and only
call psp if it existed. Thus:

  from mod_python import apache

  # Don't use "import" due to bug in mod_python 3.1.X.
  psp = apache.import_module("mod_python.psp")

  import os

  def handler(req):
    if not os.path.exists(req.filename):
      return apache.HTTP_NOT_FOUND
    return psp.handler(req)

The only way I can think of doing it without requiring a handler to be
written is to use a RewriteRule which detects the file doesn't exist and
forces a NOT_FOUND error response. At least I am presuming that this
could be done, I'm not a RewriteRule expert.

In the end, mod_python.psp should be fixed to return NOT_FOUND
when the file doesn't in fact exist. I'll create JIRA issue for it once I
confirm that later versions behave like this as well.

Graham


More information about the Mod_python mailing list