quetz-mod_python-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Gregory (Grisha) Trubetskoy" <gri...@apache.org>
Subject Re: inital questions and patches
Date Wed, 30 Jun 2004 13:50:43 GMT

On Tue, 29 Jun 2004, Dave Wilson wrote:

> Hi all,
>
> I've been trying to get pyblosxom running under mod_python, and one of
> the problems I ran into was that the pyblosxom.cgi script uses the if
> __name__ == '__main__' idiom as a wrapper around the script's body of
> code. for my initial purposes, I just added __name__ = '__main__'
> directly above the if conditional.
>
> I'm inexperienced with python, but I went looking to see how I could 
> make the __name__ variable be set to '__main__' so I don't have to hack 
> the pyblosxom cgi script, and the first way I was able to find is with 
> execfile().  but I assume the choice of using imp.find_module() and 
> imp.load_module() was made for some specific reasons.  why does 
> cgihandler.py not use execfile() and is there a way to have __name__ set 
> through the imp methods?

Well, first of all, it is made pretty clear in the documentation that the 
cgihandler is a hack. As to why it doesn't use execfile - I don't 
remember. Most likely it's because execfile will actually interpret the 
source code from scratch every time, which is so slow, you might as well 
not bother with mod_python at all and just use the Apache's CGI.


> btw, I also ran into a couple bugs, and have some fixes, which may or
> may not be useful.

see more below

>
> my setup, Apache 2.0.49, Python 2.3.4, mod_python 3.1.3, Redhat
> Enterprise Linux ES 3, kernel 2.4.21-15.0.2.ELsmp.
>
> first, I was getting an exception on the call to len(req.path_info),
> so I added a check to ensure req.path_info != None (I've never posted
> a patch thru gmail, hopefully it comes through ok).
>
> --- mod_python/apache.py        2004-02-16 13:47:27.000000000 -0600
> +++ /usr/local/lib/python2.3/site-packages/mod_python/apache.py
> 2004-06-29 01:59:52.000000000 -0500
> @@ -538,7 +538,7 @@
>     req.add_common_vars()
>     env = req.subprocess_env.copy()
>
> -    if len(req.path_info) > 0:
> +    if req.path_info != None and len(req.path_info) > 0:
>         env["SCRIPT_NAME"] = req.uri[:-len(req.path_info)]
>     else:
>         env["SCRIPT_NAME"] = req.uri

Thanks for this one, I'll add it in

> It might be better to fix the code somewhere else so that path_info is
> guaranteed to be non-None.
>
> also, the main pyblosxom.cgi script imports a config.py script from
> it's same directory, but was failing because this dir wasn't in the
> sys.path.  for my purposes, it seems like it'd be useful to allow a
> python cgi script to import other python scripts from within the same
> directory.  here's a patch that allows this:
>
> --- mod_python/cgihandler.py    2004-02-16 13:47:27.000000000 -0600
> +++ /usr/local/lib/python2.3/site-packages/mod_python/cgihandler.py
> 2004-06-29 02:57:07.000000000 -0500
> @@ -70,6 +70,7 @@
>         dir, file = os.path.split(req.subprocess_env["script_filename"])
>     else:
>         dir, file = os.path.split(req.filename)
> +    sys.path.insert(0, dir)
>     module_name, ext = os.path.splitext(file)
>
>     _lock.acquire()


Now this one I'm not so sure about. cgihandler will chdir into the 
directory of the script, and '.' should be in sys.path by default.

Also, you cannot just insert things into sys.path because after ten 
requests you'll end up with a sys.path that has ten dirs at the beginning 
:-)

Grisha


>
>
> thanks,
> Dave
>

Mime
View raw message