Christian Boos wrote:
> Ha, I had *exactly* the same thought... but then I tried to implement 
> it, and it doesn't seem to be possible, as you must return a callable 
> which takes the to-be-decorated function as input, and that callable 
> won't get called until the decorated function is called...

You can call the to-be-decorated function in the callable returned by
as_transaction, can't you?

def run_as_transaction(env, db):
    def decorator(fn):
       if not db:
           db = env.get_db_cnx()
           try:
               fn(db)
           except Exception:
               db.rollback()
           else:
               db.commit()
       else:
           fn(db)
    return decorator

def api_method(self):
    @run_as_transaction(env, db)
    def change_something(db):
        cursor = db.cursor()
        cursor.execute("UPDATE ...")

This will leave a local named "change_something" with the value None.
Not tested, of course...

-- Remy

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to