def safe_creation(context, create_fn, delete_fn, create_bindings, transaction=True):
 cm = (context.session.begin(subtransactions=True) if transaction else _noop_context_manager())
 with cm:
  obj = create_fn()
  try:
   value = create_bindings(obj['id'])
  except Exception:
   with excutils.save_and_reraise_exception():
    try:
     delete_fn(obj['id'])
    except Exception as e:
     LOG.error(_LE('Cannot clean up created object %(obj)s. Exception: %(exc)s'), {'obj': obj['id'], 'exc': e})
  return (obj, value)

def safe_creation ( context , create_fn , delete_fn , create_bindings , transaction = True ) : cm = ( context . session . begin ( subtransactions = True ) if transaction else _noop_context_manager ( ) ) with cm : obj = create_fn ( ) try : value = create_bindings ( obj [ 'id' ] ) except Exception : with excutils . save_and_reraise_exception ( ) : try : delete_fn ( obj [ 'id' ] ) except Exception as e : LOG . error ( _LE ( 'Cannot clean up created object %(obj)s. Exception: %(exc)s' ) , { 'obj' : obj [ 'id' ] , 'exc' : e } ) return ( obj , value )
this function wraps logic of object creation in safe atomic way .