[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)


        #register session.save as a cleanup


        #now call the original

        return fn(req,*args,**kwargs)

    return _setup

def login(req):


    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
[error] [client] python_cleanup: Error calling cleanup
object <bound method DbmSession.save of {'housekeeped': 1}>
[error] [client]     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)

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.


Jeff Hinrichs

More information about the Mod_python mailing list