[mod_python] Help with session.save and register_cleanup

Jeff Hinrichs - DM&T jeffh at dundeemt.com
Sat May 5 22:43:43 EDT 2007


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/


More information about the Mod_python mailing list