Skip to content

Instantly share code, notes, and snippets.

@florentx
Created February 19, 2014 17:15

Revisions

  1. florentx created this gist Feb 19, 2014.
    18 changes: 18 additions & 0 deletions util.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    import re

    # Helper to check if the name is a valid identifier
    _isidentifier = re.compile(r'[a-zA-Z_]\w*').match


    @contextlib.contextmanager
    def savepoint(cr, name, quiet=False):
    assert _isidentifier(name)
    cr.execute('SAVEPOINT "%s";' % name)
    try:
    yield
    except Exception:
    cr.execute('ROLLBACK TO "%s";' % name)
    if not quiet:
    raise
    finally:
    cr.execute('RELEASE "%s";' % name)