[mod_python] When using PSP is request object persistent?

Graham Dumpleton graham.dumpleton at gmail.com
Sun Mar 11 04:42:11 EST 2007


On 11/03/07, PETER BARKER <newbarker at btinternet.com> wrote:
> Hello,
>
> See below for the stripped-down code to my problem/query. Note that I do not
> save the session anywhere.
>
> With the two PSP template lines commented, the session is always new
> (because I'm not saving it). When the templating code is uncommented, the
> session is remembered across requests. Why is this?
>
> APACHE CONFIG:
> <Directory "C:/Program Files/Apache Software
> Foundation/Apache2.2/htdocs/pypublish">
>     SetHandler mod_python
>     PythonDebug On
>     PythonHandler mod_python.publisher
> </Directory>
>
> INDEX.HTML:
> <html>
> <head><title>hello</title></head>
> <body>
> </body>
> </html>
>
> THETEST.PY:
> from mod_python import Session, psp
> import os
> def entry(req):
>   req.session = Session.Session(req)
>   if req.session.is_new():
>     returnedDoc = "Newly created session: " + req.session.id()
>   else:
>     returnedDoc = 'Existing session:' + req.session.id()
>   #pathname = os.path.join(os.path.dirname(req.filename),'index.html')
>   #psp.PSP(req,filename=pathname).run()
>   req.content_type = 'text/html'
>   return returnedDoc

Do you use the session object in the PSP page? If you do, then what
you are seeing is consistent with how PSP has perhaps always worked.

As the code stands, if a session object is created, whether it be
prior to PSP being invoked or as a result of a reference to the
session object in a PSP page, the PSP code is always saving the
session object to ensure that the last accessed time is updated.

If the session object wasn't actually referenced from within the PSP
page this raises an interesting corner case which wasn't perhaps
thought of when PSP was changed to use a session object present as
req.session. The issue is if req.session exists but the session object
is not used in the PSP page, should the PSP code be saving the session
to have the last accessed time updated.

Thus, the code in PSP code should perhaps instead of saying:

                # the mere instantiation of a session changes it
                # (access time), so it *always* has to be saved
                if hasattr(req, 'session'):
                    req.session.save()

perhaps say:

                # the mere instantiation of a session changes it
                # (access time), so it *always* has to be saved
                if "session" in code.co_names and hasattr(req, 'session'):
                    req.session.save()

In summary, it is a feature of PSP that if a session object is used in
a PSP page that the session is always saved automatically. The
question is whether it should be saving it when the session was
created before PSP was invoked and the session isn't used in the PSP
page. Problem is that changing the behaviour may be too late now that
it is working this way.


Graham


More information about the Mod_python mailing list