[mod_python] Help with session.save and register_cleanup

Graham Dumpleton graham.dumpleton at gmail.com
Sun May 6 00:00:54 EDT 2007


A cleanup function will always be passed one argument. This will be
the data which is supplied when register_cleanup() is called. If no
cleanup data is supplied, then None will be passed as cleanup data.
Because Session object save() method already has a 'self' argument,
the cleanup data becomes the second argument it complains about.

In short, do this:

  def _cleanup(session):
    session.save()

  req.register_cleanup(_cleanup, req.session)

Graham

On 06/05/07, Jeff Hinrichs - DM&T <jeffh at dundeemt.com> wrote:
> I know that I am missing something here but googling and banging my
> head aren't working so I'll ask.
>
> I'm trying to register the session.save method from the Session I've
> attached to the req object in the req.register_cleanup()  like this:
>
> # housekeeping setup for a request
>
> def housekeeping(fn):
>
>     def _setup(req, *args, **kwargs):
>
>         #if session isn't part of the req object, add it
>
>         if not hasattr(req,"session"):
>
>             req.session = Session.Session(req)
>
>             req.session['housekeeped']=1
>
>         #register session.save as a cleanup
>
>         req.register_cleanup(req.session.save)
>
>         #now call the original
>
>         return fn(req,*args,**kwargs)
>
>     return _setup
>
>
> @housekeeping
> def login(req):
>
>     req.session['foo']='bar'
>
>     return "We are in login() %s" % req.session.keys()
>
> Everything seems peachy, the results returned are:
>
>   We are in login() ['foo', 'housekeeped']
>
> However, the register_cleanup fails to save the session, and is
> returning the following err:
>
> [notice] vampire: Reimporting module
> '/usr/home/apache22/lakota.dundeemt.pri/dora/index.py'
> [error] [client 192.168.2.52] python_cleanup: Error calling cleanup
> object <bound method DbmSession.save of {'housekeeped': 1}>
> [error] [client 192.168.2.52]     exceptions.TypeError: save() takes
> exactly 1 argument (2 given)
>
> I've tried changing the register_cleanup to call req.session.save()
> (as opposed to .save) but that complains, correctly, that save() is
> not callable.
>
> First question, can I register multiple items with multiple calls to
> req.register_cleanup? i.e.
> req.register_cleanup(_closedb, req)
> req.register_cleanup(req.session.save)
>
> or is that my problem?  Or have I just been at the keyboard too long
> to see the error that probably is staring me in the face?  The db
> cleanup appears to be working without error nor phantom connections.
>
> Thanks,
>
> Jeff Hinrichs
> http://www.OmahaPython.org/
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python
>


More information about the Mod_python mailing list