

@utils.decorator
def non_transactional(func, args, kwds, allow_existing=True):
    'A decorator that ensures a function is run outside a transaction.\n\n  If there is an existing transaction (and allow_existing=True), the\n  existing transaction is paused while the function is executed.\n\n  Args:\n    allow_existing: If false, throw an exception if called from within\n      a transaction.  If true, temporarily re-establish the\n      previous non-transactional context.  Defaults to True.\n\n  This supports two forms, similar to transactional().\n\n  Returns:\n    A wrapper for the decorated function that ensures it runs outside a\n    transaction.\n  '
    from . import tasklets
    ctx = tasklets.get_context()
    if (not ctx.in_transaction()):
        return func(*args, **kwds)
    if (not allow_existing):
        raise datastore_errors.BadRequestError(('%s cannot be called within a transaction.' % func.__name__))
    save_ctx = ctx
    while ctx.in_transaction():
        ctx = ctx._parent_context
        if (ctx is None):
            raise datastore_errors.BadRequestError('Context without non-transactional ancestor')
    save_ds_conn = datastore._GetConnection()
    try:
        if hasattr(save_ctx, '_old_ds_conn'):
            datastore._SetConnection(save_ctx._old_ds_conn)
        tasklets.set_context(ctx)
        return func(*args, **kwds)
    finally:
        tasklets.set_context(save_ctx)
        datastore._SetConnection(save_ds_conn)

dataset/ETHPy150Open GoogleCloudPlatform/datastore-ndb-python/ndb/model.py non_transactional/original